CakePHP Containable Behavior Not Working As Expected - cakephp

I have the following database tables:
products
********
id
title
artist_id
artists
*******
id
profile
rating
person_id
people
******
id
full_name
With the following model associations:
Product Model
*************
public $belongsTo = array(
'Artist' => array(
'className' => 'Artist'
'foreignKey' => 'artist_id'
)
);
Artist Model
************
public $belongsTo = array(
'Person' => array(
'className' => 'Person'
'foreignKey' => 'person_id'
)
);
public $hasMany = array(
'Product' => array(
'className' => 'Product'
'foreignKey' => 'product_id'
)
);
Person Model
************
public $hasOne = array(
'Artist' => array(
'className' => 'Artist'
'foreignKey' => 'person_id'
)
);
I have set each of the three models to use the Containable behaviour using:
public $actsAs = array('Containable');
When I use the following to get the product details along with the artist's name:
$this->Product->find('first', array('conditions' => array('Product.id' => $id), 'contain' => 'Person.full_name'))
I get the warning:
Model "Product" is not associated with model "Person"
And I only get the product details ie no artist's name. Why is this happening?

This is happening because you are calling the find operation on the Product model, and Person is not directly related to Product, but indirectly, by its relation to Artist. In this case, your contain array should be something like the following:
$this->Product->find('first', array(
'conditions' => array('Product.id' => $id),
'contain' => array('Artist' => 'Person.full_name')
));
This will return a result like
array(
[Product] => array(...),
[Artist] => array(
[Person] => array(
'full_name' => '...'
)
)
)

Related

In cakephp 2.x How to get third table data using join table?

'books
id,name
categories
id,name
books_categories
id,book_id,category_id'
' App::uses('AppModel', 'Model');
class Book extends AppModel {
public $useTable = 'books';
public $belongsTo = array(
'Agerange' => array(
'className' => 'Agerange',
'foreignKey' => 'agerange_id'
),
'Publication' => array(
'className' => 'Publication',
'foreignKey' => 'publication_id'
),
);
public $hasMany = array(
'Category' => array(
'className' => 'Book_Category',
'foreignKey' => 'book_id',
'joinTable' => 'books_categories',
'fields' => array('Category_id',),
)
);
}
<?php
App::uses('AppModel', 'Model');
class Book_Category extends AppModel {
public $useTable = 'books_categories';
public $belongsTo = array(
'Book' => array(
'className' => 'Book',
'foreignKey' => 'book_id'
),
'Category' => array(
'className' => 'Category',
'foreignKey' => 'category_id'
)
);
}
in BooksController ......
$this->paginate = array(
'contains'=>array('Book','Publication','Agerange','Book_Category'),
'order' => 'Book.id ASC');
$books = $this->paginate('Book');
enter code here
{
Book: {id: "111", title: "ABC", subtitle: "English Today Primer", author: "Pruthviraj",…},…}
Agerange:{id: "31", from: "0", to: "2", status: true}
Book:{id: "111", title: "ABC", subtitle: "English Today Primer", author: "Pruthviraj",…}
Category:[{Category_id: "5", book_id: "111"}]
}'
Output Now I am getting only category_id:5 it's okay but I want name field also of category so How can I get name field of category using Book and Book_Category?
Modify your contain array by adding Categoy to Book_Category
$this->paginate = array(
'contains' => array(
'Book',
'Publication',
'Agerange',
'Book_Category' => 'Category'
),
'order' => 'Book.id ASC'
);
OR Use hasAndBelongsToMany association, it's much simpler
Start by deleting the Book_Category
Then change the book Model
class Book extends AppModel {
public $useTable = 'books';
public $belongsTo = array(
'Agerange' => array(
'className' => 'Agerange',
'foreignKey' => 'agerange_id'
),
'Publication' => array(
'className' => 'Publication',
'foreignKey' => 'publication_id'
),
);
public $hasAndBelongsToMany = array(
'Category' => array(
'className' => 'Category',
'joinTable' => 'books_categories',
'foreignKey' => 'book_id',
'associationForeignKey' => 'category_id'
)
);
}
Then, in your controller change the pagination setting like this
$this->paginate = array(
'contains' => array(
'Publication',
'Agerange',
'Category'
),
'order' => 'Book.id ASC'
);
#link Cakephp HABTM association

Cakephp relationship issue

