Hi, first of all,I’m pretty new to Yii, but really impressed with what I can do with the framework!
I have to model objects, Customer and Project, and a project has a Customer as it’s owner. There’s no hard foreign key constraint, but one in SQL comments. Now, when I want to delete a Customer from the admin view (a generated controller with actions), I check whether the customer is referenced by one or more projects. If so, I don’t delete and set a flash message. But the message doesn’t appear… why not? It has to do with AJAX, but how? When this action is invoked from the ‘update’ page (no AJAX), this works. Here is my action method:
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'index' page.
*/
public function actionDelete()
{
if(Yii::app()->request->isPostRequest)
{
// Check if this customer is not referenced as a project owner or intermediate
$c = $this->loadModel();
$numRefs = Project::model()->count(
'ownerId=:ownerId OR intermediateId=:intId',
array(':ownerId'=>$c->id,':intId'=>$c->id)
);
if ($numRefs == 0)
{
// we only allow deletion via POST request
$c->delete();
}
else
{
Yii::app()->user->setFlash('message',"This customer is referenced from one or more projects, either as the customer or as the intermediate.");
}
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_POST['ajax']))
{
$this->redirect(array('index'));
}
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
and my view (partly):
<?php if(Yii::app()->user->hasFlash(‘message’)):?>
<div class="error">
<?php echo Yii::app()->user->getFlash('message'); ?>
</div>
<?php endif; ?>
Regards,