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.