php - CakePHP does not use my models -


i have got these 2 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 controller 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 owner , forgets create cars:

1   begin 2   insert `test`.`owners` (`name`) values ('me') 3   commit 

this erm looks like:

erm

why cakephp not save cars?

it's not possible give directly-working answer because: there's nothing wrong code in question. means aren't running own code @ all.

given answer can given advice, class matters example in question owner.

check filenames

the model conventions are:

the model class optionvalue found in file named optionvalue.php

misnamed model files far common cause of "why model logic not being executed" questions. 1 pitfall add suffix model.php model files, model file named correctly?

in case check file app/model/owner.php exists.

verify model your model

if you're sure model file named correctly check app using:

debug(get_class($this->owner)); ########## debug ########## 'owner' ########################### 

if output "appmodel" - model file not loaded. there few reasons why cakephp not use file exists , 1 of them apply:

  • the file not exist - there typo in name
  • the application not looking in same directory are
  • the application doesn't have permission open file

verify 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 not in output - association isn't saved because cake doesn't exist - you'll need identify why.

check files loaded

if in doubt, check files loaded @ run time:

debug(get_included_files()); ########## debug ########## array(     ...     ...app/model/owner.php     ... ) ########################### 

this may indicate different file being loaded, prevents app/model/owner.php being loaded.

use test verify what's happening

you can use test case aide debugging 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, sql log');     } } 

which should output:

-> console/cake test test/case/model/ownertest.php   welcome cakephp v2.4.5 console --------------------------------------------------------------- app : app path: /var/www/files.dev/htdocs/app/ --------------------------------------------------------------- cakephp test shell --------------------------------------------------------------- phpunit 3.7.24 sebastian bergmann.  f  time: 169 ms, memory: 9.00mb  there 1 failure:  1) ownertest::testcreate ma, sql log failed asserting array (     'log' => array (         0 => array (             'query' => 'begin'             'params' => array ()             'affected' => null             'numrows' => null             'took' => null         )         1 => array (             'query' => 'insert `test_database_name`.`owners` (`name`) values ('me')'             'params' => array ()             'affected' => 1             'numrows' => 1             'took' => 0.0         )         2 => array (             'query' => 'insert `test_database_name`.`cars` (`color`, `owner_id`) values ('red', 1)'             'params' => array ()             'affected' => 1             'numrows' => 1             'took' => 0.0         )         3 => array (             'query' => 'insert `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 ) identical array ().  failures! tests: 1, assertions: 2, failures: 1. $ 

note correctly 1 owner created "me", , 2 cars - each linked owner record "me".


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -