Sunday, March 4, 2012

Re: Saving simple associations

Try adding $this->Person->create(); before $this->Person->saveAll($data);

Jeremy Burns
Class Outfit

http://www.classoutfit.com

On 4 Mar 2012, at 12:15:03, FirstTimeBaker wrote:

Tilen hvala but that's not it. the problem is that if a country
already exists in the table, the code does not insert anything. So if
I rerun the code above, not a single person is added.

On Mar 3, 11:43 pm, Tilen Majerle <tilen.maje...@gmail.com> wrote:
In ur for statement use $i++ instead of ++$i :)

Dne sobota, 03. marec 2012 je pošiljatelj FirstTimeBaker <
thinkgrea...@gmail.com> napisal:







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 sitehttp://ask.cakephp.organd 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

athttp://groups.google.com/group/cake-php



--
--
Lep pozdrav, Tilen Majerlehttp://majerle.eu

--
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: