5

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.

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for reply. According to your instruction I'm changed all '@orm' annotations to 'orm'. Then run 'code'php app/console doctrine:schema:create'code' command and get 'code'No Metadata Classes to process.'code' message. Tables are don't generated. Any idea?
Can you update your question with your new entities (ie replace the existing example with what they look like now)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.