Phalcon:の取得方法/検証関連オブジェクトの前に。
-
21-12-2019 - |
質問
いモデルオーディオとモデルAudioCategory.
私が存のオーディオオブジェクトのだったのを検証することが少なくとも1audiocategoryになっています。
作成したカスタムバリデータです。
を使ってみましたドルのオーディオ->getRelated()のバリデータであり続けを取得するデータベースの情報での検証が発生前の保存(大きい)を受けてい空のリストが私のvalidatorは常にfalseを返します。
私が印刷、オーディオオブジェクトが保存されず、自分で見てもaudiocategoryの_relatedのオーディオオブジェクト(print_r($オーディオ);):
[_related:protected]=>Array
(
[audiocategory]=>Array
(
[0]=>GRQ\オーディオ\AudioCategoryオブジェクト([...])
[1]=>GRQ\オーディオ\AudioCategoryオブジェクト([...])
)
)
いうエンジンマネージャをエンジンオーディオ->audiocategoryを直接取得しませ:
アクセスを未定義の件GRQ\オーディオ\オーディオ::audiocategory
ものが返されます。
場合に呼びかけ$オーディオ->getRelated()を取得しま型のオブジェクトPhalcon\Mvc\モデル esultset\簡単な空_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'
));
}
}
私のValidatorにおけるその使用方法に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;
}
}