CakePHP: select users who have 1 or more posts - php

I have two tables: users and posts. I'm trying to select any users that have at least one post.
I'm guessing this require either a join or a subselect. Is there something in CakePHP that makes this simple?

Use counter caching, see http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#belongsto
counterCache: If set to true the associated Model will automatically
increase or decrease the “[singular_model_name]_count” field in the
foreign table whenever you do a save() or delete(). If it’s a string
then it’s the field name to use. The value in the counter field
represents the number of related rows. You can also specify multiple
counter caches by using an array where the key is field name and value
is the conditions. E.g.:
array(
'recipes_count' => true,
'recipes_published' => array('Recipe.published' => 1) ) counterScope: Optional conditions array to use for updating counter
cache field.
No additional query needed in this case. It will automatically in- and decrease the count in the users table if you add/delete posts.
Example:
public $belongsTo = array(
'User' => array(
'foreignKey' => 'user_id',
'counterCache' => true,
'counterScope' => array(
'published' => 1)));

This should select all users that made 1 post or more
SELECT * FROM `users` as u
INNER JOIN `posts` as p ON p.user_id = u.id
In cake you should be able to do something like this:
$this->User->find('all', array('joins' =>
array(
'table' => 'posts',
'alias' => 'p',
'type' => 'inner',
'conditions' => array(
'User.id = p.user_id'
)
)
);
[EDIT]
Maybe you could add
'group' => 'User.id'
or
'fields' => 'DISTINCT User.id, ...'
To remove duplicates entries

Related

CakePHP ignores condition

I have the following problem with CakePHP:
Two tables are joined (filters and accounts). Then I am building conditions and only the second condition Account.active =>1 gets executed. If I print the result, there are still showing filters that are having another mode_id than 3.
$joins= array(
array('table' => 'filters',
'alias' => 'Filter',
'type' => 'right',
'conditions' => array(
'Filter.account_id = Account.id',
)
),
);
Then I execute the request including joins and conditions
$activeAccounts = $this->Account->find('all',array(
'conditions'=>array('AND'=>array('Filter.mode_id'=>3,'Account.active'=>1)),
'joins'=>$joins));
The models were checked and no problems identified. Filter belongs to Account. Account has many Filter.
Below the query that is generated. The results are still showing filters with Filter.mode_id other than 3
Here is the query that is generated. The results are still containing rows with Filter.mode_id other than 3 despite the fact that one condition is 'Filter.mode_id'=>3
SELECT `Account`.`id`, `Account`.`user_id`, `Account`.`name`,
`Account`.`api_key`, `Account`.`account_number`, `Account`.`remaining_balance`,
`Account`.`investment_size`, `Account`.`active`
FROM `baseline_db`.`accounts` AS `Account`
right JOIN `baseline_db`.`filters` AS `Filter`
ON (`Filter`.`account_id` = `Account`.`id`)
WHERE ((`Filter`.`mode_id` = 3) AND
(`Account`.`active` = '1'))
Like say Oldskool, use the Model associations
and for your condition, The "AND" is not necessary,
you cant put :
$activeAccounts = $this->Account->find('all',array(
'conditions' => array(
'Filter.mode_id'=>3,
'Account.active'=>1
)
));
the request you want to make with the type of relation you have, seem to me weird.
If i understand, perhaps with something like that :
$this->loadModel('Filter');
$filters =$this->Filter->find("list", array(
'conditions' => array('Filter.mode_id' => 3),
'fields' => array('Filter.account_id')
));
$activeAccounts = $this->Account->find('all',array(
'conditions' => array(
'Account.account_id'=>$filters,
'Account.active'=>1
)
));

cakephp belongsto custom condition issue

I’m using cakephp 1.3 with mysql database and engine is MYISAM, I’m getting problem in relationships. I have two tables name Organization and OrgClasses, these two are associated with mbo_studio_id which is common in both table.
organization
Id(PK), name, mbo_studio_id
org_classes
id(PK), name,date, mbo_studio_id
I’m trying to fetch data on the condition of mbo_studio_id, but it adds another condition by primary key of OrgClasses.
var $belongsTo = array(
'OrgClass' => array(
'className' => 'OrgClass',
'foreignKey' => 'mbo_studio_id',
'conditions' => array('OrgClass.mbo_studio_id' => 'Organization.mbo_studio_id'),
'order' => 'OrgClass.date DESC',
'dependent' => false,
)
);
I get following query
SELECT `Organization`.`id`, `Organization`.`name, `Organization`.`mbo_studio_id`, `OrgClass`.` id`,`OrgClass`.`mbo_studio_id`, `OrgClass`.`name`, `OrgClass`.`date`
FROM `organizations` AS `Organization`
LEFT JOIN `org_classes` AS `OrgClass` ON (`Organization`.`mbo_studio_id` = `OrgClass`.`id` AND `OrgClass`.`mbo_studio_id` = Organization.mbo_studio_id)
Here I don’t want Organization.mbo_studio_id = OrgClass.id condition in query.
Thanks
Then you need to set foreignKey to false:
'foreignKey' => false,
This way it will only use your custom conditions.

CakePHP find list of associated items where id is

I want to create in cake php application with users, games and games platforms (for ex PS3)
I got tables:
userGames (game have one platform)
id, name, platform_id
users (users can have many platforms)
id, username
platforms
id, name
users_platforms
id, platform_id, user_id
And now i want to select all user id=1 platforms, and list it for select tag.
Here is sql query:
SELECT platforms.name, platforms.id FROM platforms LEFT JOIN platforms_users ON platforms_users.platform_id=platforms.id WHERE platforms_users.user_id=1
But i dont know what to list this by find('list') function in cakePHP
I try type in users controller:
$this->User->Platform->find('list', array('conditions', array('User.id'=>'1')));
But this returns sql problem (undefinded User.id)
Anyone can help me?
please try this
$this->loadModel('UserPlatform');
$this->UserPlatform->bindModel(array(
'belongsTo' => array('Platform')
));
$this->UserPlatform->find('list', array(
'fields' => array('Platform.id','Platform.name'),
'conditions' => array('UserPlatform.user_id'=>'1'),
'recursive' => 1
));
you must apply find function on join table
Your find must same as this:
$this->PlatformUser->find('list',array('fields'=>
array('Platform.name','Platform.id'),'conditions'=> array('PlatformUser.id' => 1)));
Your PlatformUser Model must have:
public $belongsTo = array(
'Platform' => array(
'className' => 'Platform',
'foreignKey' => 'platform_id',
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
),
);
What you are trying to do is actually a HasAndBelongsToMany Association.
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
hasAndBelongsToMany (HABTM)
We’ll need to set up an extra table in the database to handle HABTM associations. This new join table’s name needs to include the names of both models involved, in alphabetical order, and separated with an underscore ( _ ). The contents of the table should be two fields that are foreign keys (which should be integers) pointing to the primary keys of the involved models. To avoid any issues, don’t define a combined primary key for these two fields. If your application requires a unique index, you can define one. If you plan to add any extra information to this table, or use a ‘with’ model, you should add an additional primary key field (by convention ‘id’).
HABTM requires a separate join table that includes both model names.
(This would be the UserPlatform-table if I got that right.)
Make sure primary keys in tables cakes and recipes have “id” fields as assumed by convention. If they’re different than assumed, they must be changed in model’s primaryKey.
class Recipe extends AppModel {
public $hasAndBelongsToMany = array(
'Ingredient' =>
array(
'className' => 'Ingredient',
'joinTable' => 'ingredients_recipes',
'foreignKey' => 'recipe_id',
'associationForeignKey' => 'ingredient_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'with' => ''
)
);
}

CakePHP nesting two select queries

I have two tables in cakePHP.
competencies
------------
id
name
competenceRatings
-----------------
id
competence_id
user_id
rating
I need a way to write the following query in the cake way:
SELECT * FROM competencies WHERE id NOT IN (SELECT competence_id FROM competence_ratings WHERE employee_id = $userId)
Someone please help me!!
What i did before going to this subquery method:
I tried competencies->hasMany->competenceRatings, competenceRatings->belongsTo->competencies relations.
$competencies = $this->Competence->CompetenceRating->find('all',array('CompetenceRating.user_id' => $userId,'CompetenceRating.competence_id !=' => 'Competence.id'));
I want to be able to get the names of competencies for which a user have NOT made any ratings into competenceRatings table. i.e., I need list of names from competencies table for which there are no entries in comptenceRatings table(for given user_id).
EDIT
I tried table join also:
$options['joins'] = array(
array(
'table' => 'competence_ratings',
'alias' => 'CompetenceRating',
'type' => 'LEFT OUTER',
'conditions' => array(
'Competence.id = CompetenceRating.competence_id'
)
)
);
$options['conditions'] = array( 'CompetenceRating.employee_id' => $employee['Employee']['id'] );
$competencies = $this->Competence->find('all',$options);
you would probably have to use a subquery():
$subqueryOptions = array('fields' => array('competence_id'), 'conditions' => array('employee_id'=>$user_id));
$subquery = $this->Competence->CompetenceRating->subquery('all', $subqueryOptions);
$res = $this->Competence->CompetenceRating->find('all', array(
'conditions' => array('id NOT IN '. $subquery)
));
the source for subquery is here:
https://github.com/dereuromark/tools/blob/2.0/Lib/MyModel.php#L405
you need to put this in your AppModel.php
BUT I think the subquery is not necessary. You can probably make a single and easy query out of it:
$this->Competence->CompetenceRating->find('all', array(
'group' => 'competence_id',
'conditions' => array('NOT' => 'employee_id'=>$user_id)),
'contain' => array('Competence')
));
dont forget to include Competence via "contain" if you have recursive set to -1.

CakePHP order query results on more than 1 level

I'm using the Containable behavior to get a list of Comments (belongsTo Post, which belongs to Question; Question hasMany Post, and Post hasMany Comments; all of these belong to Users).
$data = $this->Question->find ( 'first',
array ('contain' =>
array ('User',
'Post' => array ('User', /* 'order' => 'User.created DESC'*/ )
)
)
);
It works, when I comment out the section in comments above. I suppose this is to be expected, but what I want is all of the Posts that are found, should be sorted in order of the 'created' field of the 'User' they belong to. How do I accomplish this deeper level sorting in CakePHP? I always get, "Warning (512): SQL Error: 1054: Unknown column 'User.created' in 'order clause'"
Thanks for your help!
Also, you might be trying to group on a related table from a find call that doesn't use joins.
Set your debug level to something greater than 1 so you can see the query log and make sure that Cake isn't doing two queries to fetch your data. If that is the case then the first query is not actually referencing the second table.
If you want to manually force a join in these situations you can use the Ad-Hoc joins method outlined by Nate at the following link.
http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-in-model-find
I have found two ways to get around this.
The first is to define the second level associacion directly in the model.
Now you will have access to this data everywhere.
It should look something like this.....
var $belongsTo = array(
'Foo' => array(
'className' => 'Foo', //unique name of 1st level join ( Model Name )
'foreignKey' => 'foo_id', //key to use for join
'conditions' => '',
'fields' => '',
'order' => ''
),
'Bar' => array(
'className' => 'Bar', //name of 2nd level join ( Model Name )
'foreignKey' => false,
'conditions' => array(
'Bar.id = Foo.bar_id' //id of 2nd lvl table = associated column in 1st level join
),
'fields' => '',
'order' => ''
)
);
The problem with this method is that it could make general queries more complex than they need be.
You can thus also add the second level queries directly into te find or paginate statement as follows: (Note: I found that for some reason you can't use the $belongsTo associations in the second level joins and will need to redefine them if they are already defined. eg if 'Foo' is already defined in $belongsTo, you need to create a duplicate 'Foo1' to make the association work, like the example below.)
$options['joins'] = array(
array('table' => 'foos',
'alias' => 'Foo1',
'type' => 'inner',
'conditions' => array(
'CurrentModel.foo_id = Foo1.id'
)
),
array('table' => 'bars',
'alias' => 'Bar',
'type' => 'inner',
'foreignKey' => false,
'conditions' => array(
'Bar.id = Foo1.bar_id'
)
)
);
$options['conditions'] = array('Bar.column' => "value");
$this->paginate = $options;
$[modelname] = $this->paginate();
$this->set(compact('[modelname]'));
I hope this is clear enough to understand and that it helps someone.
Check your recursive value. If it's too limiting, it will ignore the containable links, IIRC. I remember bumping into this a few times. I'd try containing multiple models, but my recursive option was set to 0 and nothing would get pulled. For your example, I'd think that a value of 1 (the default) would suffice, but maybe you've explicitly set it to 0 somewhere?
You can add before your call to find() the following:
$this->Question->order = 'Question.created DESC';
Yeah, I couldn't work out how to sort based on the related/associated model, so ended up using the Set::sort() method. Checkout this article for a good explanation.
// This finds all FAQ articles sorted by:
// Category.sortorder, then Category.id, then Faq.displaying_order
$faqs = $this->Faq->find('all', array('order' => 'displaying_order'));
$faqs = Set::sort($faqs, '{n}.Category.id', 'ASC');
$faqs = Set::sort($faqs, '{n}.Category.sortorder', 'ASC');
...And yes, it should probably be a Category->find() but unfortunately the original developer didn't code it that way, and I didn't wanna rework the views.

Categories