duanjiaopi8218 2013-08-09 07:00
浏览 11

hasMany中的独家选项是什么?

In cakephp, there is an option called "exclusive" in "hasMany" field in models. The documentation says:

When exclusive is set to true, recursive model deletion does the delete with a deleteAll() call, instead of deleting each entity separately. This greatly improves performance, but may not be ideal for all circumstances.

But it is not so clear. I want to know what does this option do exactly, and what happens if we don't use it? Thanks.

  • 写回答

1条回答 默认 最新

  • duankanyi6539 2013-08-09 13:59
    关注

    Say For Example, One Author has many Books.So we can write this in form of $hasMany association as below,

       var $name = 'Author';
           var $hasMany = array('Book' =>
            array('className' => 'Book',
                'conditions' => '',
                'foreignKey' => 'author_id',
                'dependent' => true,
                'exclusive' => true
    
            )
        );
    

    dependent: When dependent is set to true, recursive model deletion is possible. In this example, Book records will be deleted when their associated Author record has been deleted.

    exclusive – If set to true, all the associated objects are deleted in one SQL statement without having their beforeDelete callback run.This greatly improves performance, but may not be ideal for all circumstances.

    Now just look at the records in the database

    authors table
    ===================
    id    name    
    ===================
    1     first author
    2     second author 
    3     third author
    

    books table

    ==================================
    id   title   isbn       author_id
    ==================================  
    1    abc    6416446846   1
    2    xyz    3146354313   1
    3    pqr    8945468485   2
    4    fgh    6434164656   2
    5    rtt    1215445644   3
    

    Now if you delete any record of Author,all the associated books with that particular Author will also be deleted.

    评论

报告相同问题?

悬赏问题

  • ¥20 simulink单相桥式整流电路
  • ¥35 问问51单片机流水灯的代码该怎么写
  • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
  • ¥15 stata webuse报错
  • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
  • ¥15 如何利用AI去除图片中的竹架子
  • ¥15 python 写个基金爬取的代码,自动卖出功能
  • ¥15 Linux系统启动不起来
  • ¥15 为什么运行仿真数码管不亮(语言-c语言)
  • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导