2015-09-24 91 views
1

中添加灯具并进行了一些研究后,我没有找到任何适当的答案来解决这个问题。在Cakephp 3.x

我开始为我的CakePHP(3.x)应用程序编写测试,我在想如何添加灯具及其关联?换句话说,我们如何将灯具链接到另一个灯具,而无需直接在灯具内写入主键和外键。

我有一个用户表和一个UserProfiles表。 用户通过user_id列拥有一个UserProfile。这是我的用户夹具。

namespace App\Test\Fixture; 

use Cake\TestSuite\Fixture\TestFixture; 

class UsersFixture extends TestFixture 
{ 

    public $import = ['table' => 'users']; 
    public $records = [ 

     [ 
      'username' => '[email protected]', 
      'primary_role' => 1 
      'is_active' => 1, 
     ] 
    ]; 

} 

这是我的用户配置夹具

namespace App\Test\Fixture; 

use Cake\TestSuite\Fixture\TestFixture; 

class UserProfilesFixture extends TestFixture 
{ 

    public $import = ['table' => 'user_profiles']; 
    public $records = [ 

     [ 
      'first_name' => 'Mark', 
      'last_name' => 'Yellowknife', 
     ] 
    ]; 

} 

如何我都记录联系在一起?也就是说,我如何告诉用户配置文件记录添加其user_id以链接到用户记录?

必须用夹具来做到这一点,而无需手动编写密钥。

谢谢你们!

回答

1

你必须在灯具中定义这个。唯一的另一种方法是在运行时发出更新查询来设置外键,但这不过是一个好主意。

除此之外,没有一种方法可以做到这一点,我的意思是,如果没有明确定义它,代码如何知道哪些记录需要彼此关联?

因此,您至少需要定义外键,并且根据主键的生成方式(自动递增整数,UUID等),您可能必须定义它们。