开发者

Symfony 2: Doctrine can't create relationship

开发者 https://www.devze.com 2023-03-04 17:24 出处:网络
I\'m very new to Symfony 2.0 and doctrine. I have state and customer entity in different bundle. I just want to add relation between state and customer. I\'m coded state and customer entities. Here is

I'm very new to Symfony 2.0 and doctrine. I have state and customer entity in different bundle. I just want to add relation between state and customer. I'm coded state and customer entities. Here is开发者_StackOverflow中文版 the my code:

/**
 * @orm:Entity
 */
class Customer
{
    /**
     * @orm:Id
     * @orm:Column(type="integer")
     * @orm:GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @OneToOne(targetEntity="State")
     * @JoinColumn(name="state_id", referencedColumnName="id")
     */
    protected $state;

}

/**
 * @orm:Entity
 */
class State
{
    /**
     * @orm:Id
     * @orm:Column(type="integer")
     * @orm:GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @orm:Column(type="string", length="50")
     */
    protected $name;
}

And my config file:

doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        mappings:
            FogCustomerBundle: { type: annotation, dir: Entity/ }
            FogMainBundle: { type: annotation, dir: Entity/ }

So my problem is when I generate schema using php app/console doctrine:schema:create command tables are generated. But relationship doesn't generated /state column doesn't generead in customer table/. Why? I don't have any idea? I'll very happy for every advise and post.


You can run into that problem if you're closely following examples from the Doctrine2 documentation, because Symfony2 places all the Doctrine2 annotations into the orm namespace, which you seem to be missing on your OneToOne and JoinColumn annotations. Your code for the $state property should look like this:

/**
 * @orm:OneToOne(targetEntity="State")
 * @orm:JoinColumn(name="state_id", referencedColumnName="id")
 */
protected $state;

EDIT: With changes introduced in Symfony2 beta2, annotations have changed a little. Annotations need to be imported before they are used; importing Doctrine looks like this:

use Doctrine\ORM\Mapping as ORM;

Then the new usage looks like this:

/**
 * @ORM\OneToOne(targetEntity="State")
 * @ORM\JoinColumn(name="state_id", referencedColumnName="id")
 */
protected $state;

There is some discussion of further changes to the annotation system; if these changes are rolled out, I'll be back with another edit.

0

精彩评论

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

关注公众号