dflydev / dflydev-doctrine-orm-service-provider

Doctrine ORM Service Provider
MIT License
209 stars 59 forks source link

Question: Adding validation annotations in a Doctrine entity #45

Closed dsoms closed 9 years ago

dsoms commented 9 years ago

Hi!

I am trying to use vahlidation annotations in a Doctrine entity, but when I want to generate entities it fails. If I remove the validation annotations the entity and repository are generated correctly.

May be an config issue? Or isn't possible to use validation annotations in a Doctrine entity with Silex?

Thank you!

This is the code/configuration/output:

Entity with validation annotations

<?php

namespace Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\Email;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="Entity\UserRepository")
 */
class User
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $email
     *
     * @ORM\Column(name="email", type="string", length=255, unique=true)
     * @Assert\Email()
     */
    private $email;

    /**
     * @var string $password
     *
     * @ORM\Column(name="password", type="string", length=255)
     */
    private $password;

    /**
     * @var string $salt
     *
     * @ORM\Column(name="salt", type="string", length=255)
     */
    protected $salt;

    /**
     * @var array $roles
     * 
     * @ORM\Column(name="roles", type="array")
     */
    private $roles;

Doctrine ORM configuration

<?php

...

// Doctrine ORM
$app["orm.em.options"] = array(
    "mappings" => array(
        array(
            'type' => 'annotation',
            "namespace" => "Entity",
            'path' => __DIR__ .'/Entity',
            'use_simple_annotation_reader' => false
        ),
    ),
);

Console command

php bin/console orm:generate-entities src/

Output

[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\Email" in property Entity\User::$email does not exist, or could not be auto-loaded.
dominikzogg commented 9 years ago

Hello @dsoms

One things, is the error, it seems the annotation is not loadable. There are to common reasons for this. First the annotation class is not available (did you installed symfony/validatior), the second the annotation registry is missing.

https://github.com/dflydev/dflydev-doctrine-orm-service-provider#why-arent-my-annotations-classes-being-found

To get annotation validation running you need something like this:

https://github.com/saxulum/saxulum-validator-provider

Regards @dominikzogg

dsoms commented 9 years ago

Hello @dominikzogg

1- symfony/validatior is installed

composer.json

{
    "name": "fabpot/silex-skeleton",
    "description": "A pre-configured skeleton for the Silex microframework",
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.3.3",
        "silex/silex": "~1.0",
        "silex/web-profiler": "~1.0",
        "symfony/browser-kit": "~2.3",
        "symfony/class-loader": "~2.3",
        "symfony/config": "~2.3",
        "symfony/console": "~2.3",
        "symfony/css-selector": "~2.3",
        "symfony/debug": "~2.3",
        "symfony/finder": "~2.3",
        "symfony/form": "~2.3",
        "symfony/monolog-bridge": "~2.3",
        "symfony/process": "~2.3",
        "symfony/security": "~2.3",
        "symfony/translation": "~2.3",
        "symfony/twig-bridge": "~2.3",
        "symfony/validator": "~2.3",
        "doctrine/dbal": "2.*",
        "symfony/doctrine-bridge": "2.*",
        "dflydev/doctrine-orm-service-provider": "1.0.*@dev"
    },
    "autoload": {
        "psr-0": { "": "src/" }
    },
    "extra": {
        "branch-alias": {
            "dev-master": "1.1.x-dev"
        }
    }
}

2- I think the annotation registry is missing. I installed Silex with "fabpot/silex-skeleton". Which file I have to put this code?

<?php
$loader = require __DIR__ . '/../vendor/autoload.php';

\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

3- I can't install saxulum-validator-provider, my Pimple version is 1.1.1 and is required >=2.1,<4

Regards @dsoms

dominikzogg commented 9 years ago

Hello @dsoms

Adding the registry after require the $loader is perfect. Use the version ~1.0 of my validator, its for silex ~1.0

Regards dominikzogg

dominikzogg commented 9 years ago

If you really want to use the allready loaded doctrine bridge, take a look at: https://github.com/saxulum/saxulum-doctrine-orm-manager-registry-provider use the v2 its for silex ~1.0

dsoms commented 9 years ago

Hi @dominikzogg

If I install the version ~1.0 of your validator, then also need the above code or I need the both?

<?php
$loader = require __DIR__ . '/../vendor/autoload.php';

\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

Regards @dsoms

dominikzogg commented 9 years ago

@dsoms yes

dominikzogg commented 9 years ago

@dsoms you need it only ones, not ones foreach annotation using lib

dsoms commented 9 years ago

Hi @dominikzogg

It works! Thank you very much for your help!

Finally I put the code in the file "src/app.php".

Regards @dsoms

dominikzogg commented 9 years ago

@dsoms your welcome, would be cool if you close the issue

dsoms commented 9 years ago

@dominikzogg One more thing, if I like to have UniqueEntity Constraint Support within Symfony Validator Component, I need to install saxulum-doctrine-orm-manager-registry-provider and saxulum-validator-provider or only the first one?

dominikzogg commented 9 years ago

@dsoms you need both

dsoms commented 9 years ago

Hi @dominikzogg

It works too! Again, thank you very much for your help!

Issue closed.

Regards @dsoms