Yii 中的关系装置
如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道是否有一种动态方法可以做到这一点,但以下应该有效:
发布固定装置:
评论固定装置:
I don't know whether there is a dynamic way to do that, but the following should work:
Post fixture:
Comment fixture:
据我所知,您可以使用初始化脚本而不是经典装置。 Yii 文档内容如下:
因此,在您的情况下,您不会使用
protected/tests/fixtures/Comment.php
,而是使用protected/tests/fixtures/Comment.init.php
来执行以下操作:虽然我意识到这是一个很晚的回复,但这应该可以解决您的问题。自从我来到这里谷歌搜索以来,我希望我可以帮助其他需要此信息的人。
As far as I've worked out, you can use init scripts instead of classical fixtures. The Yii documentation reads:
So in your case, instead of having
protected/tests/fixtures/Comment.php
, you would haveprotected/tests/fixtures/Comment.init.php
which does this: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.
我知道它已经得到回答,但我认为这是一个更好的答案。
是的,您可以使用关系的动态字段:
Post Fixture:
Comment Fixture:
PostTest.php
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:
Comment fixture:
PostTest.php
Yii docs CDbFixtureManager