php – 在yiimongodbsuite中嵌入嵌入式文档

我需要在yiimongodbsuite中执行upsert命令.
我试过了

$model   = new Murls();          
                $model->userid=$userid;
                $model->title=$title;      
                $model->edits[0] = new Medithtml();
                $model->edits[0]->path= $htm;
                $model->edits[0]->html=$path;
                $model->edits[0]->ci=$ci;
                $model->update(array('_id'=>$rec->_id ),array('userid', 'title','edits' ), true );

但这显示错误.

Murls模型定义如下

   class Murls extends EMongodocument
    {
        public $userid;
        public $title;
        public $edits;



   public static function model($className=__CLASS__)
      {
        return parent::model($className);
      }

      // This method is required!
      public function getCollectionName()
      {
        return 'murls';
      }

      public function attributeLabels()
      {
        return array(
          'html'=>'Html',
        );
      }

      public function embeddedDocuments()
      {
        return array(
                // property name => embedded document class name
                'edits'=>'Medithtml',

        );
      }

      public function behaviors(){
            return array(
                            'embeddedArrays' => array(
                                'class' => 'ext.YiiMongoDbSuite.extra.EEmbeddedArraysBehavior',
                                'arrayPropertyName' => 'edits', // name of property, that will be used as an array
                                'arrayDocclassName' => 'Medithtml'    // class name of embedded documents in array
                ),
        );
      }

    }

并将Medithtml模型化为

class Medithtml extends EMongoEmbeddedDocument{
    public $html;
    public $path;
    public $ci;

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }


}

我需要实现的是,带有$title的记录可以有n个$html,$path和$ci.
任何帮助将不胜感激.
我正在寻找的是存储这样的数据

     array (
  '_id' => 
  MongoId::__set_state(array(
     '$id' => '51ee1956d39c2c7e078d80da',
  )),
  'userid' => '12',
  'title' => 'Mongo',
  'edits' => 
  array (
    0 => 
    array (
      'html' => 'html>body>div:nth-child(2)>a>div>a>div',
      'path' => 'ssssss',
      'ci' => '1',
    ),
    1 => 
    array (
      'html' => 'html>body>div:nth-child(2)>a>div:nth-child(3)>a>h2',
      'path' => '/assets/img/demo/demo-avatar9604.jpg',
      'ci' => '2',
    ),
    2 => 
    array (
      'html' => ' html>body>div:nth-child(2)>a>div:nth-child(3)>a>center:nth-child(16)>a>h1',
      'path' => '333',
      'ci' => '3',
    ),
  ),
)

如果存在具有’title’和’userid’的特定组合的记录,则仅更新comments数组.如果不存在,则将插入新记录

解决方法:

你是从错误的类继承.要保存文档,您必须从EMongodocument而不是EMongoEmbeddedDocument继承.这些类很相似,但目的不同.

> EMongoEmbeddedDocument仅适用于嵌入式文档,它只能用于嵌入式文档
> EMongodocument从EMongoEmbeddedDocument扩展,使用方法将数据实际保存到db.

对于一系列评论,您有两种选择:

>使用普通的PHP阵列 – 简单的可操作性更低,功耗更低,更容易发生错误.
>使用array of embedded documents – 每个评论都是文档,因此可以验证,具有严格的结构等.

认情况下,save / insert / update存储所有属性.对于部分更新,请使用$attributes的组合并将$modify设置为true.警告:传递没有$modify的属性数组将仅存储传递的属性,丢弃文档的其余部分.

public function save($runValidation = true, $attributes = null)
...
public function insert(array $attributes = null)
...
public function update(array $attributes = null, $modify = false)
...

所以在你的情况下你可以这样更新:

$model->update(array('comments'), true);

或者,如果您可以直接删除整个文档,只需保存:

$model->save();

注意:对于复合pk ovverride primaryKey():

public function primaryKey()
{
    return array('title', 'userid');
}

呃,好的stackoverflow有草稿自动保存功能:)

相关文章

MongoTemplate 是Spring Data MongoDB 中的一个核心类,为 S...
笔者今天要分享的是一个项目重构过程中如何将数据库选型由原...
mongodb/mongoTemplate.upsert批量插入更新数据的实现
进入官网下载官网安装点击next勾选同意,点击next点击custom...
头歌 MongoDB实验——数据库基本操作
期末考试复习总结