我試圖在稍後訪問的控制器內設置模型的屬性值。在我的索引我有如何在控制器中設置模型的屬性 - yii
public function actionIndex($id)
{
$model = new Recipient;
$model->listId = $id;
ActiveDataProvider('Recipient', array('criteria'=>array(
'condition'=>'list_id = '.$id)));
$this->render('index',array(
'dataProvider'=>$dataProvider,
'id'=>$id
));
}
,然後在模型中,我想使用listId所以我有
public $listId;
// before you save the function here are the things you should do
public function beforeSave()
{
if($this->isNewRecord)
{
// updates the list_id of the individual with the passed in listId
$this->list_id = $listId;
}
return parent::beforeSave();
}
現在這個模型函數將被調用之前,我在控制器中使用actionCreate(不的actionIndex)。是否仍然有辦法像控制器中那樣保留該變量,然後在模型中調用它?我一直在玩的另一個選項是將$ id從索引中的視圖傳遞到create中的視圖中。但是,我並不認爲我應該在視圖中傳遞太多數據。
如果你說$ model = new Recipient沒有被使用,那麼當我調用$ model-> listId = $ id時會發生什麼?這是不是指我創建的新收件人? – Stephen
我的意思是說,所做的一切就是設置「listid」。 '$ model'沒有被保存,在任何地方引用或傳入視圖,因此它是沒有用的。 – topher
啊,好吧,所以我需要保存$模型,如果我想反正使用它 - 所以這是空的代碼在這種情況下。即使我嘗試在「beforeSave」函數中使用它,情況也是如此? – Stephen