E. K. Thu May 19 18:12:56 -0400 2011

Subject: Delegation with conditionals?

I was wondering if it was possible to use conditionals in a model's delegation setup to avoid delegations which return arrays instead of individual objects?

I have a model with a 'has-many' association to a table of contacts:
class Curriculum extends ActiveRecord\Model { static $table_name = 'curriculum'; static $has_many = array(array('contacts', 'through' => 'cirriculum_contacts')); }

The 'curriculum' table has a (sqlite3) schema like this:
curriculum ( "id" INTEGER PRIMARY KEY NOT NULL, "code" TEXT, "description" TEXT, "created_at" DATETIME, "updated_at" DATETIME);

The 'contacts' table it's connecting to has the following schema:
contacts ("id" INTEGER PRIMARY KEY NOT NULL, "name" TEXT, "email" TEXT, "role" TEXT, "created_at" DATETIME, "updated_at" DATETIME);

And the 'curriculum_contacts' table which connects the two has this schema:
curriculum_contacts ("id" INTEGER PRIMARY KEY NOT NULL, "curriculum_id" INTEGER, "contact_id" INTEGER, "role" TEXT, "created_at" DATETIME, "updated_at" DATETIME);

This seems to work pretty well, but because I have two contacts per curriculum (an 'approval' contact and 'department' contact) if I attempt to set a delegation such as the following:
static $delegate = array(array('name', 'to' => 'contacts', 'prefix' => 'approval_contact')
and then access the delegation with $curriculum->approval_contact_name;
PHP bombs out with 'Call to a member function __get() on a non-object.'

I think this is happening because the delegation results in an array containing both the 'approval' and 'department' contacts. I can just use $curriculum->contact[$i]->name; and it works, leading me to believe that the delegation is returning an array, but I'm wondering if I'm missing something which would make the delegation work and be more specific.

Is there anyway to do something like a conditional in a delegation?
array('name','to' => 'contacts', 'prefix' => 'approval_contact','conditions' => 'WHERE role=\'approval\''), array('name','to' => 'contacts', 'prefix' => 'department_contact','conditions' => 'WHERE role=\'department\'')

If it worked (and it doesn't seem to), would let me do something simple like $curriculum->approval_contact_name;

Is there some way to accomplish this sort of delegation which I'm missing? Thanks in advance for any help.

Yoan B Sun May 22 11:54:27 -0400 2011

Can you try something like this?

 1 class Curriculum extends ActiveRecord\Model {
 2    static $table_name = 'curriculum';
 3    static $has_many = array(
 4       array('approval_contacts', 'class_name' => 'Contact', 'through' => 'curriculum_contacts', 'conditions' => "roleĀ = 'department'"),
 5       array('department_contacts', 'class_name' => 'Contact', 'through' => 'curriculum_contacts', 'through' => "role = 'approval'"),
 6    );
 7    static $delegate = array(
 8       array('name', 'to' => 'approval_contacts', 'prefix' => 'approval_contact'),
 9       array('name', 'to' => 'department_contacts', 'prefix' => 'department_contact'),
10    );
11 }

I don't see what you did as a good DB design. (https://secure.wikimedia.org/wikipedia/en/wiki/Database_normalization) But that's not my problem after all.