gpt4 book ai didi

php - 在 yii2/codeception 数据文件中引用 fixture 记录

转载 作者:可可西里 更新时间:2023-10-31 22:47:54 25 4
gpt4 key购买 nike

有没有办法在 Yii2/Codeception ActiveFixture 的 fixture 数据文件中指定另一个 fixture 的相关行?考虑这个用户/个人资料关系的例子:

用户.php:

return [
'user1' => [
'email' => 'user1@example.net',
]
];

配置文件.php:

use common\models\User;
return [
'profile1' => [
'user_id' => User::findOne(['email' => 'user1@example.net'])->id;
'name' => 'My Name',
]
];

文档指出“您可以为行指定别名,以便稍后在测试中,您可以通过别名引用该行。”有没有办法引用另一个 fixture 内的行?例如,在 profile.php 中使用类似 $this->user('user1')->id 的东西?我找不到关于如何做到这一点的任何提及。你如何创建这种相关的固定装置?

最佳答案

load() 方法执行后,别名数据只能在专用 Fixture 对象的范围内通过其 data 属性访问。使此数据可从另一个 Fixture 对象的数据文件访问的唯一方法是将其注册到某个全局对象,例如 Application 对象。

我通常只是在构建依赖数据集之前查询所有需要的数据:

use common\models\User;
$users = User::find()->indexBy('email')->all();
return [
'profile1' => [
'user_id' => $users['user1@example.net']->id,
'name' => 'My Name',
]
];

关于php - 在 yii2/codeception 数据文件中引用 fixture 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27544490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com