
위 오류는 PHP에서 MongoDBBSONDocument 클래스를 찾을 수 없기 때문에 발생합니다. 이 오류를 해결하려면 MongoDBBSONDocument 클래스를 사용하기 전에 MongoDBBSONDocument 클래스를 포함해야 합니다.
다음과 같이 MongoDBBSONDocument 클래스를 포함한 후 사용하실 수 있습니다.
#hostingforum.kr
php
use MongoDBBSONUnserializable;
use MongoDBBSONUnserializableException;
use MongoDBBSONUnserializableInterface;
use MongoDBBSONUnserializableTrait;
use MongoDBBSONUTCDateTime;
use MongoDBBSONObjectId;
use MongoDBBSONUTCDateTime;
use MongoDBBSONDocument;
use MongoDBBSONBSON;
use MongoDBBSONBSONDocument;
$document = BSONDocument::fromPHP(['name' => 'John', 'age' => 30]);
또한, MongoDBBSONDocument::fromPHP를 사용하여 PHP 객체를 MongoDB의 BSON 문서로 변환할 때, PHP 객체의 속성과 BSON 문서의 필드의 이름이 다를 경우에는 PHP 객체의 속성을 BSON 문서의 필드 이름으로 변환해야 합니다.
다음과 같이 PHP 객체의 속성을 BSON 문서의 필드 이름으로 변환할 수 있습니다.
#hostingforum.kr
php
class Person {
public $full_name;
public $age;
public function __construct($full_name, $age) {
$this->full_name = $full_name;
$this->age = $age;
}
}
$person = new Person('John Doe', 30);
$document = BSONDocument::fromPHP(['name' => $person->full_name, 'age' => $person->age]);
또는 PHP 객체의 속성을 BSON 문서의 필드 이름으로 변환할 수 있습니다.
#hostingforum.kr
php
class Person {
public $full_name;
public $age;
public function __construct($full_name, $age) {
$this->full_name = $full_name;
$this->age = $age;
}
public function toArray() {
return [
'name' => $this->full_name,
'age' => $this->age,
];
}
}
$person = new Person('John Doe', 30);
$document = BSONDocument::fromPHP($person->toArray());
위와 같이 PHP 객체의 속성을 BSON 문서의 필드 이름으로 변환할 수 있습니다.
2025-07-18 17:13