Thursday, October 1, 2009

Cannot figure out my $conditions array

I am sure this is a simple error, but I am just not seeing it. Would
you help me please?

I am trying to build a $conditions array with results from a find
('list'). I have the following models: Distributor which hasMany
Dealers. Dealers which hasMany Advisors. Advisors which belongsTo
Distributors and belongsTo Dealers. So for my Advisor schema looks
like:

id,
distributor_id,
dealer_id,
user_id,
name,
[etc.]

A Distributor is logged in and has a User.id value of "2". This user
has two Distributor records that they own; Distributor.id value of "1"
and "2". Those distributors have a total of two Advisors records. I
want to gather all the Advisors records for those two Distributors.

Here is my code so far in the advisors_controller.php:

/* Set up the "conditions" array to retrieve selected Advisor data */
$conditions = array();
$distributor_id = array(); // Could be more than one distributor...
$dealer_id = array(); // Could be more than one dealer...
$this->Advisor->recursive = 0; // Advisor and dealer and distributor
data

switch($this->user['group_id']) {
case '1': /* Admin shows all */
$conditions = array(); /* Redundant but I like it over and over */
break;
case '2': /* Distributor sees their dealer's advisors */
$distributor_id = $this->Distributor->find(
'list',
array(
'fields' => array('Distributor.id'),
'conditions' => array('user_id' => $this->user['id'])
)
);

Debugger::dump($distributor_id);

$conditions = array('Advisor.distributor_id' => array("1",
"2"));

Debugger::dump($conditions);

break;
}

With this code, with the $conditions hard-coded with the values of
"1", and "2", I have this for the Debugger results:

/* The contents of $distributor_id */
array(
"1",
"2"
)

/* The contents fo $conditions */
array(
"Advisor.distributor_id" => array() /* Why is this empty??? */
)

/* JSON object results */
{"total":2, "advisors":[... then the data ...]

I want my $conditions array to dynamically load the contents of the
$distributor_id array. I've been experimenting and cannot find the
proper syntax. I was hoping that a statement like this would work ...

$conditions = array('Advisor.distributor_id' =>
$distributor_id);

... but it doesn't. Would you help me please? I am trying to simulate
the third example from The Cookbook entry http://book.cakephp.org/view/74/Complex-Find-Conditions.
Thank you in advance for your help!

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