CakePHP does not use my models
I have got these two CakePHP V 2.4.5 models:
class Owner extends AppModel {
public $name = 'Owner';
public $hasMany = array('Car');
}
and
class Car extends AppModel {
public $name = 'Car';
public $belongsTo = array('Owner');
}
in my controller I wrote:
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));
}
But CakePHP created the owner and forgets to create his cars:
1 BEGIN
2 INSERT INTO `test`.`owners` (`name`) VALUES ('Me')
3 COMMIT
This is what my ERM looks like:
Why does CakePHP not save the cars?
Solution 1:
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.
Given that the only answer that can be given is advice, the only class that matters for the example in the question is Owner
.
Check filenames
The model conventions are:
The Model class OptionValue would be found in a file named OptionValue.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?
In this case check that the file app/Model/Owner.php
exists.
Verify that the model is your model
If you're sure the model file is named correctly check what the app is using:
debug(get_class($this->Owner));
########## DEBUG ##########
'Owner'
###########################
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:
- The file does not exist - there is a typo in the name
- The application is not looking in the same directory you are
- The application doesn't have permission to open the file
Verify the association exists
debug($this->Owner->hasMany);
########## DEBUG ##########
array(
'Car' => array(
'className' => 'Car',
'foreignKey' => 'owner_id',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'dependent' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
)
###########################
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.
Check which files are loaded
If in doubt, check which files are loaded at run time:
debug(get_included_files());
########## DEBUG ##########
array(
...
...app/Model/Owner.php
...
)
###########################
This may indicate that a different file is being loaded, which prevents app/Model/Owner.php
from being loaded.
Use a test to verify what's happening
You can use a test case to aide debugging for example:
<?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.
$
Note that correctly one owner was created for "Me", and two cars - each linked to the owner record for "Me".
Solution 2:
I'v tested your code.
It works perfect.
Try to remove models cache in /tmp/cache folder
My results:
(default) 3 queries took 0 ms Nr Query Error Affected Num. rows Took (ms)
1 BEGIN 0 0 0
2 INSERT INTO `intranet`.`owners` (`name`) VALUES ('Me') 1 1 0
3 INSERT INTO `intranet`.`cars` (`owner_id`, `color`) VALUES (3, 'red') 1 1 0
4 INSERT INTO `intranet`.`cars` (`owner_id`, `color`) VALUES (3, 'blue') 1 1 0
5 COMMIT 1 1 0
Solution 3:
Can you please try this:
$this->Owner->saveAll($data);
Instead of
$this->Owner->saveAssociated($data, array('deep' => true));
Thanks