Skip to content
This repository has been archived by the owner on Nov 21, 2019. It is now read-only.

Simple Embeded Document Model #62

Open
maxchu2021 opened this issue Mar 30, 2012 · 3 comments
Open

Simple Embeded Document Model #62

maxchu2021 opened this issue Mar 30, 2012 · 3 comments

Comments

@maxchu2021
Copy link

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
Copy link
Author

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
Copy link

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
Copy link

Thanks, it fixed!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants