0

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:

  • Postgresql
  • Relevant tables: Post, Category, fos_user
  • Post table should have 2 foreign keys: user_id and category_id
  • Diff only generates one foreign key in post table
  • If the manyToOne declaration for the user appears before the category id in post entity's doctrine config file, the post table gets a user_id foreign key but no category_id (when the diff runs)
  • If you move the manyToOne declaration for the category entity above that of the user entity, the post table gets a category_id but no user_id.

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;
}
Rodger
  • 1
  • 1

2 Answers2

0

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)

Richard
  • 4,079
  • 1
  • 14
  • 17
  • Thank you for your response! It seems that did not fix the problem, however. I think you only need to do that if the Post entity and the Category entity are not in the same bundle. In this case they are in the same bundle. – Rodger Aug 21 '15 at 02:07
0

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
Rodger
  • 1
  • 1