views:

350

answers:

1

Well this may be a question with a simple answer but I've been searching and couldn't find how to do this.

I have the models: sales and follow. The table follow has an id, an user_id and sale_id. What I wan't 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:

1

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

2

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

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()

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 e #4 '$result' does return the value i was expecting, but delete doesn't work.

But none of these attempts worked out.

Anyone can help?

Thanks.

+1  A: 

Yup, that's wrong :), 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: http://book.cakephp.org/view/845/loadModel

Rau
Thank you, that seems to be a better way to work that with the App::Import. I can already find the record I want to delete and store it in $result.$result = $this->Follow->find('first',array('conditions'=>array('Follow.user_id'=>$this->Auth->user('id'), 'Follow.sale_id'=>$id)));But "$result->delete()" still doesn't work :S. And I can't find out why. Shouldn't I use find method?
NoOne
$result->delete() makes no sense to me...$this->follow->delete($result) if you want to go on that way :)
Rau
hmm, ok, not really, I was a bit hasty:) so $this->Follow->delete($result['Follow']['id']); to be exact
Rau
Thank you, that's it! I'm still quite newbie @ Cakephp so I had no idea what was wrong with the delete :P. Once again, thank you for the fast response.
NoOne
Np mate. All you need is the model (which you can get with the loadModel() function if you are in an another model, or don't want to set the $uses array for some reason) and the id of the record - so find() is only necessary if you don't know the exact id you want to delete
Rau