开发者

OneToMany/ManyToOne SchemaException

I try to build a manyToOne relation for my Symfony2 application with Doctrine2. I get this error and I don't know why:

app/console doctrine:schema:create
PHP Deprecated:  Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/mcrypt.ini on line 1 in Unknown on line 0
ATTENTION: This operation should not be executed in an production enviroment.


  [Doctrine\DBAL\Schema\SchemaException]
  There is no column with name 'activityGroup' on table 'activity'.  

Those are the two classes: http://pastebin.com/Ev7Rwgxr

I think there actually IS activityGroup on the Activity class... so what does that error try to say?

Thanks开发者_如何转开发!


I got it...

the uniqueconstraints expect the real db field name which is activityGroup_id and not just activityGroup.

One can make sure what the field is called in the DB, by providing the JoinColumn.

So, an smart solution is:

    /**
     * @ORM\Entity
     * @ORM\Table(name="activity",
     *     uniqueConstraints={
     *         @ORM\UniqueConstraint(name="name_idx", columns={"activity_group_id", "name"}),
     *         @ORM\UniqueConstraint(name="sort_idx", columns={"activity_group_id", "sort_id"})
     *     }
     * )
     */
    class Activity
    {
        // ...

        /**
         * @ORM\ManyToOne(targetEntity="SeduceMe\SiteBundle\Entity\ActivityGroup", inversedBy="activities")
         * @ORM\JoinColumn(name="activity_group_id", referencedColumnName="id")
         */

        protected $activityGroup;
        //...
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