hasOne subtype on CakePHP - cakephp

I have 3 users tables on my DB:
Users
users_twitter
users_web
Users is the parent of both users_twitter and users_web.
And then another table for comments. Both kind of users can comment.
So, when I want to show the comments I need to get information of the author of each comment.
In order to do this I am using the variable $belongsTo on the /Model/Comment.php like this:
public $belongsTo = array(
'users' => array(
'foreignKey' => 'idUser'
)
);
In Controller/CommentController, when i do this:
public function index($idPost) {
$this->Comment->recursive = 0;
return $this->Comment->find('all');
}
I get this kind of array:
[0] => Array
(
[Comment] => Array
(
[id] => 1
[idPost] => 441
[idUser] => 387371023
[comment] => hello word
[created] => 2012-03-01 00:00:00
)
[User] => Array
(
[id] => 1
[username] => myname
)
)
And what I want is to get more info yet, the one of the subtype of Users, depending on the existence of the ID on the subtype tables.
In case it was an id of a user_web table, I would like to get this:
[0] => Array
(
[Comment] => Array
(
[id] => 1
[idPost] => 441
[idUser] => 387371023
[comment] => hello word
[created] => 2012-03-01 00:00:00
)
[User] => Array
(
[id] => 1
[username] => myname
[users_web] => Array
(
[id] => 1
[username] => myName
[password] => XXXXX
[verified] => 1
[created] => 1
)
)
Do you know if that's possible with CakePHP?
As far as I know, with $hasOne I only get one level more, I need two.

In your AppModel add
public $actsAs = array('Containable');
In your index:
public function index($idPost) {
return $this->Comment->find('all', array(
'contain' => array('User.UsersWeb')));
}
Your User model has to be associated with a UsersWeb model to make this work.
See http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html#containing-deeper-associations for a complete description.
Also why do you return the data in the controller index? Are you using requestAction for this? And why don't you use pagination? Do you really want to display hundreds of comments on one page?
See http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html

Related

cakephp linked model not showing in hasmany

I have three Models in cakephp
//Union
public $hasMany = 'Member';
and
//Member
public $hasOne = 'Post';
where as post contains post names
Now the problem is when I use:
$this->Union->findById(1);
in Union controller it shows linked Members but not name of posts
[Union] => Array
(
[id] => 1
[Name] => Dawa vyapar mandal
[created] => 2014-03-31 14:08:12
)
[Member] => Array
(
[0] => Array
(
[id] => 1
[Name] => Ashish
[post_id] => 1
[union_id] => 1
[created] => 2014-03-31 14:11:02
)
[1] => Array
(
[id] => 2
[Name] => Ashu
[post_id] => 1
[union_id] => 1
[created] => 2014-07-01 15:01:15
)
)
)
so how to achieve Post Model inside Member Model?
You have to make the model to be recursive to the level upto which you need data.
In your case, you need the data upto 2nd level, so make recursive as -
$this->Union->recursive = 2;
And after that find your data -
$this->Union->findById(1)

Cakephp-2.3 Form Pre filling doesnt load associated data