Company class
public $hasMany = array(
'Person' => array(
'className' => 'Lasso.Person',
'foreignKey' => 'company_id',
)
Person class
public $belongsTo = array(
'Company' => array(
'className' => 'Lasso.Company',
'foreignKey' => 'id',
'conditions' => '',
'fields' => 'company',
'order' => ''
)
);
How do I get my company_id field within my People views to be the display name of Company rather than just the value of company_id?
public $displayField = 'companyName';
In your Person class you can write that:
public $belongTo = array(
'Company' => array(
'className' => 'Lesso.Company',
'foreignKey' => 'company_id', // Here use company_id instead of id
.....
)
);
and also you need to put company_id field in persons table.

CakePHP multilevel models

I've been trying to figure out how to save multi-level models in CakePHP for some time now, but can't seem to find a solution.
I have three models. Survey, Question, and Choice.
Survey hasMany Question - so Question belongsTo Survey
Question hasMany Choice - so Choice belongsTo Question
The Question is linked to the Survey through its survey_id key.
The Choice, on the otherhand, is linked to the Question through its question_id key. (not directly linked to Survey though)
The form is created through $this->Form->create('Survey').
I want the application to save a survey with its corresponding questions, AND each question having their corresponding choice(s).
The problem is, only the Survey and Question models are saved. Choice gets discarded.
I'm using $this->saveAssociated($this->request->data, array( 'deep' => true) )
I will be updating my post to show the $_POST data.
Thanks,
XTN
Before saving data, you have to ensure that data should be in this format
in your controller:
$data = array('Survey' => array('id' => 1,'name' => 'test'),
'Question' => array(
array('id' => 1,'question' => 'test1','survey_id' => 1,
'Choice' => array(
array('id' => 1,'question_id' => 1,'choice' => 1),
array('id' => 2,'question_id' => 1,'choice' => 2)
)
),
array('id' => 2,'question' => 'question2','survey_id' => 1,
'Choice' => array(
array('id' => 3,'question_id' => 2,'choice' => 'sd'),
array('id' => 4,'question_id' => 2,'choice' => 'we')
)
)
)
);
$this->Survey->create();
$this->Survey->saveAssociated($data,array('deep'=>true));
Survey Model :
public $hasMany = array(
'Question' => array(
'className' => 'Question',
'foreignKey' => 'survey_id',
'dependent' => false,
)
);
Question Model :
public $belongsTo = array(
'Survey' => array(
'className' => 'Survey',
'foreignKey' => 'survey_id',
)
);
public $hasMany = array(
'Choice' => array(
'className' => 'Choice',
'foreignKey' => 'question_id',
'dependent' => false,
)
);
Choice Model :
public $belongsTo = array(
'Question' => array(
'className' => 'Question',
'foreignKey' => 'question_id',
)
);
I think it will work if found any problem please notify
Your models should looks like:
Survey Model: Survey.php
class Survey extends AppModel {
public $name = 'Survey';
public $hasMany = array('Questions' => array(
'className' => 'Question',
'foreignKey' => 'survey_id'
)
);
}
Question Model: Question.php
class Question extends AppModel{
public $name = 'Question';
public $belongsTo = array(
'Survey' => array('className' => 'Survey', 'foreignKey' => 'survey_id'),
'Answer' => array('className' => 'Choice', 'foreignKey' => 'answer_id'));
public $hasMany = array('Choices' => array('className' => 'Choice',
'foreignKey' => 'question_id'));
}
Choice Model: Choice.php
class Choice extends AppModel
{
public $name = 'Choice';
public $belongsTo = array('Question' => array('className' => 'Question',
'foreignKey' => 'question_id'));
}
Kindly ask if it not worked for you.

cakePHP and more than one type of relation between same tables

I have two tables - users and stores.
there is a third table - stores_users that should manage the relations (HABTM).
However, a store have a user_id column within it that refers to the owner of the store, the person who created the store record (belongsTo).
This user can grant management privileges to other users..
Should i expect problems because of this relations?
This shouldn't be a problem. You can give aliases to your associations as such
class Store extends AppModel {
public $belongsTo = array(
'Creator' => array(
'className' => 'User',
'foreignKey' => 'user_id',
),
);
public $hasAndBelongsToMany = array(
'Manager' => array(
'className' => 'User',
'joinTable' => 'stores_users',
'foreignKey' => 'store_id',
'associationForeignKey' => 'user_id',
),
);
}
Now find operations on Store will return data like
array(
[Store] => array(...)
[Creator] => array(...)
[Manager] => array(
0 => array(...)
1 => array(...)
)
)

How create belonge assocation in models in cakephp

I use Cakephp framework, and I have problem with my association.
How create belong to association in models in cake php.
When I use belongto and hasMany in my model.
Can I find sample model to view this example?
Simple belongsTo association:
<?php
class Profile extends AppModel {
var $name = 'Profile';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
}
?>
Simple hasMany association:
<?php
class User extends AppModel {
var $name = 'User';
var $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'user_id',
'conditions' => array('Comment.status' => '1'),
'order' => 'Comment.created DESC',
'limit' => '5',
'dependent'=> true
)
);
}
?>
More specific information about associations is in the CakePHP Book.
User has Many Photos
Photos belongs to User
In User Model :
var $hasMany = array(
'Photo' => array(
'className' => 'Photo',
'foreignKey' => 'user_id'
);
In Photo Model :
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'PhotoAlbum' => array(
'className' => 'PhotoAlbum',
'foreignKey' => 'photo_album_id',
'conditions' => '',
'fields' => '',
'order' => '',
))

Resources