doudao7511 2016-06-24 11:24
浏览 6
已采纳

too long

I have 2 entities in the database Element and Section.

1 section has 0-N elements so in the element table I have a foreign key

CONSTRAINT `elements_sections` FOREIGN KEY (`section_id`) REFERENCES `sections` (`id`) ON UPDATE CASCADE ON DELETE CASCADE

Element Model .php has this:

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

In the Section model I have:

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

All this code looks good, however, when I delete an element of the section with ID=X, if there's an element (in any other section) with ID=X that element is deleted too. This is the code I am using in my whole aplication but only fails here. $this->data is the Element object.

$this->Element->delete($this->data)

A detailed example:

Section 20 -> Element 400 Section 3 -> Element 20

If I delete the element 400, the Element 20 is deleted too.

I guess that I am missing a configuration parameter or some.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dougou7008 2016-06-27 08:24
    关注

    For somebody that can face the same problem this could help:

    Model::delete() can delete multiple records.

    [
        'id' => 1,
        'category_id' => 3,
        'price' => 10000,
    ]
    

    Will generate

    DELETE FROM elements WHERE id IN (1, 3, 10000).
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?