I'm after some code advice. I've got two models which are dependent on each other. When one of the models gets deleted I want to make sure both records in the database are deleted.
I handle this in one direction using foreign keys so if the parent gets deleted to. But as these rows are both dependent on each other I need the same functionality to happen in the child.
In the child model I've overloaded the delete method so it looks like this:
public function delete() {
$cameraTransaction = $this->dbConnection->beginTransaction();
try
{
$this->ftpuser->delete();
if($this->beforeDelete())
{
$result=$this->deleteByPk($this->getPrimaryKey())>0;
$this->afterDelete();
}
$cameraTransaction->commit();
}
catch(Exception $e) // an exception is raised if a query fails
{
$cameraTransaction->rollBack();
}
}
I've tested and this seems to work well. I wondered if an expert / guru could confirm whether I've done the right thing :)
Thanks
Alan