0

我想通过与 KitchenImage 表的一对多关系来验证链接到实体的所有图像。

这是我的控制器功能:

public function addAction(Request $request)
    {
        // Check for POST
        if ($request->getMethod() == 'POST') {

            // Load Doctrine
            $em = $this->getDoctrine()->getManager();

            // Set Validator
            $validator = $this->get('validator');

            // Kitchen Details
            $kitchen = new Kitchen();
            $kitchen->setName($request->request->get('name'));
            $kitchen->setDescription($request->request->get('description'));
            $kitchen->setImage($request->files->get('main-image'));

            // Sub Images
            foreach ($request->files->get('sub-images') as $subimage)
            {
                $image = new KitchenImage();
                $image->setImage($subimage);
                $image->setKitchen($kitchen);
                $image->upload();
                $em->persist($image);
                $kitchen->addImage($image);
            }

            // Validation
            $errors = $validator->validate($kitchen);

            if (count($errors) > 0) {
                return new Response(print_r($errors, true));
            } 
            else {
                $kitchen->upload();
                $em->persist($kitchen);
                $em->flush();
                return new Response('The kitchen is valid! Yes!');
            }
        }

        return $this->render('PWDAdminBundle:Pages:add-kitchen.html.twig');
    }

厨房实体(删除了用于格式化的 setter 和 getter):

/**
 * @ORM\Entity
 * @ORM\Table(name="kitchen")
 */
class Kitchen
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     * @Assert\NotBlank()
     */
    protected $name;

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    protected $description;

    /**
     * @Assert\File(maxSize="6000000")
     * @Assert\NotNull()
     * @Assert\Image(
     *     minWidth = 800,
     *     maxWidth = 800,
     *     minHeight = 467,
     *     maxHeight = 467
     * )
     */
    protected $image;

    /**
     * @ORM\OneToMany(targetEntity="KitchenImage", mappedBy="kitchen")
     */
    protected $images;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    public $imagePath;

    public function __construct()
    {
        $this->images = new ArrayCollection();
    }

KitchenImage 实体:

class KitchenImage
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @Assert\File(maxSize="6000000")
     * @Assert\NotNull()
     * @Assert\Image(
     *     minWidth = 800,
     *     maxWidth = 800,
     *     minHeight = 467,
     *     maxHeight = 467
     * )
     */
    protected $image;

    /**
     * @ORM\ManyToOne(targetEntity="Kitchen", inversedBy="image")
     * @ORM\JoinColumn(name="kitchen_id", referencedColumnName="id")
     */
    protected $kitchen;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    public $path;

我在 Kitchen Entity 上尝试了以下操作:

/**
 * @ORM\OneToMany(targetEntity="KitchenImage", mappedBy="kitchen")
 * @Assert\All({
 *      @Assert\Valid
 * })
 */
protected $images;

但我收到错误消息:

"The constraint Valid cannot be nested inside constraint Symfony\Component\Validator\Constraints\All.
4

0 回答 0