Canastro
Canastro

Reputation: 3009

Deleting a record from a different model

I have the models: sales and follow. The table follow has an id, an user_id and sale_id. What I want to do is: on the sales_controller I need to find one follow record that has the user_id that is Authenticated and the sale_id that is passed as parameter.

So here is what I've tried:

Attempt 1,

App::import('model','Follow');
$follow = new Follow();
$follow->user_id = $this->Auth->user('id');
$follow->sale_id = $id;
$follow->delete();

Attempt 2,

App::import('model','Follow');
$follow = new Follow();
$follow->delete(array('Follow.user_id'=>$this->Auth->user('id'), 'Follow.sale_id'=>$id));

Attempt 3,

App::import('model','Follow');
$follow = new Follow();
$result = $follow->find('first',array('conditions'=>array('Follow.user_id'=>$this->Auth->user('id'), 'Follow.sale_id'=>$id)));
$result->delete()

Attempt 4,

 var $uses = array('Sale', 'Follow');
 (...)
 $result = $this->Follow->find('first',array('conditions'=>array('Follow.user_id'=>$this->Auth->user('id'), 'Follow.sale_id'=>$id)));
 $result->delete();

In attempt 3 and 4 $result does return the value I was expecting, but delete doesn't work.

But none of these attempts worked out.

Upvotes: 0

Views: 3287

Answers (1)

Rau
Rau

Reputation: 76

Try in the controller:

    var $uses = array('MyOriginalModelName', 'Follow');

Then $this->Follow will work. And also It seems to be logical to add into the controller's "uses" list if you actually want to delete from it

On a side note you may want to check out the loadModel() function.

Upvotes: 1

Related Questions