我对CakePHP 2.x使用softdelete Utils Plugin行为
我有以下4个表
并考虑跟随彼此之间的关系,
1。对于客户
客户拥有多个客户地址
客户有很多客户联系
客户有很多客户报告
public $hasMany = array(
'CustomerAddress' => array(
'className' => 'CustomerAddress',
'foreignKey' => 'customer_id',
'dependent' => true,
),
'CustomerContact' => array(
'className' => 'CustomerContact',
'foreignKey' => 'customer_id',
'dependent' => true,
),
'CustomerReport' => array(
'className' => 'CustomerReport',
'foreignKey' => 'customer_id',
'dependent' => true,
)
);
2。对于customer_addresses
CustomerAddress belongsTo Customer
CustomerAddress belongsTo State
CustomerAddress hasOne CustomerContact
CustomerAddress hasOne CustomerReport
public $belongsTo = array(
'Customer' => array(
'className' => 'Customer',
'foreignKey' => 'customer_id',
'dependent' => false,
),
'State' => array(
'className' => 'State',
'foreignKey' => 'state_id',
)
);
public $hasOne = array(
'CustomerContact' => array(
'className' => 'CustomerContact',
'foreignKey' => 'customer_address_id',
'dependent' => true,
),
'CustomerReport' => array(
'className' => 'CustomerReport',
'foreignKey' => 'customer_address_id',
'dependent' => true,
)
);
第3。对于customer_contacts
CustomerContact belongsTo Customer
CustomerContact belongsTo CustomerAddress
public $belongsTo = array(
'Customer' => array(
'className' => 'Customer',
'foreignKey' => 'customer_id',
'dependent' => false,
),
'CustomerAddress' => array(
'className' => 'CustomerAddress',
'foreignKey' => 'customer_address_id',
'dependent' => true,
)
);
4。 for customer_reports
CustomerReport属于客户
CustomerReport belongsTo CustomerAddress
public $belongsTo = array(
'Customer' => array(
'className' => 'Customer',
'foreignKey' => 'customer_id',
'dependent' => false,
),
'CustomerAddress' => array(
'className' => 'CustomerAddress',
'foreignKey' => 'customer_address_id',
'dependent' => true,
)
);
我已将下面的代码放在bootstrap中。
CakePlugin::load('Utils');
我还在以上所有模型中添加了以下actAs
public $actsAs = array('Containable','Utils.SoftDelete');
我已将下面的代码放在appModel中。
// for softdelete, checking exist id
public function exists($id = null) {
if ($this->Behaviors->attached('SoftDelete')) {
return $this->existsAndNotDeleted($id);
} else {
return parent::exists($id);
}
}
// for softdelete, overrite delete method
public function delete($id = null, $cascade = true) {
$result = parent::delete($id, $cascade);
if ($result === false && $this->Behaviors->enabled('SoftDelete')) {
return $this->field('deleted', array('deleted' => 1));
}
return $result;
}
当我尝试使用软删除(意味着简单的级联删除)级联删除时,它工作正常。 但我的问题是。 当我使用Utils plugin的softdelete行为时,Cascade删除不起作用。
我尝试使用以下代码删除记录
if ($this->CustomerAddress->delete()) {
$this->Session->setFlash(__('CustomerAddress deleted'));
$this->redirect($this->referer());
}
SQL日志
array(
'log' => array(
(int) 0 => array(
'query' => 'SELECT COUNT(*) AS `count` FROM `lewie`.`customer_addresses` AS `CustomerAddress` LEFT JOIN `lewie`.`customers` AS `Customer` ON (`CustomerAddress`.`customer_id` = `Customer`.`id`) LEFT JOIN `lewie`.`states` AS `State` ON (`CustomerAddress`.`state_id` = `State`.`id`) LEFT JOIN `lewie`.`customer_contacts` AS `CustomerContact` ON (`CustomerContact`.`customer_address_id` = `CustomerAddress`.`id`) LEFT JOIN `lewie`.`customer_reports` AS `CustomerReport` ON (`CustomerReport`.`customer_address_id` = `CustomerAddress`.`id`) WHERE `CustomerAddress`.`id` = 43 AND `CustomerAddress`.`deleted` = '0'',
'params' => array(),
'affected' => (int) 1,
'numRows' => (int) 1,
'took' => (float) 0
),
(int) 1 => array(
'query' => 'SELECT COUNT(*) AS `count` FROM `lewie`.`customer_addresses` AS `CustomerAddress` LEFT JOIN `lewie`.`customers` AS `Customer` ON (`CustomerAddress`.`customer_id` = `Customer`.`id`) LEFT JOIN `lewie`.`states` AS `State` ON (`CustomerAddress`.`state_id` = `State`.`id`) LEFT JOIN `lewie`.`customer_contacts` AS `CustomerContact` ON (`CustomerContact`.`customer_address_id` = `CustomerAddress`.`id` AND `CustomerContact`.`deleted` = '0') LEFT JOIN `lewie`.`customer_reports` AS `CustomerReport` ON (`CustomerReport`.`customer_address_id` = `CustomerAddress`.`id` AND `CustomerReport`.`deleted` = '0') WHERE `CustomerAddress`.`id` = 43 AND `CustomerAddress`.`deleted` = '0'',
'params' => array(),
'affected' => (int) 1,
'numRows' => (int) 1,
'took' => (float) 1
),
(int) 2 => array(
'query' => 'SELECT COUNT(*) AS `count` FROM `lewie`.`customer_addresses` AS `CustomerAddress` LEFT JOIN `lewie`.`customers` AS `Customer` ON (`CustomerAddress`.`customer_id` = `Customer`.`id`) LEFT JOIN `lewie`.`states` AS `State` ON (`CustomerAddress`.`state_id` = `State`.`id`) LEFT JOIN `lewie`.`customer_contacts` AS `CustomerContact` ON (`CustomerContact`.`customer_address_id` = `CustomerAddress`.`id` AND `CustomerContact`.`deleted` = '0') LEFT JOIN `lewie`.`customer_reports` AS `CustomerReport` ON (`CustomerReport`.`customer_address_id` = `CustomerAddress`.`id` AND `CustomerReport`.`deleted` = '0') WHERE `CustomerAddress`.`id` = 43 AND `CustomerAddress`.`deleted` = '0'',
'params' => array(),
'affected' => (int) 1,
'numRows' => (int) 1,
'took' => (float) 0
),
(int) 3 => array(
'query' => 'UPDATE `lewie`.`customer_addresses` SET `deleted` = '1', `deleted_date` = '2014-05-27 10:46:43', `modified` = '2014-05-27 10:46:43' WHERE `lewie`.`customer_addresses`.`id` = '43'',
'params' => array(),
'affected' => (int) 1,
'numRows' => (int) 1,
'took' => (float) 0
),
(int) 4 => array(
'query' => 'SELECT `CustomerAddress`.`deleted` FROM `lewie`.`customer_addresses` AS `CustomerAddress` WHERE `deleted` = '1' LIMIT 1',
'params' => array(),
'affected' => (int) 1,
'numRows' => (int) 1,
'took' => (float) 0
)
),
'count' => (int) 5,
'time' => (float) 1)
如何解决此问题?