Use of containble behaviour in cakephp? - cakephp

I am having three tables category, subcategory and subsubcategory.
Now category table contain fields as id,name
subcategory table contain fields as id, category_id and name
and subsubcategory table contain fields as id, subcategory_id and name
now subsubcategory view/index showing columns as name,subcategory_name
but i want to display category_name also in my subsubcategory index page
I have used containable behaviour to associate two tables as category and subsubcategory
Here is my code:--
subsubcategoriescontroller:
$contain =array(
'Subcategory'=> array(
'Category' =>array(
'fields' => array('id', 'name')
)
));
$this->Subsubcategory->find('all',array('contain' => $contain));
appmodel:
public $actsAs = array("Containable");
still my subsubcategory page not showing expected results.
what will i do?

Have you got the correct relationships set up in your models?
class Category extends AppModel {
public $hasMany = array('Subcategory');
}
class Subcategory extends AppModel {
public $belongsTo = array('Category');
public $hasMany = array('Subsubcategory');
}
class Subsubcategory extends AppModel {
public $belongsTo = array('Subcategory');
}

Related

ID fields not auto filled for table with multiple associations (CakePHP)

I'm working on a customer database. For larger business customers the relationships get a little complicated. It's almost working but one ID field is not being automatically filled.
I have 3 models, Customer, CustomerAddress and CustomerContact. A customer can have many addresses, and many contacts, but the contacts are also based at one address so an address has many contacts.
Database:
customer has id
customer_addresses has customer_id
customer_contacts has customer_id, customer_address_id
Models:
class Customer extends AppModel {
public $hasMany = array(
'CustomerContact',
'CustomerAddress',
);
....
}
class CustomerAddress extends AppModel {
public $belongsTo = array(
'Customer',
);
public $hasMany = array(
'CustomerContact',
);
....
}
class CustomerContact extends AppModel {
public $belongsTo = array(
'Customer',
'CustomerAddress',
);
....
}
I have one form that adds fields for all 3 models, and I'm calling '$this->Customer-saveAll' in the controller. It saves everything just fine, but doesn't fill in the 'customer_address_id' field in the 'customer_contacts' table.
I've tried changing the relationship in contacts to belongs to address, I've tried renaming the field to customer_addresses_id (made it pleural) just incase it was a quirk with Cake's internal naming, but it still doesn't fill the address ID field.
My workaround is to update the contact after the saveAll command in the controller, since the address has it's id set at this point:
$this->CustomerContact->saveField( 'customer_address_id', $this->CustomerAddress->id);
This is my first project with Cake and it seems really good at doing this stuff by itself, so I'm wondering if I've got something wrong somewhere.
TIA!
EDIT:
After going through the associations again, I changed the association on CustomerContact from hasOne to belongsTo CustomerAddress because I realised I got it wrong.
I tried 2 different ways of submitting the data:
'Customer' => .... data ....
'CustomerAddress' => array(
(int) 0 => array(
'CustomerContact' => array(
(int) 0 => array(
.... data ....
)
),
.... data ....
)
)
This sets the customer_address_id field on the contact, but not customer_id.
'Customer' => .... data ....
'CustomerAddress' => array(
(int) 0 => array(
.... data ....
)
),
'CustomerContact' => array(
(int) 0 => array(
.... data ....
)
)
This sets the customer_id but not the customer_address_id.

find() method in CakePHP not working, tried searching around and not resolve.

