简体   繁体   中英

How to load data of one model through another

I am having a trouble with loading data in CakePhp. I have Customers, Customertypes and Bills models. Customertypes is related with Customers with HABTM relation, but not with Bills model. So my question is next: How can i load just the bills, which belong to the Customers, which belong to a specific Cutomertype, which means i need to load the Bills of the Customers from specific Customertype. How can i load this, using queries ? Do I need 'joins' ?

You should not need to use a an explicit join, either at Model or database level. I assume your "Customer" model "hasMany" "Bills", and the "Bills" model "belongsTo" "Customer"? If so, then you should be able to set up a $conditions array (which you subsequently pass to the paginate() function) in the Bills controller which includes a hash/value something like this :

$conditions["Customer.customer_type_id"] = $someCustomerTypeId;

Obviously, if $someCustomerTypeId needs to be a dynamic value, then you have more work to do - you may need to create a "search" form which returns $this->request->data containing search critera, which you then use to call the "index" function.

Sorry that is a bit vague. If you have, as you say, a HABTM relationship, rather than one-to-many, between customers and customer types, it may be more complicated. But hope that may help a bit. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM