Monday, April 30, 2012

Joining JOINs

It has been suggested that I need to use a manyToMany approach to solve this problem.

http://readthedocs.org/docs/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-unidirectional-with-join-table

Here is the YAML from that page (with a corrected typo)
User:
  type: entity
  manyToMany:
    phonenumbers:
      targetEntity: Phonenumber
      joinTable:
        name: users_phonenumbers
        joinColumns:
          user_id:
            referencedColumnName: id
        inverseJoinColumns:
          phonenumber_id:
            referencedColumnName: id
            unique: true
I tried to do some conversion, but have run into some barriers, which I'll detail.  Its worth noting that this YAML does not work... now I'm trying to figure out if its meant to be stand alone, or if requires the other Entities to be declared on their own..

[thinks for a few minutes]

I am pretty sure they do need their own separate entities.

So we'll have Names and Matches, like normal, and this new entity is a Joining entity, so it just builds a bridge between the two.

So lets take it from the beginning.

Names should have {id} and {name},  Matches should have {id}, {name1} and {name2} or some permutation thereof.  

[types furiously]

Okay, so I have the following test YAML code:

ORM\Testing\Names:
  type: entity
  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
  manyToMany:
    tennis:
      targetEntity: Matches
      joinTable:
        name: names_to_matches
        joinColumns:
          name_id:
            referencedColumnName: character1
        inverseJoinColumns:
          matches_id:
            referencedColumnName: id
            unique: true
#
 works fine for generating entities and proxies, but when I try to generate the SQL schema I get:
Column name `character1` referenced for relation from ORM\Testing\Names towards ORM\Testing\Matches does not exist.
[still later]

Hm.  The example on the previously provided link seems to be to link just two unrelated fields.  Like if I had a firstNames entity and a lastNames entity, firstAndLast would use code much like in that example to link them together..

.... in progress...

No comments:

Post a Comment