我有以下表格:causes
,users
,transactions
Cause hasMany Transaction
和User hasMany Transaction
Transaction belongsTo Cause
和Transaction belongsTo User
我的causes
和users
表格都包含transaction_count
个字段,belongsTo
counterCache => true
表示hasMany
两个关联。
我的dependent => true
个条款都foreignKeys
,我的Transaction
设置正确,因为如果删除原因,所有关联的交易也会被删除。
它也有效,因为当我创建transaction_count
时,users
和causes
表中的cause
字段都会正确更新。这是细分:
1。)创建transaction
2。)创建causes[ 15 ][ 'transaction_count' ]
结果:users[ 1 ][ 'transaction_count' ]
正确增加1
结果:transaction
正确增加1
3。)手动删除$transaction->delete( $id )
(causes[ 15 ][ 'transaction_count' ]
)
结果:users[ 1 ][ 'transaction_count' ]
正确减少1
结果:Cause
正确减少1
4.。失败:
如果我创建Transaction
和Cause
,然后使用$this->Cause->delete( $id )
删除transactions
,即使数据库中删除了所有Cause
(与transaction_count
)一起,users
表格中的$this->Transaction->delete()
未更新以反映减少量。
我想这是因为Cause
未被明确调用,而是因为它与{{1}}的关联而被删除了?
有解决方法吗?
答案 0 :(得分:1)
<强>表格强>
CREATE TABLE `causes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`transaction_count` int(10) unsigned NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `transactions` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`cause_id` int(10) unsigned NOT NULL DEFAULT '0',
`user_id` int(10) unsigned NOT NULL DEFAULT '0',
`name` varchar(255) NOT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`transaction_count` int(10) unsigned NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<强> user.php的强>
App::uses('AppModel', 'Model');
class User extends AppModel {
public $hasMany = array(
'Transaction' => array(
'dependent' => true
)
);
}
<强> Cause.php 强>
App::uses('AppModel', 'Model');
class Cause extends AppModel {
public $hasMany = array(
'Transaction' => array(
'dependent' => true
)
);
}
<强> Transaction.php 强>
App::uses('AppModel', 'Model');
class Transaction extends AppModel {
public $belongsTo = array(
'User' => array(
'counterCache' => true
),
'Cause' => array(
'counterCache' => true
)
);
}
<强>控制器强>
$this->Cause->query('TRUNCATE TABLE users');
$user['User'] = array(
'name' => 'aaa'
);
$this->Cause->Transaction->User->create($user);
$this->Cause->Transaction->User->save(null, false);
$userId = $this->Cause->Transaction->User->getLastInsertID();
$this->Cause->query('TRUNCATE TABLE causes');
$cause['Cause'] = array(
'name' => 'aaa'
);
$this->Cause->create($cause);
$this->Cause->save(null, false);
$causeId = $this->Cause->getLastInsertID();
$this->Cause->query('TRUNCATE TABLE transactions');
$transaction['Transaction'] = array(
'name' => 'aaa',
'user_id' => $userId,
'cause_id' => $causeId
);
$this->Cause->Transaction->create($transaction);
$this->Cause->Transaction->save(null, false);
$this->Cause->Transaction->create($transaction);
$this->Cause->Transaction->save(null, false);
$transaction['Transaction'] = array(
'name' => 'aaa',
'user_id' => $userId,
'cause_id' => 99
);
$this->Cause->Transaction->create($transaction);
$this->Cause->Transaction->save(null, false);
$this->Cause->delete($causeId);
$cause = $this->Cause->find('first', array(
'conditions' => array(
'Cause.id' => $causeId
)
));
$transactions = $this->Cause->Transaction->find('all', array(
'conditions' => array(
'Transaction.cause_id' => $causeId
)
));
$user = $this->Cause->Transaction->User->find('first', array(
'conditions' => array(
'User.id' => $userId
)
));
pr($cause);
pr($transactions);
pr($user);
if (is_a($this->Cause, 'Cause')) {
echo 'Cause';
}
if (is_a($this->Cause->Transaction, 'Transaction')) {
echo 'Transaction';
}
if (is_a($this->Cause->Transaction->User, 'User')) {
echo 'User';
}
<强>结果强>
Array
(
)
Array
(
)
Array
(
[User] => Array
(
[id] => 1
[name] => aaa
[transaction_count] => 1
)
)
CauseTransactionUser
一切正常。 仔细检查模型中的拼写错误并检查蛋糕是否正在使用您的模型,而不是在某些模型缺失时动态创建的模型。