1

What's the best way to check if a record exist before persist a form ?

  • Use Pre Perist ? But can I use query
  • Use a query in the controller and if exist throw exception
  • Use repository of my class

I red this https://stackoverflow.com/a/10688065/3942705 for querying in php/mysql but I would like to do that with symfony.

Benjamin Lucas
  • 370
  • 5
  • 20
  • It depends on what is the purpose of you checking that. If you have unique columns, you can apply validations etc. – stevenll Apr 02 '15 at 11:31
  • Let's say that I've an image position field (1,2,3,4,...) in DB corresponding to the position of an image for a product. I can't use unique because I can have two position 1 for different product. So I want to check if for product "n", position given for a new image is different. If nok than, I give an error message. I hope my explanation are more precise. – Benjamin Lucas Apr 02 '15 at 13:08
  • So One `Product` can have Many `Image` entities? And you want to make sure that the images positions inside a product are only taken once? – stevenll Apr 02 '15 at 16:04
  • Indeed, ONE Product can have MANY Images. The images position must be different/unique for this Product. So if I already have an image in DB with position 2 and I add a new one, this position must be different of 2. Thank you for your advice. – Benjamin Lucas Apr 03 '15 at 07:23

2 Answers2

1

I am going to post a solution, this may not be the best performance scenario, but it works and you can further optimize it.

Supposed: AcmeBundle is your bundle namespace, Product and Media are your entities. You need to add a validation for your products, I have used YML, you can change this.

At: AcmeBundle\Resources\config\validation.yml

AcmeBundle\Entity\Product:
    constraints:
        - Callback: { methods: [validate] }

At: AcmeBundle\Entity\Product

class Product
{
    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $media;

    [...]

    /**
     * @param ExecutionContextInterface $context
     */
    public function validate(ExecutionContextInterface $context)
    {
        $allowedPositions = array(1, 2, 3, 4);

        foreach ($allowedPositions as $position) {
            $atThisPosition = $this->media->filter(function(Media $media) {
                return $media->getPosition() === $position;
            });

            $count = $atThisPosition->count();

            if ($count > 1) {
                $context->addViolationAt('media', sprintf("Trying to set %d media at position %d", $count, $position));
            }
        }
    }
}
stevenll
  • 1,025
  • 12
  • 29
0

Ok, if you want that your record will be unique then you can use entity validation to check whether the record is unique or not. Suppose you have a User entity. You want that if the user's email or username is already exist then the user won't be able to persist on db, it can be done by checking the email and username property whether these are already exist or not, that means those are unique or not. For detail information about entity validation you can see the following link: http://symfony.com/doc/current/book/validation.html

More specifically if you want to learn about UniqueEntity validaiton you can see this link: http://symfony.com/doc/current/reference/constraints/UniqueEntity.html#basic-usage

Best of luck

Imran
  • 4,582
  • 2
  • 18
  • 37