"var $actsAs = array('Containable');"
to my app_model.php, which seems to work, however when i run a find like
this:
this->set('data',
$this->Customer->find('first',
array(
'conditions'=> array('Customer.id'=>$id),
'contain'=>array('Phone')
)
)
);
i get data like this:
Array
(
[Customer] => Array
(
[id] => 1
[firstname] => bob
[lastname] => bobly
)
[Phone] => Array
(
[0] => Array
(
[id] => 1
[number] => 5555555
[phonetype_id] => 2
[CustomersPhone] => Array
(
[customer_id] => 1
[phone_id] => 1
[id] => 1
)
)
)
)
which actually is pretty kool since i can get customer and phone / address /
whatever else i put in that 'contain' array (very neat,will use that
later!), however it does not give me the descriptive text for my phonetypes
and details the habtm table which i won't really use.
I'm wondering suddenly if my models are set up correctly. But then, in my
scaffolding, when i view a customer, i see related phones, and the type_id s
show up as numbers, but! if i 'view' a specific phone with the scaffolding
button provided, i see the phonetype name and not the id. Inversely if i
view a specific phonetype, i can see related phone numbers; so this makes me
think my models are set up correctly after all.
customer->habtm<-phone->belongsTo-><-hasMany<-phonetype
what did i do wrong?
cricket-5 wrote:
>
> On Jun 20, 8:47 am, jason001 <jason.deyalsi...@gmail.com> 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 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
>
>
--
View this message in context: http://old.nabble.com/single-find-query-involving-HABTM-and-hasOne-tp28940627p28964676.html
Sent from the CakePHP mailing list archive at Nabble.com.
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 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:
Post a Comment