Mr. Carl Tue Jan 24 20:20:21 -0500 2012

Subject: Associations of Associations

I'm unable to load data from a sub-associations from other associations. It only return null.

Model: Customer->Email(Associations)->Email_Type(Associations which doesn't load data only return null)

I'm using CodeIgniter Version 2.1, spark manager 0.0.7, php.activerecord v1.0( from this website not the spark activerecord), and XAMPP 1.7.7.

I can connect all of my model that extends ActiveRecord\Model.

Database layout:

 1 CREATE TABLE IF NOT EXISTS customers 
 2 (
 3     id MEDIUMINT NOT NULL auto_increment,
 4     first_name varchar(15) NOT NULL,
 5     last_name varchar(15) NOT NULL,
 6     comment text,    
 7     PRIMARY KEY (id)
 8 ) ENGINE=INNODB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
 9 
10 CREATE TABLE IF NOT EXISTS email_types
11 (
12     id TINYINT NOT NULL auto_increment,
13     name varchar(20) NOT NULL,
14     PRIMARY KEY (id),
15     INDEX (name)
16 )ENGINE=INNODB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
17 
18 CREATE TABLE IF NOT EXISTS emails
19 (
20     customer_id MEDIUMINT NOT NULL,
21     email_type_id TINYINT NOT NULL,
22     address varchar(256) NOT NULL,
23     PRIMARY KEY (customer_id, email_type_id),
24     FOREIGN KEY (email_type_id) REFERENCES email_types(id) ON UPDATE CASCADE ON DELETE CASCADE,
25     FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE
26 ) ENGINE=INNODB  DEFAULT CHARSET=latin1;

php model code:

 1 class Customer extends ActiveRecord\Model
 2 {
 3     // A one-to-many relationship
 4     static $has_many = array
 5     (
 6         array ('emails',     'class_name' => 'Email',  
 7             'readonly' => true, 'select' => 'email_type_id, address'),
 8         // few more list 
 9     );
10     // Data validation....
11 }
12 
13 class Email extends ActiveRecord\Model
14 {
15     // A one-to-one relationship
16     static $belongs_to = array
17     (
18          array('email_type', 'class_name' => 'Email_Type',  'readonly' => true) 
19     );
20     // Validation of data
21 }
22 
23 class Email_Type extends ActiveRecord\Model
24 {
25     // Validation of data
26 }

I tried switching class Email:static $belongs_to to $has_many however I get the following error:

Fatal error: Uncaught exception 'ActiveRecord\DatabaseException' with message '42S22, 1054, Unknown column 'email_id' in 'where clause'' in....

Second question how much information does the parent class need to load into class for the Associations to work?


Mr. Carl Fri Jan 27 14:38:24 -0500 2012

CLOSE TOPIC

The problem was 'foreign_key' => 'email_type_id'. Because I had more then one foreign_key on my table the Model didn't know which one to choice from.

 1 
 2 class Email extends ActiveRecord\Model
 3 {
 4     // A one-to-one relationship
 5     static $belongs_to = array
 6     (
 7          array('email_type', 'class_name' => 'Email_Type', 
 8              'foreign_key' => 'email_type_id',  'readonly' => true) 
 9     );
10     // Validation of data
11 }
12 

(1-1/1)