Cakephp relationship issue - cakephp

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.

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 belongsTo custom condition not working

// in Person
public $hasMany = array(
'PersonRecommendation' => array(
'className' => 'PersonRecommendation',
'foreignKey' => false,
'dependent' => true,
),
)
// in PersonRecommendation
public $belongsTo = array(
'Person' => array(
'className' => 'Person',
'foreignKey' => false,
'conditions' => array('Person.email = PersonRecommendation.email'),
),
);
When I query these out with find() the Person returns ALL recommendations. Not just those associated by email. I've tried a variety of different things, and I'm stumped. Can someone help?
Using cake 2.4.5.
You need to change the Person model
public $hasMany = array(
'PersonRecommendation' => array(
'className' => 'PersonRecommendation',
'foreignKey' => false,
'dependent' => true,
'conditions' => array('Person.email = PersonRecommendation.email')
)
)
Try the following:
'conditions' => array($Person->email => $PersonRecommendation->email)

Many-to-many relationships issue in CakePHP model

I am having issues getting all the data from a relationship. Here are my tables:
**Regions**
id(pk)
regions_name
**Areas**
id(pk)
areas_name
regions_id (fk)
**carDealer**
id(pk)
dealer_name
areas_id(fk)
I would think that with this design, I should be able to query Regions like this:
$this->Region->recursive = 5;
$getRegions = $this->Region->find('all');
And get an array of all Regions, which would have a sub array of Areas, which would have a sub array of carDealers.
My code, only brings back the Regions and Areas array but not carDealers. Here is some relevant code to my models:
Regions Table
<?php
App::uses('AppModel', 'Model');
class Region extends AppModel {
public $validate = array(
);
public $belongsTo = array(
);
public $hasMany = array(
'Areas' => array(
'className' => 'Areas',
'foreignKey' => 'regions_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
}
Areas Table:
<?php
App::uses('AppModel', 'Model');
class Area extends AppModel {
public $validate = array(
);
public $belongsTo = array(
'Regions' => array(
'className' => 'Regions',
'foreignKey' => 'regions_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
public $hasMany = array(
'carDealer' => array(
'className' => 'carDealer',
'foreignKey' => 'areas_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
}
Car Dealer Table:
<?php
App::uses('AppModel', 'Model');
class carDealer extends AppModel {
public $validate = array(
);
public $belongsTo = array(
'Areas' => array(
'className' => 'Areas',
'foreignKey' => 'areas_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
public $hasMany = array(
);
}
Am I missing something in my models relationships?
thanks
jason
Foreign keys in hasMany, belongsTo or hasOne relationships are recognized by default as the (singular) name of the related table followed by _id.
Go through http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions
So your foreign key shoud be area_id , region_id
Need to change recursive level
$this->Region->recursive = 1;
$getRegions = $this->Region->find('all');
Need to modify className of Model relationship
It should be 'Area', 'Region','CarDealer'.
Need to remove unnecessary variable or array options
Region Model
<?php
App::uses('AppModel', 'Model');
class Region extends AppModel {
public $hasMany = array(
'Area' => array(
'className' => 'Area',
'foreignKey' => 'region_id'
),
);
}
Area Model:
<?php
App::uses('AppModel', 'Model');
class Area extends AppModel {
public $belongsTo = array(
'Region' => array(
'className' => 'Region',
'foreignKey' => 'region_id'
),
);
public $hasMany = array(
'CarDealer' => array(
'className' => 'CarDealer',
'foreignKey' => 'area_id'
),
);
}
CarDealer Model:
<?php
App::uses('AppModel', 'Model');
class CarDealer extends AppModel {
public $belongsTo = array(
'Area' => array(
'className' => 'Area',
'foreignKey' => 'area_id'
),
);
}

CakePHP Containable Behavior Not Working As Expected

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' => '...'
)
)
)

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