Yii 中的关系装置

发布于 2024-11-15 08:06:36 字数 344 浏览 4 评论 0原文

如何在 Yii 中建立具有关系的装置?例如,帖子可以有评论,我如何引用夹具中的帖子 ID 来创建评论?

帖子固定装置:

return array(
  'post1'=>array(
    'title'=>'My title',
    'body'=>'My text',
  ), 
  ...

评论固定装置:

return array(
  'comment1'=>array(
    'text'=>'Comment text...',
    'post_id'=> ???
  ), 

How do I set up fixtures with relationships in Yii? For example, with posts can have comments, how do I refer to the post id in a fixture for creating comments?

Post fixture:

return array(
  'post1'=>array(
    'title'=>'My title',
    'body'=>'My text',
  ), 
  ...

Comment fixture:

return array(
  'comment1'=>array(
    'text'=>'Comment text...',
    'post_id'=> ???
  ), 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绾颜 2024-11-22 08:06:36

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

发布固定装置:

return array(
  'post1' => array(
    'id' => 1 
    'title' => 'My title',
    'body' => 'My text',
), 

评论固定装置:

return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => 1
),

I don't know whether there is a dynamic way to do that, but the following should work:

Post fixture:

return array(
  'post1' => array(
    'id' => 1 
    'title' => 'My title',
    'body' => 'My text',
), 

Comment fixture:

return array(
  'comment1' => array(
    'text' => 'Comment text...',
    'post_id' => 1
),
倾城花音 2024-11-22 08:06:36

据我所知,您可以使用初始化脚本而不是经典装置。 Yii 文档内容如下:

也有可能我们不喜欢默认的重置方式
表,即截断它并用夹具数据插入它。如果
遇到这种情况,我们可以写一个初始化脚本
特定的夹具文件。脚本必须命名为表名
后缀为 .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';

虽然我意识到这是一个很晚的回复,但这应该可以解决您的问题。自从我来到这里谷歌搜索以来,我希望我可以帮助其他需要此信息的人。

As far as I've worked out, you can use init scripts instead of classical fixtures. The Yii documentation reads:

It is also possible that we do not like the default way of resetting a
table, i.e., truncating it and inserting it with the fixture data. If
this is the case, we can write an initialization script for the
specific fixture file. The script must be named as the table name
suffixed with .init.php. For example, the initialization script for
the Post table would be Post.init.php. When CDbFixtureManager sees
this script, it will execute this script instead of using the default
way to reset the table.

So in your case, instead of having protected/tests/fixtures/Comment.php, you would have protected/tests/fixtures/Comment.init.php which does this:

// 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';

Although I realize it's a very late reply, that should fix your problem. Since I got here googling, I was hoping I could help someone else who needs this info.

甜味拾荒者 2024-11-22 08:06:36

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

Post Fixture:

return array(
  'post1' => array(
    'title' => 'My title',
    'body' => 'My text',
), 

Comment Fixture:

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

PostTest.php

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

Yii docs CDbFixtureManager

I know it's already answered but I think this is a better answer.
Yes, you can use dynamic fields for relations:

Post fixture:

return array(
  'post1' => array(
    'title' => 'My title',
    'body' => 'My text',
), 

Comment fixture:

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

PostTest.php

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

Yii docs CDbFixtureManager

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文