YiiMongoDbSuite icon indicating copy to clipboard operation
YiiMongoDbSuite copied to clipboard

Simple Embeded Document Model

Open maxchu2021 opened this issue 12 years ago • 3 comments

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

maxchu2021 avatar Mar 30 '12 04:03 maxchu2021

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];

maxchu2021 avatar Apr 12 '12 07:04 maxchu2021

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]);
            }
    }

ngojchieern avatar Apr 26 '13 10:04 ngojchieern

Thanks, it fixed!

1nstinct avatar Oct 17 '14 11:10 1nstinct