I have a model Audio and a model AudioCategory.
When I save the audio object, I want to validate that there is at least 1 audiocategory attached to it.
I created a custom validator for that.
I tried using the $audio->getRelated() in the validator, but it keeps trying to fetch in the database for the info. Since the validation occurs before saving (which is great), then I receive an empty list, thus my validator always returns false.
When I print the audio object without saving, I can see my audiocategory in the field _related of the audio object (print_r($audio);):
[_related:protected] => Array
(
[audiocategory] => Array
(
[0] => GRQ\Audio\AudioCategory Object ([...])
[1] => GRQ\Audio\AudioCategory Object ([...])
)
)
If I try to print $audio->audiocategory directly, I get a notice:
Access to undefined property GRQ\Audio\Audio::audiocategory
and nothing is returned.
If I call $audio->getRelated(), I get an object of type Phalcon\Mvc\Model\Resultset\Simple with an empty _result. (Which is logic, since it went and searched in the database...)
Therefore, my question is:
How can I get and validate the related fields before saving them?
Here is my (shortened) controller test:
$audioCategory = new AudioCategory();
$audioCategory->categoryId = 1;
$arAudioCategory[0] = $audioCategory;
$audioCategory = new AudioCategory();
$audioCategory->categoryId = 2;
$arAudioCategory[1] = $audioCategory;
$audio = new Audio();
[...other fields initialization...]
$audio->audiocategory = $arAudioCategory;
$audio->save();
Here is the (shortened) audio model:
namespace GRQ\Audio;
use GRQ\Validator\PresenceOfRelationValidator;
class Audio extends \Phalcon\Mvc\Model {
/**
* @Primary
* @Identity
* @Column(type="integer", nullable=false)
*/
public $id = 0;
/**
* @Column(type="integer", nullable=false)
*/
public $createdAt = 0;
[...other fields all reflecting the database...]
public function initialize() {
$this->setSource ( "audio" );
// table relationships
$this->hasMany ( "id", "GRQ\Audio\AudioCategory", "audioId", array(
'alias' => 'audiocategory'
) );
}
public function validation() {
[...other validations...]
$this->validate ( new PresenceOfRelationValidator ( array (
"field" => "audiocategory"
) ) );
return $this->validationHasFailed () != true;
}
}
Here is the (shortened) audio category model:
namespace GRQ\Audio;
class AudioCategory extends \Phalcon\Mvc\Model {
/**
* @Primary
* @Identity
* @Column(type="integer", nullable=false)
*/
public $id = 0;
/**
* @Column(type="integer", nullable=false)
*/
public $audioId = 0;
/**
* @Column(type="integer", nullable=false)
*/
public $categoryId = 0;
public function initialize(){
$this->setSource("audiocategory");
//table relationships
$this->belongsTo("audioId", "GRQ\Audio\Audio", "id", array(
'alias' => 'audio'
));
}
}
Here is my custom validator (which doesn't work and always returns false):
namespace GRQ\Validator;
use Phalcon\Mvc\Model\Validator;
use Phalcon\Mvc\Model\ValidatorInterface;
class PresenceOfRelationValidator extends Validator implements ValidatorInterface {
public function validate($model){
$field = $this->getOption('field');
$message = $this->getOption('message');
if (!$message) {
$message = 'The required relation '.$field.' was not found';
}
$value = $model->getRelated($field);
if (count($value) == 0) {
$this->appendMessage(
$message,
$field,
"PresenceOfRelation"
);
return false;
}
return true;
}
}
audioCategoryandaudio_category? - kbtz