Associations not working properly in CakePHP - cakephp

I have the following Models
Shop
_____________________________________
Id Name address city postalcode ...
Category
_______________________________________
Id Name Description shop_id ...
Product
______________________________________
Id Name Description category_id ...
And my Product Model I have defined $belongsTo relation like
public $belongsTo = array(
'Category' => array(
'className' => 'Category',
'conditions' => array('Category.shop_id' => 5)
)
);
What I need is that when I want to add New Product, in the Categories SelectBox should appear only Categories belonging to shop_id = 5 but I am getting all Categories.
Or i have to change the find method?

Yes, you need to add the conditions in the find query as you require it for add product
form.

Related

how to make associations between grand parent & parent & child in cakephp 2

I have 4 tabels
stores
categories
items
item_images
Now i want to make association between them categories have store_id foreign key and items table have category_id and item_images have item_id foreign key.
public $hasMany = array(
'Item' => array(
'className' => 'Item',
'foreignKey' => 'category_id'
));
The above association is for category that find the related items but i want to do associate it with item_images. How i can do it with item_images now?
In Controller i have this query
$storeCategoriesDetails = $this->Category->find("all", array('conditions' => array('Category.storeId' => $id)));
You want to put your association for the item_images on the Item model like you've done for items on the Category model. You can then retrieve the item images along with the items when retrieving categories using contain:-
$storeCategoriesDetails = $this->Category->find('all', array(
'contain' => array('Item' => array('ItemImage'))
'conditions' => array('Category.storeId' => $id)
));
I added this line in my controller function and it's working now.
$this->Category->Behaviors->load('Containable');

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.

CakePHP, don't know how to select category name from table product and category

I am very new in cakephp
I have two table tb_product and tb_category
I want to select like sql below. How can I do it with cakephp?
SQL:
SELECT tb_product.id, tb_product.name, tb_category.name
FROM tb_product
INNER JOIN tb_category
WHERE tb_product.cat_id = tb_category.cat_id"
Thank you all helper!
tb_product:
----------
id
name
===========
tb_category:
-----------
cat_id
name
==========
Thank you in advanced !!!
You can either create a model association in your Cake model for Product to automatically join to Category in a hasOne relationship based on the cat_id foreign key, or you can do it with your find() query as a manual join:
$results = $this->Product->find('all', array(
'joins' => array(
array(
'table' => 'tb_category',
'alias' => 'Category',
'type' => 'INNER',
'conditions' => 'Category.cat_id = Product.cat_id'
)
),
'fields' => array(
'Product.id',
'Product.name',
'Category.name'
)
));
A model association would look something like this:
class Product extends AppModel {
// ...
public $hasOne = array(
'Category' => array('foreignKey' => 'cat_id')
);
}
Then when you query your product model, categories that match should be returned with it:
$results = $this->Product->find('all');

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.

Cakephp: HasOne Relationship

I'm trying to model the following:
A journey has an id, a fromCity and a toCity.
My DB looks like this:
Table journey:
id | fromCity | toCity
1 2 4
2 4 2
Table fromCity:
id | name
2 paris
4 london
I have models defined for the city and for the journey.
In my model file for the journey I want to declare a $hasOne filed in order resolve the fromCity id to the city's name.
I tried to follow the CakePHP tutorial on hasOne (http://book.cakephp.org/view/80/hasOne) but I can't wrap my head around how to resolve two foreign keys.
Can someone please explain to me how a var $hasone = ... has to look for this case?
Edit: The models:
<?php class City extends AppModel { var $name='City';} ?>
<?php class Journey extends AppModel { var $name='Journey'; /*var $hasOne=array(...)*/} ?>
Edit 2:
var $hasOne = array(
'CityFrom' => array(
'className' => 'City',
'conditions' => 'Journey.fromAirport = CityFrom.id',
'dependent' => true,
'foreignKey' => 'id = Journey.fromCity' ),
'CityTo' => array (
'className' => 'City',
'conditions' => 'Journey.toCity = CityTo.id',
'dependent' => true,
'foreignKey' => 'id = Journey.toCity'
)
);
Seems to work for the first entry of the journey table. All other's values are null. I think the problem occours from having two columns with the same name in this query.
As Rin mentioned,you need to use beLongsTo instead of hasOne.Here's a example about Multiple relations to the same model from Cookbook.Hope it helps.
First of all your link leads to 1.2 docs, you probably downloaded 1.3.
In 1.3, you should probably also use belongsTo on FromCity and ToCity Models (but I could be wrong, long time ago I used it :)

Resources