CakePHP counterCache关联表字段不会在delete()上更新

时间:2014-04-19 12:43:24

标签: cakephp

我有以下表格:causesuserstransactions

Cause hasMany TransactionUser hasMany Transaction

Transaction belongsTo CauseTransaction belongsTo User

我的causesusers表格都包含transaction_count个字段,belongsTo counterCache => true表示hasMany两个关联。

我的dependent => true个条款都foreignKeys,我的Transaction设置正确,因为如果删除原因,所有关联的交易也会被删除。

它也有效,因为当我创建transaction_count时,userscauses表中的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.。失败:

如果我创建TransactionCause,然后使用$this->Cause->delete( $id )删除transactions,即使数据库中删除了所有Cause(与transaction_count)一起,users表格中的$this->Transaction->delete()未更新以反映减少量。

我想这是因为Cause未被明确调用,而是因为它与{{1}}的关联而被删除了?

有解决方法吗?

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

一切正常。 仔细检查模型中的拼写错误并检查蛋糕是否正在使用您的模型,而不是在某些模型缺失时动态创建的模型。