YiiMongoDbSuite
YiiMongoDbSuite copied to clipboard
Simple Embeded Document Model
After inserted the document. While "$user = User::model()->find();" I've got fatal error as following:
Fatal error: Call to a member function setAttributes() on a non-object in...
Document link: http://canni.github.com/YiiMongoDbSuite/xhtml/basic.simple-embedded-document.html
In EMongoDocument.php line 366:
$this->$fieldName->setAttributes($values[$fieldName], $safeOnly);
I change rewrite it to following codes and just to make it work.
$this->$fieldName = $values[$fieldName];
That is a solution. But in my opnion, it's not good. Cause when you display Embedded Document info using $data->embeddedClassName->fieldname will give an error called trying to get property on a non-object. So I'd ike to change the content of EMongoDocument::setAttributes() to this which can solve the problem more suitable: if($this->hasEmbeddedDocuments()) { $attributes=array_flip($safeOnly ? $this->getSafeAttributeNames() : $this->attributeNames());
foreach($this->embeddedDocuments() as $fieldName => $className)
if(isset($values[$fieldName]) && isset($attributes[$fieldName]))
{
$this->$fieldName->setAttributes($values[$fieldName], $safeOnly);
unset($values[$fieldName]);
}
}
to:
if($this->hasEmbeddedDocuments()) { $attributes=array_flip($safeOnly ? $this->getSafeAttributeNames() : $this->attributeNames());
foreach($this->embeddedDocuments() as $fieldName => $className)
// Create an embedded document object
$this->$fieldName = new $className;
if(isset($values[$fieldName]) && isset($attributes[$fieldName]))
{
$this->$fieldName->setAttributes($values[$fieldName], $safeOnly);
unset($values[$fieldName]);
}
}
Thanks, it fixed!