所以,我试图找出什么是函数的确切顺序/时间被调用在我的控制器在Yii。现在,我有这样的代码
public function actionIndex()
{
$userId = Login::model()->getUserId();
// update all of the account balances upon viewing
// Account::model()->updateAccountBalance($userId);
// limits the data provided to only those accounts that are of the same userId
$dataProvider= new CActiveDataProvider('Account', array('criteria'=>array(
'condition'=>'user_id="'.$userId.'"')));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
// an attempt to update my account after the view has rendered.
// nope this is super super slow.
Account::model()->updateAccountBalance($userId);
}
我想更新我的帐户余额在数据库后视图中的一切都已呈现。(在我看来,我有几个使用Ajax调用外部服务器。)现在,这似乎工作得很好-而且它似乎和最初使用javascript一样快。但是,我对顺序还是有点困惑。是我的函数吗
Account::model()->updateAccountBalance($userId);
被称为后视图已经完全呈现(即ajax调用)?我知道有特定的功能,如afterRender和过滤器-但这个功能在渲染后被调用吗?
看你的代码是100%肯定的,Account::model()->updateAccountBalance($userId);
没有被调用。$this->render();
将exit
后,您的视图渲染成功。您也可以在这里查看有关render();
的文档:http://www.yiiframework.com/doc/api/1.1/CController#render-detail
通过将true作为返回参数添加到$this->render();
,您将能够在视图呈现后提供应用程序"退出"。但是那样的话,你会再次得到你的"延迟"。
$viewData = $this->render('index',array(
'dataProvider'=>$dataProvider,
), true);
// an attempt to update my account after the view has rendered.
// nope this is super super slow.
Account::model()->updateAccountBalance($userId);
echo $viewData;
你应该运行Account::model()->updateAccountBalance($userId);
在一个自己的线程由AJAX(客户端)或CRON(后端)触发,使异步进程工作良好,没有任何"延迟"。