We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Delete related records after update/save

I hav two models.

namespace Manager\Models;
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Uniqueness;
class Aliases extends Model
{
    public $id;
    public $name;
    public $description;
    public $aliastypeId;
    public function validation()
    {
        $this->validate(new Uniqueness(array(
            "field" => "name",
            "message" => "The alias is already registered"
        )));
        return $this->validationHasFailed() != true;
    }
    public function initialize()
    {
        $this->hasMany('id', 'Manager\Models\AliasesContent', 'aliasId', array(
            'alias' => 'aliasescontent',
            'reusable' => true
        ));
    }
}

namespace Manager\Models;
use Phalcon\Mvc\Model;
class AliasesContent extends Model
{
    public $id;
    public $content;
    public $aliasId;
    public function initialize()
    {
        $this->belongsTo('aliasId', 'Manager\Models\Aliases', 'id', array(
            'alias' => 'aliases',
            'reusable' => true
        ));
    }
}

When I edit a "Aliases" and saved, I have to remove all the contents of the table "AliasesContent" referring to "AliasesContent.aliasId" to re-enter the new content.

public function editAction($id)
{
if ($this->request->isPost()) {
   $aliases->assign(array(
      'name' =>$this->request->getPost('name', 'striptags'),
      'description' => $this->request->getPost('description', 'striptags')
   ));

   // Delete the entire contents of the related table.
   AliasesContent::find('aliasId IN ('.$id.')')->delete();
   // ------------

   $aliasContentPost = $this->request->getPost('aliascontent');
   $aliascontent = array();
   foreach ($aliasContentPost as $key => $value) {
      $aliascontent[$key] = new AliasesContent();
      $aliascontent[$key]->content = $value;
   }
   $aliases->AliasesContent = $aliascontent;
   if (!$aliases->save()) {
      $this->flash->error($aliases->getMessages());
   } else {
      $this->flash->success("Alias was updated successfully");
   }
}

The problem is that if the update/save fails the validation name (Uniqueness) present in the model, the related table records are deleted and new records are not inserted.

I do not want to update only the difference, I need to be removed all records in the related table and reinsert the new records, after validation of the main table "Aliases".

Thanks for any help.



33.8k
Accepted
answer

Yeah, when you update a record of a table like yours that have depending records, you have to re-create the relations. I also had to do that many times.

And for this:

The problem is that if the update/save fails the validation name (Uniqueness) present in the model, the related table records are deleted and new records are not inserted.

Just do a if ($alias->create()). Inside, initiate a manual transaction first ($this->db->begin()). When you do all your logic correctly (sucesfully delete/recreate records), do a commit ($this->db->commit()); else, a rollback ($this->db->rollback()). This way you will have your original records intacted and no ones will be created.

More info on https://docs.phalcon.io/es/latest/reference/models.html#manual-transactions

Thank you for your help RompePC and Seghei.