Skip to content

Symfony 2 UniqueEntity with two associations is not working. #6727

Closed
Sukhrob opened this Issue Jan 14, 2013 · 13 comments

5 participants

@Sukhrob
Sukhrob commented Jan 14, 2013
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @UniqueEntity({"field1", "field2"})
 * @ORM\Table(name = "myentity")
 */
class MyEntity
{
    /**
     * @ORM\ManyToOne(.....)
     * @ORM\JoinColumn(.....)
     */
     protected $field1;

   /**
    * @ORM\ManyToOne(.....)
    * @ORM\JoinColumn(.....)
    */
    protected $field2;
}

It works with non-association fields, though. I am using Symfony 2.1.4 and Doctrine (>=2.2.3,<2.4-dev). Can anyone help? Thanks.

@Sukhrob
Sukhrob commented Jan 18, 2013

I suppose it is not implemented yet. Is that right?

@xphere
xphere commented Mar 8, 2013

Is this bug still around? Can you explain exactly the meaning of "is not working"?

@sp0ken
sp0ken commented Apr 19, 2013

I can confirm that the bug is still in Symfony 2.2.1.
When specifying a @UniqueEntity() with two associations as fields the validation does nothing. To be more precise, Apache process takes 100% of the CPU and the request times out with no message or info after around 25s.

@Sukhrob
Sukhrob commented Apr 19, 2013

@xphere Well, I think you got the answer from @sp0ken.

@xphere
xphere commented Apr 19, 2013

@Sukhrob @sp0ken having a minimal project that reproduces the bug would help, I'll try to do that and see what happens

@sp0ken
sp0ken commented Apr 19, 2013

Here is my entity with the failing UniqueEntity if it helps. And I'm manually validating it in my controller (ajax request).


namespace Jacotte\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use Jacotte\ProductBundle\Validator\Constraints as JacotteAssert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * Like
 *
 * @ORM\Table(name="Liked", uniqueConstraints={
 *     @ORM\UniqueConstraint(columns={ "user_id", "product_id"}),
 *     @ORM\UniqueConstraint(columns={ "user_id", "comment_id"}),
 *     @ORM\UniqueConstraint(columns={ "user_id", "photo_id"}),
 *     @ORM\UniqueConstraint(columns={ "user_id", "video_id"})
 * })
 * @ORM\Entity(repositoryClass="Jacotte\UserBundle\Entity\LikeRepository")
 * @ORM\UniqueEntity(fields={"user", "product"})
 */
class Like
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * Liked product
     * @var \Jacotte\ProductBundle\Entity\Product $product
     * 
     * @ORM\ManyToOne(targetEntity="\Jacotte\ProductBundle\Entity\Product", inversedBy="likes")
     */
    private $product;

    /**
     * Liked comment
     * @var \Jacotte\ProductBundle\Entity\Comment $comment
     * 
     * @ORM\ManyToOne(targetEntity="\Jacotte\UserBundle\Entity\Comment", inversedBy="likes")
     */
    private $comment;

    /**
     * Liked video
     * @var \Jacotte\MediaBundle\Entity\Video $video
     * 
     * @ORM\ManyToOne(targetEntity="\Jacotte\MediaBundle\Entity\Video", inversedBy="likes")
     */
    private $video;

    /**
     * Liked Photo
     * @var \Jacotte\MediaBundle\Entity\Photo $Photo
     * 
     * @ORM\ManyToOne(targetEntity="\Jacotte\MediaBundle\Entity\Photo", inversedBy="likes")
     */
    private $photo;

    /**
     * Reviewer
     * @var \Jacotte\UserBundle\Entity\User $user
     * 
     * @ORM\ManyToOne(targetEntity="\Jacotte\UserBundle\Entity\User", inversedBy="likes")
     */
    private $user;

    /**
     * Date of creation of this comment
     * @var datetime $created_at
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(type="datetime")
     */
    private $created_at;

    /**
     * Date of the last update of this comment
     * @var datetime $updated_at
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(type="datetime")
     */
    private $updated_at;

And here is part of the controller :

$like = new Like();
$like->setUser($this->getUser());
$like->setProduct($product);

$validator = $this->get('validator');
$errorList = $validator->validate($product);
@Sukhrob
Sukhrob commented Apr 19, 2013

@sp0ken Why your fields are private? They should be protected, I think. It was written in doctrine's documentation. Try to use protected. Maybe, it works. But, it didn't work in 2.1.4 as I remember.

@sp0ken
sp0ken commented Apr 19, 2013

They're set on private by default when using symfony console to generate entities. No luck however when making them protected.

@Sukhrob
Sukhrob commented Apr 19, 2013

Well, I remember that they were protected by default before 2.1.4. Well, it seems it has changed in 2.2.

@stof
Symfony member
stof commented Apr 19, 2013

It has nothing to do with Symfony 2.1 or 2.2. The EntityGenerator is not part of Symfony but of Doctrine

@xphere
xphere commented Apr 20, 2013

@sp0ken @Sukhrob I'm unable to reproduce the buggy behaviour in 2.2.1 nor in 2.1.4
I'm using this simpler case: https://gist.github.com/xphere/5425313
Could you try it?

@sp0ken
sp0ken commented Apr 22, 2013

It's now working for me, but I'm not sure what really changed as it is also working with my previous code. Anyways thanks @xphere

@fabpot fabpot closed this Apr 22, 2013
@xphere
xphere commented Apr 22, 2013

@sp0ken Glad it worked now, I hope it does too for @Sukhrob

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.