私はREST APIを書いています.私の問題は、他のエンティティと1対多の関係を持つエンティティでリクエストをデシリアライズしたいときです。新しいものを作成し、彼をオブジェクトに割り当てます。
投稿例は次のとおりです。
{"カテゴリ": {"ID": 1}, {"テーマ": {"ID": 1} }
私が期待しているのは、category:1 と theme:1 で新しいチャンネルを追加することですが、代わりに doctrine が新しいカテゴリー/テーマを作成します。
私がやりたかったのは、JMS Serializer でデシリアライズして作成したカテゴリ/テーマ オブジェクトを、カスタム バリデータ内の Doctrine オブジェクトに変更することです。
class Channel
{
/**
* @ChoiceEntity(targetEntity="Bundle\ChannelBundle\Entity\ChannelCategory", allowNull=true)
* @Type("Bundle\ChannelBundle\Entity\ChannelCategory")
*/
public $category;
/**
* @ChoiceEntity(targetEntity="Bundle\ChannelBundle\Entity\Theme", allowNull=true)
* @Type("Bundle\ChannelBundle\Entity\Theme")
*/
public $theme;
}
そしてここでカスタムバリデータ:
class ChoiceEntityValidator extends ConstraintValidator
{
/**
*
* @var type
*/
private $entityManager;
/**
*
* @param type $entityManager
*/
public function __construct($entityManager){
$this->entityManager = $entityManager;
}
/**
* @param FormEvent $event
*/
public function validate($object, Constraint $constraint)
{
if($constraint->getAllowNull() === TRUE && $object === NULL){//null allowed, and value is null
return;
}
if($object === NULL || !is_object($object)) {
return $this->context->addViolation($constraint->message);
}
if(!$this->entityManager->getRepository($constraint->getTargetEntity())->findOneById($object->getId())) {
$this->context->addViolation($constraint->message);
}
}
}
リポジトリの結果の値を使用して、カスタムバリデータから $object を変更する方法はありますか?