如何使用 CakePHP 中的 SimpleTest 处理固定装置中的外键关系以进行单元测试?

发布于 2024-07-23 18:36:38 字数 1590 浏览 2 评论 0原文

我正在尝试为 CakePHP 中的单元测试创​​建一些固定数据(通过 SimpleTest),但我不知道如何处理我的外键关系。 这是固定代码的示例:

<?php
class SpecialtyFixture extends CakeTestFixture {
    var $name = "Specialty";
    var $import = "Specialty";

    var $records = array(
            array(
               'id' => '1', 
               'event_id' => '1', 
               'code' => 'endocrin-1', 
               'name' => 'Endocrinology'),
            array(
               'id' => '2', 
               'event_id' => '1', 
               'code' => 'ent-1', 
               'name' => 'Ear, Nose and Throat')
    );
}
?>

因此,正如您可以猜测的那样,Specialty 有一个事件的外键(我的事件模型如下所示):

<?php
class Event extends AppModel {
    var $name = "Event";
    var $primaryKey = "id";

    var $hasMany = array(
            'EventLocation' => array('className' => 'EventLocation'),
            'Faculty' => array('className' => 'Faculty'),
            'Agenda' => array('className' => 'Agenda'),
            'Role' => array('className' => 'Role'),
            'Specialty' => array('className' => 'Specialty'),
    );

    var $hasAndBelongsToMany = array('User');
}
?>

我收到的错误是这样的:

意外的 PHP 错误 [SQL 错误: 1054:“字段列表”中的未知列“event_id”]严重性[E_USER_WARNING]在[/dev/trunk/cake/libs/model/datasources/dbo_source.php第525行] /dev/trunk/app/tests/cases/models/event.test.php -> 事件测试用例 -> endCase

我承认我对 CakePHP 固定数据的理解很少(文档有点稀缺,网络上的示例都是重复的一些微不足道的示例)所以关于我可以/应该做什么有什么想法吗?

I'm trying to create some fixture data for my unit tests in CakePHP (via SimpleTest) and I have no idea how to handle my foreign key relationships. Here's a sample of the fixture code:

<?php
class SpecialtyFixture extends CakeTestFixture {
    var $name = "Specialty";
    var $import = "Specialty";

    var $records = array(
            array(
               'id' => '1', 
               'event_id' => '1', 
               'code' => 'endocrin-1', 
               'name' => 'Endocrinology'),
            array(
               'id' => '2', 
               'event_id' => '1', 
               'code' => 'ent-1', 
               'name' => 'Ear, Nose and Throat')
    );
}
?>

So, as you can guess Specialty has a foreign key to event (my Event model looks like this):

<?php
class Event extends AppModel {
    var $name = "Event";
    var $primaryKey = "id";

    var $hasMany = array(
            'EventLocation' => array('className' => 'EventLocation'),
            'Faculty' => array('className' => 'Faculty'),
            'Agenda' => array('className' => 'Agenda'),
            'Role' => array('className' => 'Role'),
            'Specialty' => array('className' => 'Specialty'),
    );

    var $hasAndBelongsToMany = array('User');
}
?>

The error I'm getting is this:

Unexpected PHP error [<span style = "color:Red;text-align:left"><b>SQL Error:</b> 1054: Unknown column 'event_id' in 'field list'</span>] severity [E_USER_WARNING] in [/dev/trunk/cake/libs/model/datasources/dbo_source.php line 525]
/dev/trunk/app/tests/cases/models/event.test.php -> EventTestCase -> endCase

I'll admit my understanding of CakePHP fixture data is minimal (documentation is kinda scarce, and examples on the web all rehash somewhat trivial examples) so any ideas on what I can/should do?

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

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

发布评论

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

评论(1

好久不见√ 2024-07-30 18:36:38

看一下 Fixturize shell 由 Debuggable 的优秀人员完成。 一旦数据库中有数据,您就可以使用此 shell 自动将其提取到装置中。 省去了很多头痛的事情。

和您一样,我对灯具的了解有限,但我认为您可以通过在数据库中获取可靠的内容并将其提取到灯具中来避免这种痛苦。

Take a look at the Fixturize shell by the good folks at Debuggable. Once you have data in your database, you can use this shell to extract it into fixtures automatically. Saves an awful lot of headache.

Like you, my knowledge of fixtures is limited, but I think you'd be able to avoid this kind of pain by getting something solid in your database and just extracting that to fixtures.

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