Saturday, March 3, 2012

Saving simple associations

Hi all.

I am new with CakePHP and I have the following problem with testing
associations.

I have two objects: a country and a person. One person can live in one
country and a country can have many people. I have two test tables as
follows:

Table countries:

CREATE TABLE IF NOT EXISTS `countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `a` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Note, that the field name in countries has to be unique.

Table people:

CCREATE TABLE IF NOT EXISTS `people` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`age` int(11) NOT NULL,
`countries_id` int(11) NOT NULL,
PRIMARY KEY (`id`,`countries_id`),
UNIQUE KEY `name_UNIQUE` (`age`),
KEY `fk_persons_countries` (`countries_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

As you can notice, both tables are associated through the foreign key
'countries_id' in people.

I have the corresponding models:

Model Country:

<?php
class Country extends AppModel {
public $name = 'Country';

var $useDbConfig = 'test'; // We use the test database.

public $validate = array ('name' => 'isUnique');

public $hasMany = array(
'Person' => array(
'className' => 'Person',
'foreignKey' => 'countries_id'
));
}
?>

Model Person:

<?php
class Person extends AppModel {
public $name = 'Person';

var $useDbConfig = 'test'; // We use the test database.

public $belongsTo = array('Country' => array(
'className' => 'Country',
'foreignKey' => 'countries_id'
),);
}
?>

In people controller I wish to add the following three people:

$people = array
(
array('id' => 1, 'age' => 23, 'country' => 'Belgium'),
array('id' => 2, 'age' => 12, 'country' => 'Austria'),
array('id' => 3, 'age' => 55, 'country' => 'Belgium')
);

with the following code:
for ($i = 0; $i < 3; ++$i)
{
$data['Person'] = array('id' => $people[$i]['id'], 'age' =>
$people[$i]['age']);
$data['Country'] = array('name' => $people[$i]['country']);

$this->Person->saveAll($data);
}

However, the third person is not saved and no error is reported. I
would expect for CakePHP to get the id of Belgium and save the third
person with this id in the countries_id field. Obviously I do not
understand the associations completely yet. Please help.

Thanks!

Here is My SQL log if you need it:

Query Affected Num. rows Took (ms) Actions
SELECT COUNT(*) AS `count` FROM `countries` AS `Country` WHERE
`Country`.`name` = 'Belgium' 1 1 1

SELECT COUNT(*) AS `count` FROM `countries` AS `Country` WHERE
`Country`.`name` = 'Belgium' 1 1 1

INSERT INTO `countries` (`name`) VALUES ('Belgium') 1 1 1
SELECT COUNT(*) AS `count` FROM `people` AS `Person` WHERE
`Person`.`id` = 1 1 1 2 maybe slow

INSERT INTO `people` (`id`, `age`, `countries_id`) VALUES (1, 23, 1) 1
1 1
SELECT COUNT(*) AS `count` FROM `countries` AS `Country` WHERE
`Country`.`name` = 'Austria' 1 1 1

SELECT COUNT(*) AS `count` FROM `countries` AS `Country` WHERE
`Country`.`name` = 'Austria' 1 1 1

INSERT INTO `countries` (`name`) VALUES ('Austria') 1 1 1
SELECT COUNT(*) AS `count` FROM `people` AS `Person` WHERE
`Person`.`id` = 2 1 1 1

INSERT INTO `people` (`id`, `age`, `countries_id`) VALUES (2, 12, 2) 1
1 1
SELECT COUNT(*) AS `count` FROM `countries` AS `Country` WHERE
`Country`.`name` = 'Belgium' 1 1 1

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: