如何使用 CakePHP 中的 SimpleTest 处理固定装置中的外键关系以进行单元测试?
我正在尝试为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下 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.