Rodger
Rodger

Reputation: 1

Doctrine 2 Migrations Diff Won't Generate More Than One Foreign Key per Table

I am in the process of creating a simple blog with Symfony / Doctrine 2 and running into a problem with the php app/console doctrine:migrations:diff command.

Info:

I have tried the manyToOne declarations with and without the inversedBy part, the joinColumn part, etc. Below are my YML configs. With this config, a user_id foreign key will be created in the post table but no category id. (see bottom of the post config)

If anyone has any ideas I'd really appreciate it!

Post Entity config

Conduct\BlogBundle\Entity\Post:
type: entity
table: null

manyToOne:
        user:
            targetEntity: Acme\UserBundle\Entity\User
            inversedBy: posts
            joinColumn:
                name: user_id
                referencedColumnName: id
manyToOne:
    category:
            targetEntity: Conduct\BlogBundle\Entity\Category
            inversedBy: posts
            joinColumn:
                name: category_id
                referencedColumnName: id
lifecycleCallbacks: {  }

Category Entity config

Conduct\BlogBundle\Entity\Category:
type: entity
table: null
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    title:
        type: string
        length: 255
oneToMany:
        posts:
            targetEntity: Conduct\BlogBundle\Entity\Post
            mappedBy: category
lifecycleCallbacks: {  }

User Entity config

Acme\UserBundle\Entity\User:
type: entity
table: fos_user
id:
  id:
    type: integer
    generator:
      strategy: AUTO
oneToMany:
  posts:
    targetEntity: Conduct\BlogBundle\Entity\Post
    mappedBy: user

Category Entity Code

class Category
{


private $posts;

public function __construct()
{
    $this->posts = new ArrayCollection();
}
}

Post Entity Code

class Post
{
   protected $category;
}

Upvotes: 0

Views: 1087

Answers (2)

Rodger
Rodger

Reputation: 1

Finally! Found the problem.

When you have multiple many to one declarations they should be grouped together like this:

manyToOne:
    category:
         targetEntity: Conduct\BlogBundle\Entity\Category
         inversedBy: posts
         joinColumn:
             name: category_id
             referencedColumnName: id
    user:
        targetEntity: Acme\UserBundle\Entity\User
        inversedBy: posts
        joinColumn:
            name: user_id
            referencedColumnName: id

Upvotes: 0

Richard
Richard

Reputation: 4129

Category entity config:

oneToMany:
        posts:
            targetEntity: Post

Should be:

oneToMany:
        posts:
            targetEntity: Conduct\BlogBundle\Entity\Post

Similarly in post entity config:

manyToOne:
    category:
            targetEntity: Category

Should be:

manyToOne:
    category:
            targetEntity: Conduct\BlogBundle\Entity\Category

You should do similar in your other entity relations where you have not provided the full namespace (comments & postmeta)

Upvotes: 0

Related Questions