hasMany中的独家选项是什么?

时间:2020-12-11 17:18:42

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

在cakephp中,模型中的“hasMany”字段中有一个名为“exclusive”的选项。文件说:

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.

当exclusive设置为true时,递归模型删除会使用deleteAll()调用执行删除操作,而不是单独删除每个实体。这大大提高了性能,但可能并不适用于所有情况。

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 个解决方案

#1


0  

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

比如说,一个作者有很多书。所以我们可以用$ hasMany关联的形式写这个,如下所示,

   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.

dependent:当dependent设置为true时,可以删除递归模型。在此示例中,Book记录将在删除其关联的作者记录时删除。

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.

exclusive - 如果设置为true,则在一个SQL语句中删除所有关联的对象,而不进行beforeDelete回调运行。这极大地提高了性能,但可能并不适合所有情况。

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.

现在,如果您删除任何作者记录,则该特定作者的所有相关书籍也将被删除。

#1


0  

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

比如说,一个作者有很多书。所以我们可以用$ hasMany关联的形式写这个,如下所示,

   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.

dependent:当dependent设置为true时,可以删除递归模型。在此示例中,Book记录将在删除其关联的作者记录时删除。

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.

exclusive - 如果设置为true,则在一个SQL语句中删除所有关联的对象,而不进行beforeDelete回调运行。这极大地提高了性能,但可能并不适合所有情况。

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.

现在,如果您删除任何作者记录,则该特定作者的所有相关书籍也将被删除。