本文介绍了CakePHP不使用我的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有这两个CakePHP V 2.4.5模型:I have got these two CakePHP V 2.4.5 models:class Owner extends AppModel { public $name = 'Owner'; public $hasMany = array('Car');}和class Car extends AppModel { public $name = 'Car'; public $belongsTo = array('Owner');}我写了:var $uses = array('Owner', 'Car');public function test(){ $data = array( 'Owner' => array( 'name' => 'Me' ), 'Car' => array( array('color' => 'red'), array('color' => 'blue') ) ); $this->Owner->saveAssociated($data, array('deep' => true));}但是CakePHP创建了所有者并忘记创建他的汽车:But CakePHP created the owner and forgets to create his cars:1 BEGIN2 INSERT INTO `test`.`owners` (`name`) VALUES ('Me')3 COMMIT这是我的ERM的样子:This is what my ERM looks like: 为什么CakePHP不保存汽车?Why does CakePHP not save the cars?推荐答案这是不可能给一个直接工作的答案,因为:题。这可能意味着你没有运行你自己的代码。 It's not possible to give a directly-working answer because: There's nothing wrong with the code in the question. That probably means you aren't running your own code at all. 考虑到可以给出的唯一答案是advice,问题中唯一重要的例子是 Owner 。Given that the only answer that can be given is advice, the only class that matters for the example in the question is Owner. 模型约定是:模型文件是迄今为止为什么是我的模型逻辑未执行问题的最常见的原因。一个缺陷是添加后缀 Model.php 到模型文件,是正确命名的模型文件? Misnamed model files is by far the most common cause of "why is my model logic not being executed" questions. One pitfall is to add the suffix Model.php to model files, is the model file named correctly? 在这种情况下,请检查文件 app / Model / Owner.php 是否存在。In this case check that the file app/Model/Owner.php exists.如果您确定模型文件命名正确检查应用程序使用的是什么:If you're sure the model file is named correctly check what the app is using:debug(get_class($this->Owner));########## DEBUG ##########'Owner'###########################如果输出为AppModel模型文件未加载。有很少的原因,为什么CakePHP不会使用一个文件存在,其中一个将应用:If the output is "AppModel" - the model file is not loaded. There are very few reasons why CakePHP will not use a file that exists and one of them will apply: 文件不存在 - 是名称中的拼写错误。 应用程序不在同一目录中 应用程序没有打开文件的权限 debug($this->Owner->hasMany);########## DEBUG ##########array('Car' => array( 'className' => 'Car', 'foreignKey' => 'owner_id', 'conditions' => '', 'fields' => '', 'order' => '', 'limit' => '', 'offset' => '', 'dependent' => '', 'exclusive' => '', 'finderQuery' => '', 'counterQuery' => ''))###########################如果 Car 不在输出中 - 关联不会保存,因为它不存在 - 你需要识别为什么If Car is not in the output - the association isn't saved because to cake it doesn't exist - you'll need to identify why.如果有疑问,请检查运行时加载的文件: If in doubt, check which files are loaded at run time:debug(get_included_files());########## DEBUG ##########array( ... ...app/Model/Owner.php ...)###########################这可能表示正在加载不同的文件, app / Model / Owner.php 。This may indicate that a different file is being loaded, which prevents app/Model/Owner.php from being loaded.您可以使用测试用例来帮助调试,例如:You can use a test case to aide debugging for example:<?phpApp::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'); }} -> Console/cake test Test/Case/Model/OwnerTest.php Welcome to CakePHP v2.4.5 Console---------------------------------------------------------------App : appPath: /var/www/files.dev/htdocs/app/---------------------------------------------------------------CakePHP Test Shell---------------------------------------------------------------PHPUnit 3.7.24 by Sebastian Bergmann.FTime: 169 ms, Memory: 9.00MbThere was 1 failure:1) OwnerTest::testCreateLook ma, the sql logFailed 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.$请注意,为我正确创建了一个所有者,两个车辆 - 分别链接到我的所有者记录。Note that correctly one owner was created for "Me", and two cars - each linked to the owner record for "Me". 这篇关于CakePHP不使用我的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 17:25