【发布时间】:2014-08-31 22:47:22
【问题描述】:
我目前正在尝试将一个变量从控制器传递到我的模型。从以前的观点来看,我在一个按钮中传递了 $id。
<a class="btn btn-info" type="button" href="index.php?r=recipient/index&id=<?php echo $data->id; ?>">Manage</a>
从那里,我访问了控制器内部的 $id 并使用了它。但是,现在我正在模型中创建一个需要使用该 $id 的函数。我不能将它作为参数传递,因为它是 beforeSave 函数(在我保存新条目之前更新我的数据库) 这在模型 Recipient.php 中看起来像这样
// 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 selected ListId
$this->list_id = Recipient::model()->getListId;
}
return parent::beforeSave();
}
最后,我尝试在同一模型中创建函数 getListId。
public function getListId()
{
// my code here
}
有没有一种简单的方法可以将变量从控制器传递给模型以供使用?或者,模型中的函数有没有办法访问传递给控制器的变量?
【问题讨论】: