2009-12-22 6 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()'를 호출 할 수 없습니까? 가장 효과적인 방법은 아니지만 액티브 레코드가 작동하는 것처럼 보입니다 ... – Franz

+0

하지만 $ this-> 내가 할 수있는 일은 다음과 같습니다. App :: Import? 나는 다음을 찾는다 : $ this-> Follow-> find ('first', array ('conditions'=> 배열 ('Follow.user_id'=> $ this-> Auth-> user ('id') , 'Follow.sale_id'=> $ id)))하지만 결과가 반환되지 않았습니다 ... 제가 여기서 뭔가 잘못하고 있습니까? – Canastro

답변

1

시도 :

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

그런 다음 $this->Follow이 작동합니다. 또한 컨트롤러의 "사용"목록에 실제로 추가하려는 경우 논리적 인 것 같습니다.

loadModel() function을 확인해보십시오.

+0

감사합니다. App :: Import로 작업하는 것이 더 좋은 방법 인 것 같습니다. 이미 삭제하고 $ 결과에 저장하려는 레코드를 찾을 수 있습니다. $ result = $ this-> follow-> find ('first', array ('conditions'=> 배열 ('Follow.user_id'=> $ this-> Auth-> user ('id') ' sale_id '=> $ id)))); "$ result-> delete()"는 여전히 작동하지 않습니다. S. 그리고 나는 이유를 알 수 없다. find 메소드를 사용해야하지 않습니까? – Canastro

+0

$ result-> delete()는 나에게 이해가되지 않습니다. $ this-> follow-> delete ($ result) 그런 식으로 진행하고 싶다면 :) – Rau

+1

흠, 괜찮아요. 조금 성급한 :) 그래서 $ this-> Follow-> delete ($ result [ 'Follow'] [ 'id']); 정확히 – Rau