I have two table,
categories hasMany products
id
name
active
products belongsTo categories
id
name
category_id
active
When I am editing the categories, in the same time I am also displaying the Products related to the category so that I can update/modify products related to the category.
Issue:- When I add more products for the category that works fine but when I remove some products from the category,the removed product does not get deleted from the database. So I want to know that This functionality is supported by CakePHP or not. If yes please help me to find where I am going wrong.
Here is the save code:-
$categoryProducts = $this->Categories->get(1, [
'contain' => 'Products'
]);
if($this->request->is['post', 'put']){
$entity = $this->Categories->patchEntity($categoryProducts, $this->request->data);
$this->Categories->save($entity);
}
When you set up your hasMany relationship, add 'saveStrategy' => 'replace'. See the hasMany section of the manual for details.
Related
I have 3 tables in database:
Cakephp 3.
Cars{id, type_id,....}
Types{id, name,....}
Images{id, type_name,image_url,....}
And in model cars i would like to set relation to images, actually model associations.
I need to fetch image_url from images, with value type_id wich is the same in table Cars and in table Images. It means i can forget table Types.
Now i am fetching Images with join query from controller, but i would like to associate those models.
Query in controller looks like:
$query->join(['table' = 'Images',
'alias' => 'i',
'conditions' => 'Cars.type_id = i.type_id'
]);
This is relation type: 1 car can have 1 or more images, and 1 image can belong to 1 or many cars. It means M:M.
It should be relation belongsToMany.
I try to set diferent ralations option, from hasMany to belongsToMany it does not work.
Is it possible to do this at all, or it maybe isn't because one table does not contain primary key of another, and i am forced to use join query in controller?
Thany you for advice.
added contain with "recursion" in query:
$query->contain(['Cars','Types' => ['Images']]);
and i get what i needed.
best regards
I am new to cakePhp. I have two models name Category and SubCategory in CakePHP3. I want to select the object of Category model where id = 2 and related models of SubCategory. Please help me to get the data.
Use associations and containments, it's well explained in the manual.
http://book.cakephp.org/3.0/en/orm/associations.html
http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#retrieving-associated-data
Once you've setup the associations, you can simply query for the category and have the associated sub categories included automatically.
$query = TableRegistry::get('Categories')
->find()
->contain(['SubCategories'])
->where([
'id' => 2
]);
debug($query->toArray());
Press Story belongsTo Company
Company belongsTo Sector
Is there a way to display a list of the Press Stories, having the name of the company and the name of the sector?
The array returned by cake when paginating Press Stories contains Company and Press information but no additional information about the Sector.
use recursive => '2' in find condition.
See this link.
Ex: $this->Model->find('all', array('conditions' => array(....),
'recursive' => '2'));
I've got two models Category and Item. Category can have many items and item can be in many categories so relation between them is HABTM.
My problem is that a category can be deleted even if items are in it.
I have a foreign key in the database table categories_items that RESTRICTS deletion, but it doesn't help.
What should I do to prevent category from being deleted if items are in it?
You need to overwrite the actual "delete" method in your CategoriesController to verify that there are no Items in the category before it is deleted.
Something like...
$c = $this->Category->findById($id);
$rels = $this->CategoriesItem->find('count', array('conditions' => array('CategoriesItem.category_id' => $id)));
if(count($rels) > 0) $this->Session->setFlash("NO WAY JOSE");
else $this->Category->delete($id);
suppose you have those model relations:
Offer HABTM Category
How would you build the find conditions to find all the categories that have at least one offer. In the same time the Offer should be Offer.enabled => 1. So find all the categories with at least one enabled offer. The conditions to check in the offer model are several, but once I can check for the enabled, I think I will be able to check for any other field.
I could bind the habtm model to Category, but in this case I can't check the Offer.enabled condition. Maybe somehow using containable?
The sql query would be :
SELECT DISTINCT Category.nome from categories as Category
LEFT JOIN categories_offers AS CategoriesOffer
ON (CategoriesOffer.category_id = Category.id)
LEFT JOIN offers as Offer ON (CategoriesOffer.offer_id = Offer.id)
WHERE Offer.enabled = 1
Thank you
Use the conditions key in your relationship definition to set the relationship up only if Offer.enabled = 1. Taken from the book. eg:
class Category extends Model {
public $hasAndBelongsToMany = array(
'Offer' => array(
'conditions' => array( 'Offer.enabled' => 1 )
)
);
}