user2143356
user2143356

Reputation: 5607

Simple one-to-one setup with Symfony2 / Doctrine2 and primary key thing

I'm confused as to all this.

I think Doctrine doesn't allow you to use a primary key on a join (I read it somewhere), but I see here http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/limitations-and-known-issues.html that it says "It is not possible to use join columns pointing to non-primary keys" which seems the opposite.

Anyway, I want to join primary keys, so surely I can do that? I am trying to set up a simple one-to-one relationship. This is what I have but I'm getting various errors that point to this problem.

I can get it set up by joining fields that aren't primary keys but that seems rubbish to me in the case I want to use it for.

I simply want an extra database table to extend the main database table. What's the best way to do this based on any limitations?

I'm using Doctrine 2.2 but can use any version.

The code below is what I would expect to work, but it doesn't allow it.

--

User:

user_id # primary
field_1
field_2

Extra User Info:

extra_id # primary
extra_info_1
extra_info_2

User.orm.yml:

User:
    type: entity
    table: null
    fields:
        user_id:
            id: true
            generator:
                strategy: AUTO
        field_1:
            type: string
        field_2:
            type: string

    oneToOne:
        user_id:
            targetEntity: UserExtra
            mappedBy: extra_id

UserExtra.orm.yml:

UserExtra:
    type: entity
    table: null
    fields:
        extra_id:
            id: true
            generator:
                strategy: AUTO
        extra_info_1:
            type: string
        extra_info_2:
            type: string

    oneToOne:
        extra_id:
            targetEntity: User
            mappedBy: user_id

Upvotes: 0

Views: 679

Answers (1)

1ed
1ed

Reputation: 3668

Your schema is incorrect. The id should be under the id key not under fields and you should never ever write somethig_id in an entity. You bind objects together by reference and not database ids. Please read through the documentation carefully. It is possible to use a primary key as a foreing key in a related entity since Doctrine 2.1: identity-through-foreign-entities

User:
  type: entity
  id:
    id:
      type: integer
      generator:
        strategy: AUTO

Address:
  type: entity
  id:
    user:
      associationKey: true
  oneToOne:
    user:
      targetEntity: User

Upvotes: 1

Related Questions