-->

Annotations in extended Sonata user class not bein

2019-01-24 15:03发布

问题:

I extended the user bundle from the Sonata project with Sonata EasyExtends bundle. It is placed under src/Application/Sonata/UserBundle by default

Now I want to customize the extended class and add some fields. I notice though that annotations are not being processed, I need to define the mappings in src/Application/Sonata/UserBundle/Resources/config/doctrine/User.orm.xml

Is there any way to make use of the annotations instead of the XML file? I think it will solve a lot of my problems with referencing the user class, as now the command

php app/console doctrine:schema:update --force

doesnt seem to recognize the annotations..

回答1:

Delete Application/Sonata/UserBundle/Resources/config/doctrine at first

and later change Entity/User.php and Entity/Group.php to annotation type:

<?php

namespace Application\Sonata\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;

/**
 * User
 *
 * @ORM\Table(name="fos_user_user")
 * @ORM\Entity
 */

class User extends BaseUser
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=127)
     */
    protected $test;
}
<?php

namespace Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseGroup as BaseGroup;
use Doctrine\ORM\Mapping as ORM;

/**
 * Group
 *
 * @ORM\Table(name="fos_user_group")
 * @ORM\Entity
 */
class Group extends BaseGroup
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

}

In the end you should type: php app/console doctrine:schema:update --force and everything should works pretty.



回答2:

In resume, if you use XML, annotations does not rules.

If you delete config/doctrine folder, it will look for annotations, and there, you can put what you want.