duanchao9559 2013-07-20 07:48
浏览 66
已采纳

在yiimongodbsuite中嵌入嵌入文档

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 );

But this shows an error.

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
                ),
        );
      }

    }

and model Medithtml as

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

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


}

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',
    ),
  ),
)

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

  • 写回答

2条回答 默认 最新

  • douxing8939 2013-07-23 11:10
    关注

    Finally I got solution in this way:

           $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);
          }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备