Simple Embeded Document Model #62

Open
keanyc opened this Issue Mar 30, 2012 · 3 comments

Projects

None yet

3 participants

@keanyc
keanyc commented Mar 30, 2012

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

@keanyc
keanyc commented Apr 12, 2012

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

@ngojchieern

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

Thanks, it fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment