Phalcon:如何得到验证有关目前保存?
-
21-12-2019 - |
题
我有一个型号的音频和一个模型AudioCategory.
当我保存的声音目的,我想证实,至少有1audiocategory。
我创建了一个自定义验证程序。
我试图使用元音->getRelated()在验证,但它一直试图获取数据库中的信息。由于验证之前发生的节(这是伟大的),然后我收到一个空的名单,因此我的验证程序总是返回错误的。
当我打印的声音对象,而不保存,我可以看到我audiocategory领域_related的音频对象(print_r($音频);):
[_related:保护]=>阵列
(
[audiocategory]=>阵列
(
[0]=>GRQ\音频\AudioCategory Object([...])
[1]=>GRQ\音频\AudioCategory Object([...])
)
)
如果我尝试print$音频->audiocategory直接,我得到通知:
访问不确定性GRQ\音频\音::audiocategory
并没有什么是返回。
如果我呼叫$音频->getRelated(),我得到的一个目的类型Phalcon\软\型\结果集\简单的一个空_result.(这是逻辑的,因为它去搜索的数据库...)
因此,我的问题是:
我如何可以得到并核实有关领域之前拯救他们吗?
这里是我的(缩短)控制器试验:
$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();
这里是(缩短的)音模型:
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;
}
}
这里是(缩短)的音频类型:
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'
));
}
}
这是我的定义验证程序(其中不工作,并总是返回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;
}
}
解决方案
所以,我找到一种方式实现这一目标。不确定这是最好的方式,但它的工作:
由于价值得到保护,我不得不让他们从我的对象。
所以我创造了一个基本模型,从而延长自己说:
基本模型:
namespace GRQ;
class BaseModel extends \Phalcon\Mvc\Model {
/**
* This function should be used to get the data in the _related field directly.
* It is very useful if you need to validate the presence of a relation BEFORE saving in the database.
* To initialize the field with the database content, use $this->getRelated().
*/
public function getInternalRelated(){
return $this->_related;
}
}
然后我改变了我的音频类延长,从我的基本模型:
音频模型(简化):
namespace GRQ\Audio;
use Phalcon\Mvc\Model\Validator\Numericality;
use GRQ\Validator\MinValueValidator;
use GRQ\Validator\PresenceOfRelationValidator;
class Audio extends \GRQ\BaseModel {
/**
* @Primary
* @Identity
* @Column(type="integer", nullable=false)
*/
public $id = 0;
/**
* @Column(type="string", length=255, nullable=false)
*/
public $title = '';
public function initialize() {
$this->setSource ( "audio" );
// table relationships
$this->hasMany ( "id", "GRQ\Audio\AudioCategory", "audioId", array(
'alias' => 'audiocategory'
) );
}
public function validation() {
$this->validate ( new PresenceOfRelationValidator ( array (
"field" => "audiocategory"
) ) );
return $this->validationHasFailed () != true;
}
}
我AudioCategory模型(简化)留下的差不多相同的:
namespace GRQ\Audio;
use Phalcon\Mvc\Model\Message;
class AudioCategory extends \GRQ\BaseModel {
/**
* @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'
));
}
}
我现在使用验证程序的方法getInternalRelated到验证:
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->getInternalRelated();
if (count($value[$field]) == 0) {
$this->appendMessage(
$message,
$field,
"PresenceOfRelation"
);
return false;
}
return true;
}
}