Jeremy Burns
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 testingassociations.I have two objects: a country and a person. One person can live in onecountry and a country can have many people. I have two test tables asfollows: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:<?phpclass 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:<?phpclass 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. Iwould expect for CakePHP to get the id of Belgium and save the thirdperson with this id in the countries_id field. Obviously I do notunderstand the associations completely yet. Please help.Thanks!Here is My SQL log if you need it:Query Affected Num. rows Took (ms) ActionsSELECT COUNT(*) AS `count` FROM `countries` AS `Country` WHERE`Country`.`name` = 'Belgium' 1 1 1SELECT COUNT(*) AS `count` FROM `countries` AS `Country` WHERE`Country`.`name` = 'Belgium' 1 1 1INSERT INTO `countries` (`name`) VALUES ('Belgium') 1 1 1SELECT COUNT(*) AS `count` FROM `people` AS `Person` WHERE`Person`.`id` = 1 1 1 2 maybe slowINSERT INTO `people` (`id`, `age`, `countries_id`) VALUES (1, 23, 1) 11 1SELECT COUNT(*) AS `count` FROM `countries` AS `Country` WHERE`Country`.`name` = 'Austria' 1 1 1SELECT COUNT(*) AS `count` FROM `countries` AS `Country` WHERE`Country`.`name` = 'Austria' 1 1 1INSERT INTO `countries` (`name`) VALUES ('Austria') 1 1 1SELECT COUNT(*) AS `count` FROM `people` AS `Person` WHERE`Person`.`id` = 2 1 1 1INSERT INTO `people` (`id`, `age`, `countries_id`) VALUES (2, 12, 2) 11 1SELECT COUNT(*) AS `count` FROM `countries` AS `Country` WHERE`Country`.`name` = 'Belgium' 1 1 1--Our newest site for the community: CakePHP Video Tutorialshttp://tv.cakephp.org> Check out the new CakePHP Questions sitehttp://ask.cakephp.organd helpothers with their CakePHP related questions.To unsubscribe from this group, send email tocake-php+unsubscribe@googlegroups.com For more options, visit this groupathttp://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:
Post a Comment