我需要在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');
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。