2009-12-22 68 views
0

我有型號:銷售和關注。表格後面有一個id,一個user_id和sale_id。 我想要做的是:在sales_controller上,我需要找到一個具有已驗證的user_id和作爲參數傳遞的sale_id的跟蹤記錄。刪除不同型號的記錄

所以這是我試過:

嘗試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(); 

在嘗試3和4 $result確實返回我期待的值,但刪除不起作用。

但是這些嘗試都沒有解決。

+0

難道你不能首先獲取符合條件的記錄(而不是創建一個新的'Follow'對象),然後在其上調用'delete()'?不是最有效的方法,但這就是Active Record似乎工作的方式... – Franz 2009-12-22 14:51:35

+0

但是,我可以做$ this->遵循我做的應用程序::導入?我嘗試做的發現:$ this-> Follow-> find('first',array('conditions'=> array('Follow.user_id'=> $ this-> Auth-> user('id')) ,'Follow.sale_id'=> $ id)))但它沒有返回任何結果...我在這裏做錯了什麼? – Canastro 2009-12-22 15:19:07

回答

1

嘗試在控制器:

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

然後$this->Follow會工作。 而且如果你真的想從中刪除它似乎是合乎邏輯的添加到控制器的「用途」列表中

在旁註中,您可能需要查看loadModel() function

+0

謝謝你,這似乎是一個更好的方式與App :: Import一起工作。我已經可以找到要刪除的記錄並將其存儲在$ result中。 $ result = $ this-> follow-> find('first',array('conditions'=> array('Follow.user_id'=> $ this-> Auth-> user('id'),'Follow。 sale_id'=> $ ID))); 但是「$ result-> delete()」仍然不起作用:S。我找不到原因。我不應該使用find方法嗎? – Canastro 2009-12-22 15:51:20

+0

$ result-> delete()對我沒有意義... $ this-> follow-> delete($ result)如果你想繼續這樣:) – Rau 2009-12-22 15:53:39

+1

嗯,好的,不是真的,我是一個bit hasty :) so $ this-> Follow-> delete($ result ['Follow'] ['id']);準確地說 – Rau 2009-12-22 15:55:35