2

我正在尝试为 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 有一个事件的外键(我的 Event 模型看起来像这样):

<?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');
}
?>

我得到的错误是:

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

我承认我对 CakePHP 夹具数据的理解很少(文档有点稀缺,网络上的例子都在重复一些琐碎的例子)所以关于我可以/应该做什么的任何想法?

4

1 回答 1

3

看看 Debuggable 的好人提供的Fixturize shell。一旦您的数据库中有数据,您就可以使用这个 shell 将其自动提取到固定装置中。省去了很多头痛。

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.

于 2009-06-04T16:23:37.850 回答