2014-07-14 2 views
1

최근에 업데이트 한 모델 저장이라는 개념이 도입되었습니다. 이 예제에서는 incrementPayList라는 함수를 만들었습니다.이 함수는 목록의 ID를 가져 와서 하나씩 증가시킵니다. 모델 저장 위치 - Yii

은 paylist 모델

public function incrementPayList($id) 
{ 
    $results = Recipient::model()->findAll(array("condition"=>"list_id = $id")); 
    $count = count ($results); 
    $this->num_indv = $count + 1;   
} 

이제받는 사람 컨트롤러, I는이 함수를 호출하고있는 함수에 대한 코드입니다. 받는 사람은 내가 $count 정확한 수를 반환 것을 알고 특정 payList

*/ 
public function actionCreate($id) 
{ 
    $model=new Recipient; 
    // Uncomment the following line if AJAX validation is needed 
    // $this->performAjaxValidation($model); 

    if(isset($_POST['Recipient'])) 
    { 
     $model->attributes=$_POST['Recipient']; 

     Paylist::model()->incrementPayList($id); 
     if($model->save()) 
     { 
      $this->redirect(array('view','id'=>$model->id)); 
     } 
    } 

    $this->render('create',array(
     'model'=>$model, 
     'id'=>$id 
    )); 
} 

에 연결되어있는 개인을 모두 보유 -하지만 필드 num_indv에서 그 수를 저장하지 않습니다. 필자는 새로운 PayList 모델을 저장하지 않기 때문에이 사실을 알게되었습니다. 그러나, 어떻게/어디에서해야합니까? 함수 자체에서해야할까요? 아니면 컨트롤러에서해야합니까? 그러나 컨트롤러에서 수행 할 경우 동일한 모델이 아니기 때문에 $model->save()을 단순히 사용할 수 없습니다.

답변

0

$modelPaylist = new Paylist; 

    if(modelPaylist->save()).... 
+0

모델 또는 컨트롤러에 있습니까? – Stephen

+0

받는 사람 컨트롤러 –

1

처럼 작동하지 않는 이유를 절약 할 수 있습니다 당신이 Paylist 인스턴스 $modelincrementPayList를 실행하지만 Recipient 싱글에 (자세한 내용은 Difference between static class and singleton pattern?Best practice on PHP singleton classes 참조)하지 않는 것입니다 .

에 코드를 읽어야 만 숫자가 아니라 상품 자체를 필요로하기 때문에

$model->attributes=$_POST['Recipient']; 
$model->incrementPayList($id); 
if($model->save()) 
    { 

대신 count($results) 다음 findAllCActiveRecord::count()를 사용합니다.

+0

어떻게 $ model-> incrementPayList ($ id); $ model이 새로운 수신자 모델로 형성된 경우 작동합니다. 당신은 내가 recipientPayList를 수령인 모델에 집어 넣어야한다는 말입니까? – Stephen

+0

내가 틀렸다면 나를 정정하되'incrementPayList'는 하나의 레코드에 대해'num_indv'를 설정합니다. 각 CActiveRecord는 레코드를 나타내므로'$ model'에서 호출해야하며 Recipient :: model()에서는 호출하지 않아야합니다. – topher