I have two models in HABT relation.Doctor and Patience.
I'm trying to create an edit form for Patient inside the Doctors controller.When I'm in Doctor controller I want to get all data of the Patients (not related Patients of Doctor )and its related Doctor. I tried the code below but it returns only Patient not its related data.
$options = array('recursive' => 2,'conditions' => array('Patience.' .$this->Doctor->Patience->primaryKey => $id));
$this->request->data = $this->Doctor->Patience->find('first', $options);
This only getting Patience model data not its related data.
Array ( [Patience] => Array ( [username] => akhil [password] => 93477851451f6583c7efd383e715cb6f8accb804 [id] => 4 [name] => akhil [age] => 33 [address] => w4ewrf [cellno] => 324323 [email] => awad#gmail.com [medicines] => sd [diseases] => sdf [misc] => sdf ) [Doctor] => Array ( ) )

CakePHP same array from the direct and associated model

I have simple model relationship in CakePHP 1.3 with Categories -> Products
Category hasMany Products
There is slight difference between the data arrays which I get in the different controllers. The Product data is in the main product array when getting as associated model in the Categories controller and is separated when getting it in Products.
For Example to get 'Product1'
in Categories - $category['Product'][0]['title']
and in Products - $product[0]['Product']['title']
I would like to use same element for displaying the products. It does not matter which array scheme will be used just to be the same. And where is the right place to make the modification? I can modify those arrays after getting them, but don't think that it is the best option.
When I am in the Categories controller and get a category I get this:
// $this->Category->findById('12');
Array
(
[ProductCategory] => Array
(
[id] => 12
[title] => Category 1
[updated] => 2013-02-24 10:06:15
[created] => 2013-02-24 10:06:15
)
[Product] => Array
(
[0] => Array
(
[id] => 4
[parent_id] => 12
[title] => Product1
[updated] => 2013-02-24 10:17:01
[created] => 2013-02-24 09:12:59
)
[1] => Array
(
[id] => 6
[parent_id] => 12
[title] => Product2
[updated] => 2013-02-24 10:16:54
[created] => 2013-02-24 09:13:53
)
)
And when getting all the products inside the Products controller:
// $this->Product->find('all');
Array
(
[0] => Array
(
[Product] => Array
(
[id] => 10
[parent_id] => 12
[title] => Product1
[updated] => 2013-02-24 10:16:42
[created] => 2013-02-24 09:16:35
)
)
[1] => Array
(
[Product] => Array
(
[id] => 8
[parent_id] => 12
[title] => Product2
[updated] => 2013-02-24 10:16:47
[created] => 2013-02-24 09:15:39
)
)
)
)
One of your finds is a find('all') and the other is a findById() (which uses find('first')).
Both of these return data in a different format, since find('first') knows you only want one item, and find('all') is an unknown set of item(s).
Just use find('all') for both, but set your limit based on whether you need only one or more than one. Then, your data will be returned exactly the same.
Which Controller you retrieve your data from has no effect on the data returned. Which MODEL however, does - so just make sure you're doing your find from the same model.
Eg.
//in your ProductsController
$this->Product->find('all');
//in your CategoriesController
$this->Category->Product->find('all');
// in some other controller
$this->loadModel('Product);
$this->Product->find('all');
PS - BUT it's better if you don't do your "finds" in your Controller - make a method in your Model, and call it from your Controller(s) so instead of $this->Product->find(), it would be $this->Product->getProducts() (or whatever you want to call it). (read more about "fat models, skinny controllers" for reasons/examples...etc).
Dave is right, the difference is the method you're using... Even though you claim that associated data is always merged, your find on the 'Product' model is NOT associated data, so the format WILL always be different.
I've been here for a short while and I've already noticed that Dave knows his stuff. :)
I agree with the fat models/skinny controllers paradigm for clean, efficient code.
If you changed:
<?php
$this->Category->contain(array(
'Product'
));
$this->Category->find('all',
array(
'conditions' => array(
'Category.id' => $id // 12 for your OP.
),
'limit' => 1
)
);
?>
Should give you:
<?php
array
(
[0] => array
(
'Category' => array
(
[id] => 12
[title] => Category 1
[updated] => 2013-02-24 10:06:15
[created] => 2013-02-24 10:06:15
),
'Product' => array
(
[0] => array
(
...
),
[1] => array
(
...
)
)
)
)
?>
Please correct me if I am mistaken, thanks!
Or if you want "Products" to look like:
<?php
'Product' => array
(
[0] => array
(
'Product' => array
(
...
)
)
)
?>
when fetching data from the category model, you would need to fetch the associated data manually, e.g.:
<?php
$this->Category->contain();
$cats = $this->Category->find('all');
foreach ($cats as &$cat) {
$this->Category->Product->contain(); // You have to contain for each find.
$cat['Product'] = $this->Category->Product->find('all',
array(
'conditions' => array(
'Product.category_id' => $cat['Category']['id']
)
)
);
}
?>

hasMany reduced to hasOne in CakePHP

Basically I have following models in CakePHP:
User(id, username)
Photo(id, user_id, path)
I have set up following relation: User hasMany Photo.
On one screen, I would like to list users, and show random photo next to each user. I tried setting up following relation:
User hasOne SamplePhoto (where SamplePhoto is just Photo model)
but when user has two photos for instance, he is listed twice on the list.
basically my question is: can you reduce hasMany relation to hasOne, without adding any fields to table schema presented above? I would like to tell cake - find the first record in the Photo table which matches a certain user_id.
You could also use the Containable behaviour and then set up something like:
$this->User->find(
'all',
array(
'contains' => array(
'Photo' => array(
'order' => 'rand()',
'limit' => 1
)
)
)
);
You should then get something like
Array
(
[User] => Array
(
[id] => 121
[username] => tom
)
[Photo] => Array
(
[0] => Array
(
[id] => 123
[user_id] => 121
[path] => Somewhere
)
)
)
if you do a find like $this->User->read(null,$id), the return will be an array that looks something like:
Array
(
[User] => Array
(
[id] => 121
[username] => tom
)
[Photo] => Array
(
[0] => Array
(
[id] => 123
[user_id] => 121
[path] => Somewhere
)
[1] => Array
(
[id] => 124
[user_id] => 121
[path] => SomeOtherPlace
)
)
)
From this array you can pick the photo however you like, be it the first:
$this->data['Photo'][0]
the last:
$this->data['Photo'][count($this->data['Photo'])]
an explicit record:
$this->data['Photo'][3]
or by some random means:
$this->data['Photo'][$rnd]
Don't make this more complicated than it needs to be. :)
$data = $this->User->Photo->find('first',
array('conditions' => array('Photo.user_id' => $id)));
Gives you a photo in $data['Photo'] with the user attached in $data['User'].
You'd do something like:
$user = $this->User->find('first', array('conditions' =>
array('username'=>$this->data['User']['username'],
'active'=>true) ));

Cakephp paginator sort method with a habtm relation question

Here's an example...
I'm using a table to manage all of my Statuses with the tree behavior. I have a Book model which has a status for it's condition and one for it's availability. So the Book model HABTM Statuses even thought it can only have one condition and one availability.
Is there a way to use the $paginator->sort('Condition', '?') with a HABTM ? I would like to sort the title from the array below.
This is what $this->data is giving me :
[Status] => Array
(
[0] => Array
(
[id] => 2
[parent_id] => 1
[lft] => 2
[rght] => 3
[title] => Mint
[created] => 2010-01-18 16:20:54
[modified] => 2010-01-18 16:20:54
[active] => 1
[BooksStatus] => Array
(
[id] => 7
[book_id] => 3
[status_id] => 2
)
)
)
i would use 2 x belongsTo with different names.
$belongsTo = array(
'BookStatus' => array(
'className' => 'Status',
'foreignKey' => 'book_status_id'
),
'BookCondition' => array(
'className' => 'Status',
'foreignKey' => 'book_condition_id'
)
)
dont know how this would then fit into your tree strut
if that doesnt work then Try $paginator->sort('Condition', 'BooksStatus.status_id');
you will need to make sure the query used to retrieve the above does a join(unBind/bind on the fly)

Resources