Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2: Doctrine can't create relationship

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 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.

like image 320
Zeck Avatar asked May 10 '11 13:05

Zeck


1 Answers

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.

like image 57
Derek Stobbe Avatar answered Nov 14 '22 01:11

Derek Stobbe