在 yiimongodbsuite 中插入嵌入的文档 [英] Upsert embedded document in yiimongodbsuite

查看:21
本文介绍了在 yiimongodbsuite 中插入嵌入的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to perform an upsert command in yiimongodbsuite. I tried

$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 模型定义如下

Murls model is defined as follows

   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 建模为

and model Medithtml as

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.任何帮助将不胜感激.我正在寻找的是存储这样的数据

What I need to achieve is that a record with $title can have n number of $html , $path and $ci. Any help will be appreciated. What I am looking is to store data like this

     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' 的特定组合的记录存在,则仅更新评论数组.如果不存在,将更新新记录插入

Only the comments array will be updated if record with a particular combination of 'title' and 'userid' exists.If it doesn not exists a new record will be inserted

推荐答案

最后我是这样解决的:

       $rec = $model->find($criteria)  ;
           if($rec){

       foreach($rec->edits as  $editarray){
            $var[]=$editarray;
       }
        $edits_new= new Medithtml();
        $edits_new['html']=$htm;
        $edits_new['ci']=$ci;
        $edits_new['path']=$path;  
        $var[]=$edits_new;
        $rec->edits=$var;

        $rec->userid=$userid;
        $rec->title=$title;
        $rec->update(array('edits'  ), true);
      }

这篇关于在 yiimongodbsuite 中插入嵌入的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