CakePHP 软删除的数据仍然显示在模型关联中

发布于 2024-10-18 08:52:53 字数 1414 浏览 5 评论 0原文

我正在使用 SoftDeletableBehavior 作为我的地址模型。

这样设置后,当您删除地址时,它并不会真正删除它,而是将数据库中的 deleted 列设置为 1。

这也会更改您在 beforeFind 函数中的查询地址模型仅提取 deleted != 1 的条目

当我通过地址控制器(如 /addresses/show/43)查看地址时,这会起作用。

但是我的Users控制器hasMany地址。因此,当我在用户控制器中调用 $this->find('first'); 来获取用户时,我还会获取关联的地址。我希望这不会给我(软)删除的地址,但它确实给了我。地址模型上的 beforeFilter 也从未被调用。

处理这个问题的正确方法是什么?


更新。

显然,当我这样做时:

$data = $this->User->find('first',array('conditions' => array('User.id' => $id),'recursive' => 2));
    

我有一个 $data['User] 数组和一个 $data['Address] 数组(以及其他数组)。

但这并没有使用地址模型中的beforeFind过滤器。所以我得到了错误的数据。

但如果我这样做:

$data = $this->User->find('first',array('conditions' => array('User.id' => $id),'recursive' => 2));
$data['Address'] = $this->User->Address->find('all',array('conditions'=>array('user_id'=>$id)));

那么它确实使用Address模型上的beforeFind过滤器并返回正确的数据。

(当然采用不同的格式[Address][0][id][Address][0][Address][id]

我做什么不明白的是,如果提取的关联数据不使用自己的模型来查找其数据,那么还有什么意义呢?这不是MVC的主要目的之一吗?

也许我只是不明白?或者我错过了什么?

有人可以启发我吗?

I am using the SoftDeletableBehavior for my addresses model.

This sets it so when you delete an address it doesnt really delete it, instead sets a deleted column in the database to 1.

This also changes your query in the beforeFind function of the Address model to only pull entries where deleted != 1

This works when I am viewing addresses via the addresses controller like /addresses/show/43.

However my Users controller hasMany addresses. So when I am in the users controller and I call $this->find('first'); To get the user, I also get the associated addresses. I am expecting this to NOT give me the (softly) deleted addresses but it DOES. The beforeFilter on the Address Model is never called either.

What is the right way to handle this?


Update.

Apparently when I do this:

$data = $this->User->find('first',array('conditions' => array('User.id' => $id),'recursive' => 2));
    

I have a $data['User] array and a $data['Address] array (along with others).

But this does not use the beforeFind filter in the Address Model. So I get the wrong data.

But if I do this:

$data = $this->User->find('first',array('conditions' => array('User.id' => $id),'recursive' => 2));
$data['Address'] = $this->User->Address->find('all',array('conditions'=>array('user_id'=>$id)));

Then it does use the beforeFind filter on the Address model and returns the right data.

(Of course in a different format [Address][0][id] vs [Address][0][Address][id])

What I do not understand is if the associated data thats pulled does not use its own model to find its data, then whats the point? Isn't this one of the main purposes of MVC?

Maybe I just don't understand? Or I am missing something?

Can someone please enlighten me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屌丝范 2024-10-25 08:52:53

你就不能把这个条件放到你的协会里吗?

<?php

class User extends AppModel {
    var $hasMany = array(
        'Address' => array(
            'conditions' => array('NOT' => array('Address.deleted' => '1')),
        )
    );
}

?>

Can't you put this condition in your association?

<?php

class User extends AppModel {
    var $hasMany = array(
        'Address' => array(
            'conditions' => array('NOT' => array('Address.deleted' => '1')),
        )
    );
}

?>
煞人兵器 2024-10-25 08:52:53

就个人而言,我会为用户的查找查询添加一个条件

$this->Users->find->('all',array('conditions'=>array('Address.deleted !='=> ;'1')));

(尚未测试代码的确切语法,但这是总体思路)

Personally, I would add a condition to the find query for users

$this->Users->find->('all',array('conditions'=>array('Address.deleted !='=>'1')));

(haven't tested the code for exact syntax, but this is the general idea)

感情旳空白 2024-10-25 08:52:53

当您使用 $this->User->find(...) 并包含地址时,您只会看到 User 模型的回调触发,因为这是您用来驱动查找的模型。

这就是为什么您必须在用户模型的关联中使用额外的条件来过滤掉软删除的地址。

如果当递归范围包含地址时,在用户模型上的每次查找期间都会触发地址模型的回调,那么您将无法控制何时包含软删除的地址,并且可能存在这些回调在功能和参数操作方面发生冲突的风险。更不用说这可能产生的严重性能影响。

When you use $this->User->find(...) and include Addresses, you will only see the User model's callbacks fire, because that is the model you are using to drive the find.

This is why you have to use extra conditions in your User model's associations to filter out the soft-deleted Addresses.

If the Address model's callbacks were fired during every find on the User model when recursion scope included Addresses, you'd have no control as to when to include the soft-deleted Addresses and would risk clashes in functionality and param manipulation by those callbacks. Not to mention the serious performance impact this could have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文