Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Simple Embeded Document Model #62

Open
keanyc opened this Issue · 3 comments

3 participants

@keanyc

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

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
Something went wrong with that request. Please try again.