CakePHP search in hasMany relations - cakephp

I have a model Content which belongsTo Categories, hasMany Publishers, and Publisher belongsTo city.
There is also a search form where someone selects from a drop-down box which category to view and which city.
But how can I combine these two in a single paginate condition? I mean I cannot do something like:
$this->paginate('Content',array('conditions' =>array('Category.id'=>$category,
'City.id'=>$city)));
because to get cities cake performs a different query.
Nor can I do something like:
$this->paginate('Content',array('conditions' =>array('Category.id'=>$category),
'contain'=>array('Publisher.City'=>array('conditions'=>array(City.id'=>$city)))));
because this will search according to category and filter the city results according to $city.
I know I can do something like:
$this->Content->Publisher->City->find(...)
but this will change the output of my paginated data.
What I usually do is write my custom query where I LEFT join all models and filter the results in WHERE. But I wanted to aks if there is a more cake (sic) way!
thanks

i've experienced the same thing when i first try to create a simple search engine with hasmany relationship.
i used multiple $this->find() and assigned it in a variable then on the paginate code i used$this->paginate(array_merge(name of the variables used in $this->find()));.
hope this will help you...
~gio

Related

Generating form for associated data on through table

I have a hasMany through schema for volunteers who make up teams. It's like this:
Volunteers belong to many Teams
Teams belong to many Volunteers
Both through a table of Memberships.
The reason why I used a through table is because I need to select certain volunteers as team leaders in certain teams they participate in. The field is boolean --TINYINT(1)-- and part of my join table. This is similar to CakePHP documentation's example of starred, I assume, on which an element is selected as highlighted.
Now, I am wondering on how to select volunteers as team leaders on an form in a way that I can load the list of associated volunteers (or during the selection page) and then mark a checkbox for 'team leader' for each name, or a widget that allows me to select only from the reduced list of volunteers already members of this team. So far, I have decided to leave the associated volunteer selection in the team edit page:
<?php echo $this->Form->control('volunteers._ids'); ?>
Which is pretty easy and straightforward, but from here, But I can't seem to find a way to generate the selection box for the already-reduced list of volunteers. I've found everywhere for an example of this, but I probably don't have the skill level to make a good search, as I'm not sure how to word my question, I guess.
Can you please help me out? I understand that a through table has a belongTo relation, but I don't know whether CakePHP generates the list of inputs for me, or whether I need to create a loop (which would mean that I need to add something on the controller to do so).
Thank you!

cakePHP hasMany not joining the SQL query

I am unable to do a simple condition for my paginator. I have a Survey and Campaign models. Campaigns hasMany Surveys setup in the Campaign model.
I want to select a survey and the paginator should display the campaign is linked to, but I keep getting a Unknown column Survey.id - On inspection I see that Survey is not joined to the Campaign model, only in the WHERE clause. I also found out that hasMany does not join the tables to the query.
How else can I go about doing the above?
I could not find an answer to this. It appears that hasMany does not automatically get added to a query. I have had recommendations that using the containable feature in cake may help.

CakePHP 2.1 HABTM Not Getting Associated Info

I have a question that's been bugging me all afternoon:
I'm making a guitar gear site, so I am using a gear items table, a user table, and gear-to-user bridge table. That is, a user can own multiple items and items can belong to multiple users. So HABTM.
An item belongs to a brand (e.g. Fender - Stratocaster), so I set up a belongsTo relationship in the item model as well as a HasMany relationship in the brands model. When I check the output in the items controller, the gear and its associated brand's data is all there as it should be.
The user control panel (and similar areas) basically list all of the user's owned items. After setting up the HABTM relationship between users and items, I checked the controller's output. While the item's information and the bridge table information all appeared, the item's associated brand information did not. The results should essentially be a list of items, including brand information, as if it were "where user_id = x". Instead, it seems to only be grabbing the item information and none of its relationships.
Is there something I'm missing or a dumb mistake? Thanks.
Did you consider setting/changing the recursive attribute when performing find() on the User?
The higher recursive is set, the deeper associations will be found.
An alternative might be to use the Containable behavior on User:
$this->User->find('first',array(
'conditions'=>array('User.id'=>1),
'contain' => array('Item'=>array('Brand'))
));
Set the recursive level up or you can use ContainableBehavior

CakePHP HABTM find all records that have associated records

I have a model - "Category" which HABTM "Blog". I need to create a query that will only select categories that have more zero blogs associated with them. I'm having trouble working out how to do this. All I've come up with is getting the categories out of the database using find('all'...) and then extracting those that have something in the $category['Blog'] array. Obviously I'd much prefer to not have to ask the database to do all that work, so a more elegant solution would be much appreciated.
Thanks for reading!
not really elegant (short), but I think this is the correct way:
add a blogs_category_count field to categories table
add Category hasMany BLogsCategory with counterCache
so you just have to add a condition to that find('all')

Cakephp retrieve data in HABTM association

I have 2 tables subscribers and distribution list and the relationship between them is HABTM. 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 should i write
$this->DistributionSubscriber->find('list', array('conditions'=>array('distribution_id <>' => 1), 'fields'=>array('subscriber_id'), 'recursive'=>-1));
I would try something like this. You want to start with the join table in this case, and do your find() from there. I haven't checked this syntax, but you should get the idea.

Resources