ItGo.me Focus on IT Recommend

Home > CakePHP multi-HABTM associations with pagination

CakePHP multi-HABTM associations with pagination

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

up vote 0 down vote favorite For an e-commerce app that I'm building I am using CakePHP. I have created the db and afterwards baked my app with cake bake. My models are all linked up properly as follows: Product hasMany CategoryProduct,ImagesProduct Category hasMany CategoryProduct CategoryProduct belongsTo Product,Category Image hasMany ImagesProduct ImagesProduct belongsTo Product,Image I have tried in various ways to obtain a paginated view of products dependent of the category_id with no succes. I have managed to obtain the array I wanted with $this->CategoryProducts->findByCategoryId($id), but I can't use the built-in paginator from cakePHP with the resultant array. Could somebody tell me how to properly formulate the $options['joins'] array to pass on to the paginate function in order to get the same result, but in a paginated way? The SQL for what I want to do would be something like this: SELECT p . * , i.filename FROM products p LEFT JOIN ( category_products cp, categories c, images_products ip, images i ) ON ( cp.product_id = p.id AND c.id = cp.category_id AND c.id =2 AND ip.product_id = p.id AND ip.image_id = i.id ) cakephp pagination cakephp-1.3 has-and-belongs-to-many
  |
  this question edited Feb 18 '11 at 18:37 Ish 13.1k 11 46 72 asked Feb 18 '11 at 13:55 Andrei Coman 104 1 1 7



 |  1 Answers

up vote 1 down vote This is a question that perplexed me for quite sometime. You shouldn't have to associate either of your join models (CategoryProduct, ImagesProduct) directly to your models if you're using a HABTM association with CakePHP. cake bake may not have picked it up the HABTM association correctly if you didn't have the table

Recommend:has and belongs to many - Cakephp retrieve data in HABTM association

BTM. Now here particular users are associated to distribution list, i wish to add more users, but when adding new users to distribution list, i would like to show users who are not associated with that distribution lists. what condition sho

names quite right. The join tables should be categories_products and images_products, which would make th join models CategoriesProduct and ImagesProduct. Either way though, the following should get you going on filtering by categories: //in products_controller.php:

//Fake a HasOne association, setting the reset parameter to false since pagination

//requires two queries to complete (one for the count, one for the data)

$this->Product->bindModel(array(

'hasOne' => array(

'CategoriesProduct

)

), false);

//Group by products since the HasOne will return multiple rows for each product

$options = array(

'group' => 'Product.id',

'conditions' => array(

'CategoriesProduct.category_id' => $categories

)

);

//To paginate the result:

$this->paginate = $options;

$products = $this->paginate();

In this example $categories can be either a single category_id or an array containing category_ids (i.e. array('1', '2', '3')). The result will be an 'OR' of categories. If you're looking to filter by an 'AND' type condition, check http://edblogs.cal.msu.edu/devteam/2011/02/08/cakephp-habtm-searches/. Hope this helps.
  |
  this answer edited Feb 20 '11 at 5:17 answered Feb 19 '11 at 4:47 wreality 56 4



 | 

Recommend:has and belongs to many - CakePHP find HABTM

e Message belongsto User Message HABTM Tag Tag HABTM Message If a User is logged in he might want to see all Message tagged with something. $messages = $this->Message->find('all', array( 'conditions' => array("Message.user_id" => $this-

up vote 0 down vote favorite For an e-commerce app that I'm building I am using CakePHP. I have created the db and afterwards baked my app with cake bake. My models are all linked

------splitte line----------------------------