Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii中的关系夹具_Php_Yii_Fixtures - Fatal编程技术网

Php Yii中的关系夹具

Php Yii中的关系夹具,php,yii,fixtures,Php,Yii,Fixtures,我如何与Yii中的关系建立固定装置?例如,如果帖子可以有注释,我如何在创建注释的夹具中引用帖子id 立柱固定装置: return array( 'post1'=>array( 'title'=>'My title', 'body'=>'My text', ), ... return array( 'comment1'=>array( 'text'=>'Comment text...', 'post_id'=>

我如何与Yii中的关系建立固定装置?例如,如果帖子可以有注释,我如何在创建注释的夹具中引用帖子id

立柱固定装置:

return array(
  'post1'=>array(
    'title'=>'My title',
    'body'=>'My text',
  ), 
  ...
return array(
  'comment1'=>array(
    'text'=>'Comment text...',
    'post_id'=> ???
  ), 
return array(
  'post1' => array(
    'id' => 1 
    'title' => 'My title',
    'body' => 'My text',
), 
return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => 1
),
return array(
  'post1' => array(
    'title' => 'My title',
    'body' => 'My text',
), 
return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => $this->getRecord('post', 'post1')->id
),
注释固定装置:

return array(
  'post1'=>array(
    'title'=>'My title',
    'body'=>'My text',
  ), 
  ...
return array(
  'comment1'=>array(
    'text'=>'Comment text...',
    'post_id'=> ???
  ), 
return array(
  'post1' => array(
    'id' => 1 
    'title' => 'My title',
    'body' => 'My text',
), 
return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => 1
),
return array(
  'post1' => array(
    'title' => 'My title',
    'body' => 'My text',
), 
return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => $this->getRecord('post', 'post1')->id
),

我不知道是否有一种动态的方法可以做到这一点,但以下方法应该有效:

立柱固定装置:

return array(
  'post1'=>array(
    'title'=>'My title',
    'body'=>'My text',
  ), 
  ...
return array(
  'comment1'=>array(
    'text'=>'Comment text...',
    'post_id'=> ???
  ), 
return array(
  'post1' => array(
    'id' => 1 
    'title' => 'My title',
    'body' => 'My text',
), 
return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => 1
),
return array(
  'post1' => array(
    'title' => 'My title',
    'body' => 'My text',
), 
return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => $this->getRecord('post', 'post1')->id
),
注释固定装置:

return array(
  'post1'=>array(
    'title'=>'My title',
    'body'=>'My text',
  ), 
  ...
return array(
  'comment1'=>array(
    'text'=>'Comment text...',
    'post_id'=> ???
  ), 
return array(
  'post1' => array(
    'id' => 1 
    'title' => 'My title',
    'body' => 'My text',
), 
return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => 1
),
return array(
  'post1' => array(
    'title' => 'My title',
    'body' => 'My text',
), 
return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => $this->getRecord('post', 'post1')->id
),

据我所知,您可以使用init脚本而不是传统的fixture。草案内容如下:

也有可能我们不喜欢默认的重置方法 表,即截断它并将其与夹具数据一起插入。如果 在这种情况下,我们可以为 特定夹具文件。脚本必须命名为表名 后缀为.init.php。例如,的初始化脚本 Post表将是Post.init.php。当CDbFixtureManager看到 此脚本将执行此脚本,而不是使用默认脚本 重置表格的方法

因此,在您的情况下,您将使用
protected/tests/fixtures/Comment.php
,而不是
protected/tests/fixtures/Comment.init.php
,它可以实现以下功能:

// the $this variable refers to the CBdFixtureManager instance
$this->truncateTable($tableName);
$post = $this->getRecord('Post','post1'); // get dependent fixture

// define new fixture
$commentAttributes = array(
  'text' => 'Comment text...',
  'post_id' => $post->id
);
$comment = new Comment;
$comment->setAttributes($commentAttributes);
if(!$comment->save()) throw new CException('Unable to save fixture');

// add new row primary key
$pk = Comment::model()->getTableSchema()->primaryKey;
if(is_string($pk))
  $commentAttributes[$pk] = $comment->$pk;
elseif(is_array($pk))
  foreach($pk as $key)
    $commentAttributes[$key] = $comment->$key;

$this->_rows['Comment']['comment1'] = $commentAttributes;
$this->_records['Comment']['comment1'] = 'Comment';

虽然我意识到这是一个很晚的答复,这应该可以解决你的问题。自从我来到谷歌,我希望我能帮助其他需要这些信息的人。

我知道已经有答案了,但我认为这是一个更好的答案。 是的,您可以为关系使用动态字段:

立柱固定装置:

return array(
  'post1'=>array(
    'title'=>'My title',
    'body'=>'My text',
  ), 
  ...
return array(
  'comment1'=>array(
    'text'=>'Comment text...',
    'post_id'=> ???
  ), 
return array(
  'post1' => array(
    'id' => 1 
    'title' => 'My title',
    'body' => 'My text',
), 
return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => 1
),
return array(
  'post1' => array(
    'title' => 'My title',
    'body' => 'My text',
), 
return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => $this->getRecord('post', 'post1')->id
),
注释固定装置:

return array(
  'post1'=>array(
    'title'=>'My title',
    'body'=>'My text',
  ), 
  ...
return array(
  'comment1'=>array(
    'text'=>'Comment text...',
    'post_id'=> ???
  ), 
return array(
  'post1' => array(
    'id' => 1 
    'title' => 'My title',
    'body' => 'My text',
), 
return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => 1
),
return array(
  'post1' => array(
    'title' => 'My title',
    'body' => 'My text',
), 
return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => $this->getRecord('post', 'post1')->id
),
postest.php

public $fixtures=array(
    'post'=>'Post',
    ...
);

Yii docs

我们在这里讨论的是关系查询吗?不是吗?我只想创建与生产系统中的关系类似的夹具(用于单元测试)。我的糟糕。先生,我会调查的。谢谢您的回复,尽管有时间延迟!我希望它能对将来的人有所帮助。我试过了,但没用,我在引导或配置上有什么不同吗?cdbFixtureManager中的_记录对我来说总是空的什么是_记录?它是否适用于硬编码id?如果不是,那么它与我的答案无关,您应该寻求有关创建测试和装置的帮助。提示:我认为$fixture数组中的顺序很重要,因此请确保首先包含主表(这里的'post'是$fixture中的第一个元素,然后是'comment'。使用硬编码id时,除此之外,不需要其他配置。我猜你拼错了什么。例如,cdbFixtureManager没有任何“getRecords”方法。它有“getRecord”,应该返回一个ActiveRecord对象。也许您使用了“->id”来获取id,但它应该与您的模型中的id相匹配。我想我找到了原因:CDBFixtureManager按照readdir php函数给出的顺序从fixture文件夹加载所有fixture文件。要按特定顺序加载fixture,您需要按该顺序将它们添加到$fixture数组中,并在“fixtures”文件夹中创建一个空init.php文件。希望有帮助。如果有人想知道,这确实有效-尽管文档中提到动态生成
id
字段,并声明不设置它们。因为fixture的加载顺序取决于php readdir命令列出fixture文件的顺序,可能需要暂时禁用外键约束。