Monday, August 31, 2009

Problem getting associations to work

Hi all, I am officially completely stumped. I cannot seem to get
associations to work at all, here is my code:

clients table
mysql> DESCRIBE clients;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(50) | NO | | NULL | |
| address_1 | varchar(100) | NO | | NULL | |
| address_2 | varchar(100) | NO | | NULL | |
| city | varchar(40) | NO | | NULL | |
| state | varchar(10) | NO | | NULL | |
| postal | varchar(10) | NO | | NULL | |
| country | char(2) | NO | | NULL | |
| phone | varchar(10) | NO | | NULL | |
| fax | varchar(10) | NO | | NULL | |
| url | varchar(80) | NO | | NULL | |
+-----------+--------------+------+-----+---------+-------+

contacts_table
mysql> DESCRIBE contacts;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| first_name | varchar(20) | NO | | NULL | |
| last_name | varchar(20) | NO | | NULL | |
| email | varchar(40) | NO | | NULL | |
| phone | varchar(20) | NO | | NULL | |
| cell_phone | varchar(20) | NO | | NULL | |
| fax | varchar(20) | NO | | NULL | |
| client_id | int(11) | NO | | NULL | |
+------------+-------------+------+-----+---------+----------------+

contacts.php
<?php

class Contact extends AppModel {
var $name = 'Contact';
var $belongsTo = 'Client';
}
?>

contacts_controller.php
<?php
class ContactsController extends AppController {
var $name = 'Contacts';
public $view = 'Theme';
public $theme = 'default';
public $helpers = array('Form', 'Html', 'Javascript', 'Time');

public function index() {
$this->set('contacts', $this->Contact->find('all'));
}
}
?>

clients.php
<?php

class Client extends AppModel {
var $name = 'Client';
var $hasMany = 'Contact';
}
?>

clients_controller.php
<?php

class ClientsController extends AppController {
var $name = 'Clients';
public $view = 'Theme';
public $theme = 'default';
public $helpers = array('Form', 'Html', 'Javascript', 'Time');

public function index() {
$this->set('clients', $this->Client->find('all'));
}
}
?>

My understanding is that all of the above should result in both the
clients table and the contacts table being queried, however, this does
not appear to be the case. print_r() on the result in the contacts
controller produces:

Array ( [Contact] => Array ( [id] => 1 [first_name] => John
[last_name] => Doe [email] => someone@somewhere.net [phone] =>
7605555555 [cell_phone] => 765551234 [fax] => [client_id] => 0 ) )

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: