【问题标题】:CakePHP 2: In $this->Model->delete($id) delete the hasmany model in 'dependent' => falseCakePHP 2:在 $this->Model->delete($id) 中删除 'dependent' => false 中的 hasmany 模型
【发布时间】:2014-03-27 14:10:00
【问题描述】:

我在 CakePHP 2.1.5 中有一个奇怪的行为

当我删除一个理由时,会删除两件事。理由与 DocumentsProduct 相关联。

理由控制器:

class JustificationsController extends AppController {

  public function delete($id = null) {

    $this->Justification->id = $id;
    if ($this->Justification->delete()) {                        
      $valorNull = null;
      $this->Justification->DocumentsProduct->updateAll(
        array('DocumentsProduct.justification_id' => $valorNull),
        array('DocumentsProduct.justification_id' => $id));
    }
  }
}

理由模型:

public $hasMany = array(
  'DocumentsProduct' => array(
    'className' => 'DocumentsProduct',
    'foreignKey' => 'justification_id',
    'dependent' => false,
    'conditions' => '',
    'fields' => '',
    'order' => '',
    'limit' => '',
    'offset' => '',
    'exclusive' => '',
    'finderQuery' => '',
    'counterQuery' => ''
    )
  );

文档产品型号:

public $belongsTo = array(  
  'Justification' => array(
    'className' => 'Justification',
    'foreignKey' => 'justification_id',
    'conditions' => '',
    'fields' => '',
    'order' => ''
    )
);

因此,如果我在理由/删除视图中删除一个理由,则会同时删除理由和 DocumentProduct。但是依赖是错误的。 我尝试 $this->Justification->delete($id, false),但仍然删除 DocumentsProduct。

我做错了什么?

更新:这是查询日志的一部分:

(int) 130 => array(
        'query' => 'SELECT COUNT(*) AS `count` FROM `icorvera_sgcp`.`approvals_documents_products` AS `ApprovalsDocumentsProduct`   WHERE `ApprovalsDocumentsProduct`.`id` = '52d00c27-4d84-47bc-9c47-169cc4460461'',
        'params' => array(),
        'affected' => (int) 1,
        'numRows' => (int) 1,
        'took' => (float) 1
    ),
    (int) 131 => array(
        'query' => 'DELETE `ApprovalsDocumentsProduct` FROM `icorvera_sgcp`.`approvals_documents_products` AS `ApprovalsDocumentsProduct`   WHERE `ApprovalsDocumentsProduct`.`id` = '52d00c27-4d84-47bc-9c47-169cc4460461'',
        'params' => array(),
        'affected' => (int) 1,
        'numRows' => (int) 1,
        'took' => (float) 1
    ),
    (int) 132 => array(
        'query' => 'DELETE `DocumentsProduct` FROM `icorvera_sgcp`.`documents_products` AS `DocumentsProduct`   WHERE `DocumentsProduct`.`id` = '52d00c27-a1f4-4756-a126-169cc4460461'',
        'params' => array(),
        'affected' => (int) 1,
        'numRows' => (int) 1,
        'took' => (float) 1
    ),
    (int) 133 => array(
        'query' => 'DELETE `Justification` FROM `icorvera_sgcp`.`justifications` AS `Justification`   WHERE `Justification`.`id` = '529e1dbc-bbc0-4be1-828d-09c8c4460461'',
        'params' => array(),
        'affected' => (int) 1,
        'numRows' => (int) 1,
        'took' => (float) 1
    )
),
'count' => (int) 134,
'time' => (float) 343
)

【问题讨论】:

  • 您在数据库中是否有表之间的链接?
  • 不。我也得到了蛋糕查询的日志
  • 可以添加 ApprovalsDocumentsProduct 模型关系吗?

标签: database cakephp dependencies models


【解决方案1】:

我也有类似的问题。

经过一番调查,我注意到在数据库本身中,我对“onDelete”事件进行了“级联”操作。这覆盖了 cakephp 行为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多