不可能给出直接有效的答案,因为:问题中的代码没有任何问题。这可能意味着您根本没有运行自己的代码。
鉴于唯一可以给出的答案是建议,对于问题中的示例来说唯一重要的类是Owner
.
检查文件名
The 模型约定 are:
模型类 OptionValue 可以在名为 OptionValue.php 的文件中找到
迄今为止,错误命名的模型文件是“为什么我的模型逻辑没有被执行”问题的最常见原因。一个陷阱是添加后缀Model.php
模型文件,模型文件命名是否正确?
在这种情况下,请检查该文件app/Model/Owner.php
exists.
验证型号是否为your model
如果您确定模型文件命名正确检查应用程序正在使用什么:
debug(get_class($this->Owner));
########## DEBUG ##########
'Owner'
###########################
如果输出是“AppModel”-则不会加载模型文件。 CakePHP 不使用现有文件的原因很少,其中之一将适用:
- 文件不存在 - 名称有拼写错误
- 该应用程序没有在您所在的同一目录中查找
- 该应用程序没有权限打开该文件
验证关联是否存在
debug($this->Owner->hasMany);
########## DEBUG ##########
array(
'Car' => array(
'className' => 'Car',
'foreignKey' => 'owner_id',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'dependent' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
)
###########################
If Car
不在输出中 - 关联未保存,因为它不存在 - 您需要确定原因。
检查加载了哪些文件
如果有疑问,请检查运行时加载了哪些文件:
debug(get_included_files());
########## DEBUG ##########
array(
...
...app/Model/Owner.php
...
)
###########################
这可能表明一个不同的正在加载文件,这会阻止app/Model/Owner.php
从被加载。
使用测试来验证发生了什么
您可以使用测试用例来帮助调试,例如:
<?php
App::uses('Owner', 'Model');
class OwnerTest extends CakeTestCase {
public $fixtures = array(
'app.car',
'app.owner',
);
public function setUp() {
parent::setUp();
$this->Owner = ClassRegistry::init('Owner');
}
public function tearDown() {
unset($this->Owner);
parent::tearDown();
}
public function testCreate() {
$data = array(
'Owner' => array(
'name' => 'Me'
),
'Car' => array(
array(
'Car' => array(
'color' => 'red',
)
),
array(
'color' => 'blue',
)
)
);
$this->assertSame('Owner', get_class($this->Owner));
$this->Owner->getDatasource()->getLog();
$this->Owner->saveAssociated($data);
$log = $this->Owner->getDatasource()->getLog();
$expected = array();
$this->assertSame($expected, $log, 'Look ma, the sql log');
}
}
Which should output:
-> Console/cake test Test/Case/Model/OwnerTest.php
Welcome to CakePHP v2.4.5 Console
---------------------------------------------------------------
App : app
Path: /var/www/files.dev/htdocs/app/
---------------------------------------------------------------
CakePHP Test Shell
---------------------------------------------------------------
PHPUnit 3.7.24 by Sebastian Bergmann.
F
Time: 169 ms, Memory: 9.00Mb
There was 1 failure:
1) OwnerTest::testCreate
Look ma, the sql log
Failed asserting that Array (
'log' => Array (
0 => Array (
'query' => 'BEGIN'
'params' => Array ()
'affected' => null
'numRows' => null
'took' => null
)
1 => Array (
'query' => 'INSERT INTO `test_database_name`.`owners` (`name`) VALUES ('Me')'
'params' => Array ()
'affected' => 1
'numRows' => 1
'took' => 0.0
)
2 => Array (
'query' => 'INSERT INTO `test_database_name`.`cars` (`color`, `owner_id`) VALUES ('red', 1)'
'params' => Array ()
'affected' => 1
'numRows' => 1
'took' => 0.0
)
3 => Array (
'query' => 'INSERT INTO `test_database_name`.`cars` (`color`, `owner_id`) VALUES ('blue', 1)'
'params' => Array ()
'affected' => 1
'numRows' => 1
'took' => 0.0
)
4 => Array (
'query' => 'COMMIT'
'params' => Array ()
'affected' => 1
'numRows' => 1
'took' => 0.0
)
)
'count' => 9
'time' => 0.0
) is identical to Array ().
FAILURES!
Tests: 1, Assertions: 2, Failures: 1.
$
请注意,正确地为“我”创建了一名车主和两辆汽车 - 每辆汽车都链接到“我”的车主记录。