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

Need to validate relation model

Hi everyone.

I have a Template model with following relations

class Template extends ModelBase
{

    public function initialize()
    {
        $this->hasMany('id', 'Api\Modules\Template\Models\TemplateLanguage', 'template_id', ['alias' => 'TemplateLanguage']);
        $this->hasMany('id', '\Api\Modules\Template\Models\TemplateRelation', 'related_template_id', ['alias' => 'TemplateRelationRelated']);
        $this->hasMany('id', '\Api\Modules\Template\Models\TemplateRelation', 'template_id', ['alias' => 'TemplateRelationMain']);
        $this->belongsTo('template_type_id', '\Api\Modules\Template\Models\TemplateType', 'id', ['alias' => 'TemplateType']);
        $this->belongsTo('locked_by_user_id', '\Api\Modules\User\Models\User', 'id', ['alias' => 'UserLockedBy']);
        $this->belongsTo('created_by_user_id', '\Api\Modules\User\Models\User', 'id', ['alias' => 'UserCreatedBy']);
    }

}

In my TemplateRelation model I have following validators

class TemplateRelation extends ModelBase
{
    public function validation()
    {
        $di = \Phalcon\DI::getDefault();
        $validator = new Validation();
        $validator->add(
            'related_template_id',
            new Digit([
                "message" => $di['t']->_("template_relation.error.related_template_id_must be number")
            ]));
        return $this->validate($validator);
    }
}

The $di['t'] is my service for translation.

When in my controller I do

class TemplateController extends ControllerBase
{
    public function updateAction(int $templateId
    {
        //some code with seting $template properties
        $template->update();
    }
}

And then TemplateRelation validators don't work. Anyone has any idea what I could do wrong?

ok do this and show us the error message

class TemplateController extends ControllerBase
{
    public function updateAction(int $templateId
    {
        //some code with seting $template properties
        if($template->update() === false) {
            echo implode(' ', $template->getMessages());
        }
    }
}

@DegiovanniEmilio

ok do this and show us the error message

class TemplateController extends ControllerBase
{
   public function updateAction(int $templateId
   {
       //some code with seting $template properties
       if($template->update() === false) {
          echo implode(' ', $template->getMessages());
      }
   }
}

I run the code against my test data, when I put a string in a property I expect a int.

Column 'aaa' doesn't belong to any of the selected models (1), when preparing: SELECT [Api\Modules\Template\Models\Template].* FROM [Api\Modules\Template\Models\Template] WHERE aaa LIMIT :APL0:<br><pre>#0 [internal function]: Phalcon\Mvc\Model\Query->_getQualified(Array)
#1 [internal function]: Phalcon\Mvc\Model\Query->_getExpression(Array)
#2 [internal function]: Phalcon\Mvc\Model\Query->_prepareSelect()
#3 [internal function]: Phalcon\Mvc\Model\Query->parse()
#4 [internal function]: Phalcon\Mvc\Model\Query->execute()
#5 /var/www/api/app/modules/template/models/Template.php(136): Phalcon\Mvc\Model::findFirst('aaa')
#6 /var/www/api/app/modules/template/models/TemplateRelation.php(98): Api\Modules\Template\Models\Template::findFirst('aaa')
#7 /var/www/api/app/modules/template/controllers/TemplateController.php(265): Api\Modules\Template\Models\TemplateRelation->setRelatedTemplateId('aaa')
#8 [internal function]: Api\Modules\Template\Controllers\TemplateController->updateAction(13, '13')
#9 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(Api\Modules\Template\Controllers\TemplateController), 'updateAction', Array)
#10 [internal function]: Phalcon\Dispatcher->dispatch()
#11 /var/www/api/public/index.php(55): Phalcon\Mvc\Application->handle()
#12 {main}</pre>

@WojciechŚlawski Thank you for the advice, I will try to implement it :)