Hi,
I am attempting to create a directory after a "client" is created in the DB.
In the aferSave() method of the Client.php model I was intending to attempt the directory creation, on failure delete the recently created model and set a flash message and return "false".
$result in my action always returns true though, despite my return false. From reading it seems afterSave return type is void.
As the directory requires the ID of the recently created model, how else can I do this without an afterSave call, and how can I notify the action of a failure if I can’t return false?
Also - I had to instantiate a new model (of the current class) with the $this->id as it didn’t seem to be able to “delete itself” with $this->delete()
public function actionCreate()
{
$model = new Client;
if( isset( $_POST['Client'] ) )
{
$model->attributes = $_POST['Client'];
$model->date_created = date( "Y-m-d H:i:s" );
$result = $model->save();
if( $result )
{
var_dump( $result );
#$this->redirect( array( 'view', 'id' => $model->id ) );
}
else
{
// Flash message set in Model with notify user of creation failure
}
}
}
protected function afterSave()
{
parent::afterSave();
if( $this->isNewRecord )
{
$dir = Yii::app()->params['upload_certificates_path'] . $this->id;
if( ! @mkdir( $dir, Yii::app()->params['upload_octal'] ) )
{
$model = Client::model()->findByPk( $this->id );
$model->delete();
//$this->delete(); // was causing a can't delete as model is new record error
Yii::app()->adminUser->setFlash( 'error', 'Unable to create the certificate directory for this client' );
return false;
}
}
}