简体   繁体   English

嵌入式文档数组YiiMongoDbSuite

[英]Array of Embedded Documents YiiMongoDbSuite

I have three classes. 我有三节课。 The course consists of the stages. 该课程包括各个阶段。 The stages consists of the steps. 这些阶段包括步骤。

class Course extends EMongoDocument{
....
    public function behaviors()
    {
        return array(
            'embeddedArrays' => array(
                'class'=>'ext.YiiMongoDbSuite.extra.EEmbeddedArraysBehavior',
                'arrayPropertyName'=>'stages',
                'arrayDocClassName'=>'Stage'
            ),
        );
    }
}

class Stage extends EMongoEmbeddedDocument{
...
    public function behaviors()
{
        return array(
            'embeddedArrays' => array(
                'class'=>'ext.YiiMongoDbSuite.extra.EEmbeddedArraysBehavior',
                'arrayPropertyName'=>'steps',
                'arrayDocClassName'=>'Step'
            ),
        );
}
class Step extends EMongoEmbeddedDocument{
...
}

In mongodb i have data: 在mongodb中,我有数据:

{
    "name" : "course1",
    "online" : "0",
    "author_id" : ObjectId("521df3f1e405688411000029"),
    "approved" : false,
    "stages" : [ 
        {
            "_id" : ObjectId("521dfd84e40568d80900002a"),
            "name" : null,
            "steps" : null,
            "price" : null
        }, 
        {
            "_id" : ObjectId("5220c648e40568701c000031"),
            "name" : null,
            "steps" : [ 
                {
                    "_id" : ObjectId("5220c648e40568701c000032"),
                    "name" : null
                }
            ],
            "price" : null
        }
    ],
    "short_description" : "test",
    "_id" : ObjectId("521dfd7ce40568d809000029")
}

When i try read data from mongodb: Fatal error: Call to a member function toArray() on a non-object in * *extensions\\YiiMongoDbSuite\\extra\\EEmbeddedArraysBehavior.php on line 104 当我尝试从mongodb读取数据时:致命错误:在第104行的* * extensions \\ YiiMongoDbSuite \\ extra \\ EEmbeddedArraysBehavior.php中的非对象上,调用成员函数toArray()

$arrayOfDocs[] = $doc->toArray();

If stage not have steps all works fine. 如果阶段没有步骤,则一切正常。 What i do wrong? 我做错了什么?

There appear to be multiple versions of YiiMongoDbSuite on GitHub. GitHub上似乎有多个版本的YiiMongoDbSuite Are you using v1.3.6.3 of canni/YiiMongoDbSuite ? 您是否正在使用canni / YiiMongoDbSuite v1.3.6.3

Looking at the beforeToArray() method in EEmbeddedArraysBehavior.php , the code seems to access the property and then immediately iterator over it. 纵观beforeToArray()的方法EEmbeddedArraysBehavior.php ,代码似乎在它访问属性后立即迭代器。 If the stages.0.steps field in your document is null , I don't see how the first is_array() check in the behavior code will pass. 如果文档中的stages.0.steps字段为null ,那么我看不到行为代码中的第一个is_array()检查如何通过。 I would suggest debugging the function and inspecting the value of the stages property around where it is saved to the _cache property and before the foreach() iteration. 我建议调试该函数,并在将其保存到_cache属性的位置以及在foreach()迭代之前检查stages属性的值。 My guess is that it may be an array with a single element whose value is null . 我的猜测是它可能是一个具有单个元素的数组,其值是null

I found a solution. 我找到了解决方案。 While filling values for embedded document fields it gets an array (what to fill) from function attributeNames() of that embedded document, then flip it and so on. 在为嵌入式文档字段填充值时,它会从该嵌入式文档的function attributeNames()获取一个数组(填充内容),然后将其翻转 ,依此类推。 So to fix NULL values you should fill in the return array of that function. 因此,要修复NULL值,您应该填写该函数的返回数组。 My code will help you: 我的代码将帮助您:

<?php
class Pricing extends EMongoEmbeddedDocument {
    public $setup;
    public $monthly;
    public $annually;
    public function rules() {
        return array(
        );
    }
    public function attributeNames() {
        return array(
            'setup' => 'setup',
            'monthly' => 'monthly',
            'annually' => 'annually'
        );
    }
}

also this code 也是这个代码

public function setAttributes($values, $safeOnly=true)
    {
        if(!is_array($values))
            return;

        if($this->hasEmbeddedDocuments())
        {
            $attributes=array_flip($safeOnly ? $this->getSafeAttributeNames() : $this->attributeNames());

            foreach($this->embeddedDocuments() as $fieldName => $className)
                $this->$fieldName = new $className;
                if(isset($values[$fieldName]) && isset($attributes[$fieldName]))
                {
                    $this->$fieldName->setAttributes($values[$fieldName], $safeOnly);
                    unset($values[$fieldName]);
                }
        }

        parent::setAttributes($values, $safeOnly);
    }

helped me to prevent fatal error Fatal error: Call to a member function setAttributes() on a non-object in EMongoDocument.php 帮助我防止致命错误致命错误:调用EMongoDocument.php中非对象上的成员函数setAttributes()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM