Question

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?

Was it helpful?

Solution

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')),
        )
    );
}

?>

OTHER TIPS

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)

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top