I'm just learning cakePHP. I have a CATEGORIES table and a POSTS table. A post have one category. So there is a category_id foreign key in POSTS table.
In my Model, I have Category.php with this code.
<?php
App::uses('AppModel', 'Model');
class Category extends AppModel {
// A hasMany association will allow us to fetch a category’s posts when we fetch a Category record.
public $primaryKey = 'category_id';
public $hasOne = 'Post';
}
?>
In my Post.php I have something like this.
class Post extends AppModel {
public $hasMany = array('Photo');
public $primaryKey = 'post_id';
public $belongsTo = array('User'); // 'Category',
public $relatedImages;
public $belongsTo = 'Category';
Now in my PostController.php I have
$allCategories = $this->Category->find('list');
$this->set('allCategories', $allCategories);
The objective is to retrieve all the category from the categories table and show it in my form.
However I encountered the error of Call to a member function find() on a non-object
I'm not sure what is happening here. How can I retrieve this?
many thanks!
Use
$this->Post->Category->find('list');
As you have to goto Category table Thru posts as u r in posts controller Now currently.
In your post model try to declare the Category as below
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
'Category' => array(
'className' => 'Category',
'foreignKey' => 'category_id'
)
);

find('all') doing unexpected (unwanted) SQL join in CakePHP

I've set up two models: city, and country. Below is how I've defined them:
class City extends AppModel { // for "cities" table
public $hasOne = 'Country';
}
class Country extends AppModel { // for "countries" table
public $hasMany = array(
'City' => array(
'className' => 'City'
)
);
}
and in my controller:
public function getCities() {
$this->loadModel('City');
$cities = $this->City->find('all');
}
but it's giving me this error:
Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Country.city_id' in 'on clause'
SQL Query: SELECT `City`.`id`, `City`.`country_id`, `City`.`name`, `City`.`latitude`, `City`.`longitude`, `City`.`time_zone`, `City`.`dma_id`, `City`.`code`, `City`.`created`, `City`.`modified`, `Country`.`id`, `Country`.`country_id`, `Country`.`name`, `Country`.`code`, `Country`.`created`, `Country`.`modified` FROM `rtynbiz_ls`.`cities` AS `City` LEFT JOIN `rtynbiz_ls`.`countries` AS `Country` ON (`Country`.`city_id` = `City`.`id`) WHERE 1 = 1
Notice: If you want to customize this error message, create app/View/Errors/pdo_error.ctp
I can't understand why it's trying to do a join with Country table. I only want to fetch cities. How do I stop this from happening? And, why is it trying to make an association using Country.city_id (which doesn't exists) Also, have I named my classes and tables correctly? Thanks
Following lines of code are making relationship so JOIN is present there. To remove the JOIN from the query just replace :
class City extends AppModel { // for "cities" table
public $hasOne = 'Country';
}
class Country extends AppModel { // for "countries" table
public $hasMany = array(
'City' => array(
'className' => 'City'
)
);
}
With:
class City extends AppModel { }// for "cities" table
class Country extends AppModel {} // for "countries" table
You can make relationships in the tables easily by following this
By default, CakePHP will automatically try to pull additional model's data. To stop that from being the default, in your AppModel, set this variable:
public $recursive = -1;
I would also suggest adding Containable behavior, so your app model looks like this:
<?php
class AppModel extends Model {
public $actsAs = array('Containable');
public $recursive = -1;
}
Read more about "recursive" and "Containable Behavior" at the CakePHP book.
If you don't want to do join when you want to retrieve your data make it recursive -1
public function get_cities() {
$this->loadModel('City');
$this->City->recursive=-1;
$cities = $this->City->find('all');
//or
$cities = $this->City->find('all', array('recursive'=>-1));
}
any way this would be your model:
class Country extends AppModel {
public $hasMany = array(
'City' => array(
'className' => 'City',
'foreignKey' => 'country_id',
'dependent' => false,
),
);
}
class City extends AppModel {
public $belongsTo = array(
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id',
)
);
}
make sure you don't have messy code on these 2 models

CakePHP Empty BelongsTo and hasMany relations array

