CakePHP Containable on Assoicated Model - cakephp

I have a setup where there are Books Users and Logs. Each Log has a Book and a User and I am trying to retrieve a list of all the books regardless of the user, but then also retrieve the Logs associated with that book (recursive is set to 1), but then only retrieving the logs of the current logged in user.
Hopefully that's clear. I tried using Containable and like so:
$this->Book->contain('Log.user_id = 2');
But unfortunately this leaves out Books for which User 2 has no logs for. Am I going about this correctly and I'm just not using containable properly, or am I doing this all wrong.
Any help is appreciated

$this->Book->Behaviors->attach('Containable');
$this->Book->find('all', array(
'contain' => array(
'Log.user_id' => 2
)
)
);

Related

Cakephp4, how to load associated data in an Entity object?

I have a Users table and a Roles table.
A user has one role. So in UsersTable.php:
$this->belongsTo('Roles', [
'foreignKey' => 'role_id',
'joinType' => 'INNER',
]);
Now in User.php (Entity!!) I need the role name of the users role.
But I have only the User Entity in which is no associated data.
I now have:
public function getRole()
{
$q = TableRegistry::getTableLocator()->get('Roles');
$roles = $q->find('list')->toArray();
return $roles[$this->role_id];
}
This works, but TableRegistry is marked obsolete in cake4, and I can't find any other way the make this work. What is the propper way of doing this?
If you want to use both the User and Role data in a controller you can just do for example
$user = $this->Users->get($id, [
'contain' => ['Roles'],
]);
and the resulting object has $user->role defined as a Role entity.
However, I have a feeling you already know this and have another issue that may or may not overlap with a problem I was solving just recently. Check out ndm's answer to my question here and hopefully it helps you! CakePHP 4.1 User entity as authorization identity associated fields
Apologies if that's not your scenario, I just found this current thread while looking for an answer to my problem, and it was definitely the closest to my own issue.

WP_User_Query takes time on huge database for pagination

I have an issue with huge WordPress database to find users. I have about 147000 user into my database and 75000 users have same category. When i try to search users by a category for example "Doctors" then Query fails and page got stuck
I need $total_users from WP_User_Query to make pagination but it seems that query fail.
I also tried this WP_User_Query unable to retrieve all user at once
but page load time is still too much.
$query_args = array(
'role' => 'professional',
'order' => 'DESC',
'orderby' => 'ID',
);
Can anyone there to help me to get this resolve?
here is the link : Site link
Thanks

cakephp: find statement with 'contain'

the following User model function is from MilesJones forum plugin. Can someone tell me on what is the use of 'contain' in the find stmt. I couldn't find any example with contain in the cakephp cookbook. Any helps is appreciated.
public function getProfile($id) {
return $this->find('first', array(
'conditions' => array('User.id' => $id),
'contain' => array(
'Access' => array('AccessLevel'),
'Moderator' => array('ForumCategory')
)
));
}
By default when a find statement executes cake pulls all the data from the model on which the find function is executing plus all the data from the models that are associated with the model. Most of the time you don't need that extra data, Cake has containable behaviour for exactly that purpose. You can specify which associated model's data you want in your result.
In the above example find statement will fetch the first record from the User model plus associated data from Access and Moderator models.
Here is the link from cakephp book http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
Here is cakephp documentation about contain

Containable behavior does not return from depth 3 - CakePHP

I use CakePHP 1.2.6 and have the following relations:
Showcase HABTM User belongsTo Galleryitem hasOne Image
I try to get all the data related to a Showcase, and therefor also all its users with their Galleryitem -> Image. I use the following query:
$showcase = $this->Showcase->find('first',
array('conditions'=>array('Showcase.id'=>$id),
'contain'=> array(
'User' => array(
'Galleryitem' => array(
'Image'
)
)
)
)
);
This does returns and empty array of Galleryitem and thus no Image records at all.
If I try the following:
$showcase = $this->Showcase->User->find('first',
array(
'contain'=> array(
'Galleryitem' => array(
'Image'
)
)
)
);
I do get some data here about Image. So it seems the depth plays a role here.
Other factors that came to mind were the belongsTo relation between User and Galleryitem.
What causes my query not to return data from a depth of 3?
Update
The set of Showcase relations is in my project much more branched than I explain above. All the other branches properply show up. So I guess it has to do with specific relations in this branch, the User belongsTo Galleryitem.
Strange still, because the other branches contain this very same set of Galleryitem hasOne Image relation.
I usually go for the dot syntax (which I can't see in the book):
$this->Showcase->contain('User','User.GalleryItem','User.GalleryItem.Image');
$showcase = $this->Showcase->User->find('first');
although I'm struggling to find a three-deep example in any of my code.

Select from a table where associated table fieldname = 'x' in Cake PHP

In CakePHP, I have two tables, Countries & Networks. They have a HABTM relationship and are joined by countries_networks.
I'm trying to get all countries from the countries table where the 'name' field in Networks = 'o2'
I've realised I can't do this using a basic find(), so I've been experimenting with the containable behaviour. I have managed to restrict the returned data, but it looks as though 'containable' doesn't exactly work as I want. Heres my code:
$countries = $this->Country->find('all', array('contain' => array(
'Network' => array(
'conditions' => array('Network.name =' => "o2"),
)
)));
This query however returns ALL countries, and the Network.name if its 'o2'. What I really need to do is return ONLY the countries that have a Network.name of 'o2', and no others.
Can anyone help? Thanks.
"=' =>"
What is it? there is no needed to use "=" symbol after "Network.name"
Your query returns you exactly what your ask.
Try to select from Network.
$countries = $this->Country->Network->find('first', array(
'conditions' => array('Network.name' => "o2"),
'contain' => array('Country')
));
$countries = $countries['Country'];
You should be able to do something like this:
$this->Country->hasAndBelongsToMany['Network']['conditions'] = array('Network.name'=>'o2');
$myCountries = $this->Country->find('all');
I haven't tested this, but it should get you pretty close to where you need to be.
Also, bear in mind that changing the hasAndBelongsToMany array like this will affect all subsequent queries against that model during the current page load (but it will be reset the next time the model is instantiated).
I'm on my way out the door, so sorry for the brief explanation.
I think what you want is a HABTM relationship, but to be able to filter based on the associated model's data. To do this, check out the "Containable" behavior in Cake's manual. Pretty sure that's what you're after.

Resources