On Jun 20, 8:47 am, jason001 <[email protected]> wrote:
> Hey guys, I am extremely new to cake and i've been struggling trying to
> create a single find query. My database has 4 tables, customers, phones,
> customers_phones, and phonetypes. The customers table HABTM phones via
> customers_phones, and the phones table has a foreign key from phonetypes,
> indicating the type of phone number. some sample data might look like this:
>
> customers(id, name) = (1, 'bob')
> customers_phones(id, customer_id, phone_id) = (1, 1, 4)
> phones(id, number, phonetype_id) = (4,5555555,3)
> phonetypes(id, typename) = (3, 'work')
>
> I made models for phone, customer and phonetype, and in my
> customers_controller i was able to find phone numbers related to a specific
> customer by using:
>
> var $uses = array('Customer', 'Phone');
You shouldn't need Phone in there if your associations are set up
correctly.
> $condt = array('conditions'=>array('customer.id'=>$id));
> $phones = $this->Phone->Customer->find('all', $condt);
I'll show you the "lazy" way, by using ContainableBehavior. You'd need
to either add that to Customer's $actsAs array or to AppModel's (which
is what I do, so it's always available).
$this->set(
'data',
$this->Customer->find(
'first',
array(
'conditions' => array(
'Customer.id' => $id
),
'contain' => array(
'Phone' => array(
'PhoneType'
)
)
)
)
);
That should create a variable, $data, for your view with the following
structure:
array(
'Customer' => array(
'id' => ...,
'name' => '...'
),
'Phone' => array(
0 => array(
'id' => ...,
'number' => '...'
'phonetype_id' => ...
'PhoneType' => array(
'id' => ...,
'typename' => ''
)
),
1 => array(
'id' => ...,
'number' => '...'
'phonetype_id' => ...
'PhoneType' => array(
'id' => ...,
'typename' => ''
)
),
...
)
)
So, the query is done on the Customer model, fetching the data for the
given ID, along with all related Phones. Each Phone entry should also
come with its corresponding PhoneType. The instructions were to select
the Customer, containing all Phones and, in turn, containing all
PhoneTypes.
A couple of points: Note that $data here is different than $this-
>data, which is set by Cake when you do a read() or when a form is
submitted. I prefer to use $data, rather than, eg. $phones, $users,
etc. as there's often a lot of mixed data in the result, not just from
a single model. $data seems more intuitive and allows me to set my
result view variables in a consistent fashion. Just a suggestion.
For PhoneType model, I would change the column name to "name", rather
than 'typename'. The array structure will keep things sorted out for
you. And Cake always uses table aliases for queries, so there
shouldn't be any trouble there, either.
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en