I've been working this issue this whole weekend, and I can't seem to find a solution.
I have three models:
User Class:
class User extends AppModel {
var $hasMany = array('Like','Event');
}
Like Class:
class Like extends AppModel {
var $belongsTo = array ('Event','User');
}
Event Class:
class Event extends AppModel {
var $belongsTo = 'User';
var $hasMany = 'Like';
}
This is my find statement:
$this->set('likes', $this->Like->find('all', array(
'contain'=>array(
'User','Event')));
My like table is:
id(int 10)
user_id(int 10)
event_id(int 10)
My user table has:
id(int 10)
plus all regular user related column
My event table has:
id(int 10)
user_id (int 10)
Plus all other event related columns
NOW THE PROBLEM:
[0]
[Like]
id:77
user_id:30
event_id:130
[Event]
id(null)
user_id(null)
title(null)
date(null)
etc......
[User]
id:30
email:********#gmail.com
password:**********************
created:2013-07-07 23:45:13
modified:2013-07-07 23:45:13
etc.........
This is my query:
SELECT `Like`.`id`, `Like`.`user_id`, `Like`.`event_id`, `Event`.`id`, `Event`.`user_id`, `Event`.`title`, `Event`.`date`, `Event`.`price_advance`, `Event`.`price_door`, `Event`.`price_vip`, `Event`.`created`, `Event`.`bcg_img`, `Event`.`event_info`, `Event`.`event`, `Event`.`phone1`, `Event`.`phone2`, `Event`.`promoter`, `User`.`id`, `User`.`email`, `User`.`password`, `User`.`created`, `User`.`modified`, `User`.`isAdmin`, `User`.`verified`, `User`.`full_name` FROM `quadb`.`likes` AS `Like` LEFT JOIN `quadb`.`eventss` AS `Event` ON (`Like`.`event_id` = `Event`.`id`) LEFT JOIN `quadb`.`users` AS `User` ON (`Like`.`user_id` = `User`.`id`) WHERE 1 = 1
I don't know why my event array is coming back null. Any help will be greatly appreciated.
EDIT
After changing the find code to add an order clause, it magically works, I don't know exactly why:
$this->set('likes', $this->Like->find('all', array(
'order' => array('Like.created' => 'DESC'),
'contain'=>array('Quad'))));
class Event extends AppModel {
var $belongsTo = array('User');
var $hasMany = array('Like');
}

Translate from two tables with i18n database tables (cakePHP)

I have four tables:
gallery_categories table:
id (int);
title (varchar);
gallery_pictures table:
id (int);
title (varchar);
gallery_categories_gallery_pictures table to joint above tables:
gallery_category_id (int);
gallery_picture_id (int);
and the i18n table:
id (int)
locale (varchar)
model (varchar)
foreign_key (int)
field (varchar)
content (text)
I tried:
$this->GalleryCategory->bindModel(array(
'hasOne' => array(
'GalleryCategoriesGalleryPicture',
'GalleryPicture' => array(
'className' => 'GalleryPicture',
'foreignKey' => false,
'conditions' => array('GalleryPicture.id = GalleryCategoriesGalleryPicture.gallery_picture_id')
))));
$galleryCategories = $this->GalleryCategory->find('all', array(
'fields' => array('GalleryCategory.*','GalleryPicture.*')
));
But all time it returns with the right Category table fields and wrong Pictures table fields...
The Category fields contain the right translated words.
The Pictrures fields contain the wrong translated words.
The Models:
class GalleryCategory extends AppModel
{
public $tablePrefix = 'ef_';
var $name = 'GalleryCategory';
public $actsAs = array('Translate' => array(
'title' => 'titleTranslation'
)
);
}
class GalleryPicture extends AppModel
{
public $tablePrefix = 'ef_';
var $name = 'GalleryPicture';
public $actsAs = array('Translate' => array(
'title' => 'titleTranslation'
)
);
}
How could I get the right translated words from both table?
ps: The translation with "i18n Database Tables" works right with only one table (exp: only GalleryCategory or only GalleryPicture).
Translation doesn't work with associated models, see the last couple of lines here:
Note that only fields of the model you are directly doing find on will be translated. Models attached via associations won’t be translated because triggering callbacks on associated models is currently not supported.
Update
It might be tricky, as you have a HABTM relationship - previously I have worked around this by adding a second data retrieval to the afterFind callback of a model, but this was with a simpler association. Hope this helps.

Resources