Update record without automatic joins to respective belongsTo associations

208 Views Asked by At

I want to update a record for a specific condition in only items table but when I checked the query, it automatically add joins with it's associated tables. For examples:

$this->Item->updateAll(
    array('Item.alg_update' => 0),
    array('Item.id' => 123456),
);

it executes following query:

UPDATE `DB`.`items` AS `Item` LEFT JOIN   
`DB`.`item_favorites` AS `ItemFavorite` ON  
(`ItemFavorite`.`item_id` = `Item`.`id`) LEFT JOIN  
`DB`.`categories` AS `Category` ON (`Item`.`category_id` =  
`Category`.`id`) LEFT JOIN `DB`.`subcategories` AS `Subcategory` ON  
(`Item`.`subcategory_id` = `Subcategory`.`id`) LEFT JOIN  
`DB`.`item_descriptions` AS `ItemDescriptions` ON  
(`Item`.`description_id` = `ItemDescriptions`.`id`) SET  
`Item`.`alg_update` = 0  WHERE `Item`.`id` = 123456
1

There are 1 best solutions below

3
arilia On

From the manual (last paragraph

By default, updateAll() will automatically join any belongsTo association for databases that support joins. To prevent this, temporarily unbind the associations.

so you have to do

$this->Item->unbindModel(
    array('belongsTo' => array('Subcategory'))
);

$this->Item->updateAll(
    array('Item.alg_update' => 0),
    array('Item.id' => 123456),
);