0

I found this response: https://stackoverflow.com/a/9135093/620095, but I wouldn't like to use a new class. I'm looking for the best way (elegant and straightforward).

Here is the 'Tag' entity:

MyApp\CoreBundle\Entity\Tag:
    type: entity
    table: tag
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 255
            nullable: false

The 'Post' entity:

MyApp\PostBundle\Entity\Post:
    type: entity
    table: post
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        title:
            type: string
            length: 255
            nullable: false
        content:
            type: text
            nullable: false
    manyToMany:
        Tags:
            targetEntity: MyApp\CoreBundle\Entity\Tag
            joinTable:
                name: post_tag
                joinColumns:
                    post_id:
                        referencedColumnName: id
                inverseJoinColumns:
                    tag_id:
                        referencedColumnName: id

The 'Event' entity:

MyApp\EventBundle\Entity\Event:
    type: entity
    table: event
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        title:
            type: string
            length: 255
            nullable: false
        content:
            type: text
            nullable: false
    manyToMany:
        Tags:
            targetEntity: MyApp\CoreBundle\Entity\Tag
            joinTable:
                name: event_tag
                joinColumns:
                    event_id:
                        referencedColumnName: id
                inverseJoinColumns:
                    tag_id:
                        referencedColumnName: id

Suppose I want a 'description' field in the 'event_tag' table. What should I do?

UPDATE: If is impossible, how would stay my three yml to add the quoted field?

Community
  • 1
  • 1
cbacelar
  • 545
  • 7
  • 20

2 Answers2

0

Sorry, you have to create a new entity.

You have to considere this entity as an individual symfony entity

For example:

Product >- Stock -< Store
Product >- Bill -< User
...

And not as a link between two others entities.

Vincent Barrault
  • 2,906
  • 1
  • 19
  • 17
  • I suspect not. It's just the way Doctrine 2 works. I suppose you could switch to a different ORM but I don't of any that supports what you want. It's not that big of a deal to replace a ManytoMany with two ManyToOne. – Cerad Feb 11 '14 at 19:17
0

@cbacelar "I updated the question. Can you help me?" => Event-Tag relationship could be sth like:

In Event entity, change manyToMany by oneToMany:

  oneToMany:
        EventTags:
            targetEntity: MyApp\EventBundle\Entity\EventTag
            mappedBy: Events

In Tag entity, change manyToMany by oneToMany:

   oneToMany: 
        EventTags:
            targetEntity: MyApp\EventBundle\Entity\EventTag
            mappedBy: Tags

Create MyApp\CoreBundle\Entity\EventTag entity:

type: entity
table: event_tag
id:
    id:
        type: integer
        generator: { strategy: AUTO }
fields:
    description:
        type: text
        nullable: false
manyToOne:
    Events:
        targetEntity: MyApp\EventBundle\Entity\Event
        inversedBy: EventTags
        joinColumn:
           name: event_id
           referencedColumnName: id
    Tags:
        targetEntity: MyApp\CoreBundle\Entity\Tag
        inversedBy: EventTags
        joinColumn:
           name: tag_id
           referencedColumnName: id

`

jdharandas
  • 416
  • 4
  • 7
  • ok... but when i generated the crud for 'event' entity, the field 'tags' doesnt appears. how embbed the form? – cbacelar Feb 13 '14 at 13:02
  • I have not done things like that in forms, but I can give you a few ideas: 1) see http://www.prowebdev.us/2012/07/symfnoy2-many-to-many-relation-with.html 2) try creating a not mapped field with tags entities (and a query builder, see http://symfony.com/doc/current/reference/forms/types/entity.html) and then adding your EventTag entity in the pre/post submit: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html Later, if I have a few minutes, I'll think about that and I'll give you a better answer. There might be a better way. – jdharandas Feb 13 '14 at 15:08
  • did you find your answer ? check also: http://symfony.com/doc/current/cookbook/form/form_collections.html – jdharandas Feb 19 '14 at 16:14