Doctrine2 Many-To-One self-referecing : parent not generated - symfony

I'm using Symfony2 with doctrine and have no problems with relations except for one many-to-one/one-to-many self-referencing relation.
I have an entity Customer that can have zero, one or more entities (which are also Customer). When I generate the entities using 'doctrine:generate:entities BundleName' I only have a var '$entities' in my Entity Customer.php and there is no var '$mother_house'. Also the generated migration (using doctrine:migrations:diff) does not contain the creation a new field 'mother_house_id'.
The schema in Customer.orm.yml is this one :
Acme\Bundle\CustomerBundle\Entity\Customer:
type: entity
table: customer
repositoryClass: Acme\Bundle\CustomerBundle\Entity\CustomerRepository
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
company_name:
type: string
length: 255
reference:
type: string
length: '20'
created_at:
type: datetime
oneToMany:
entities:
targetEntity: Customer
mappedBy: mother_house
manyToOne:
mother_house:
targetEntity: Customer
inversedBy: entities
joinColumn:
mother_house_id:
referencedColumnName: id
manyToOne:
created_by:
targetEntity: Acme\Bundle\UserBundle\Entity\User
joinColumn:
created_by:
referencedColumnName: id
lifecycleCallbacks: { }

I've found my mistake.
All relations of the same type (many-to-one, one-to-many, etc.) must be grouped under one field 'manyToOne', 'oneToMany', etc.
So I just had to change
manyToOne:
mother_house:
targetEntity: Customer
inversedBy: entities
joinColumn:
mother_house_id:
referencedColumnName: id
manyToOne:
created_by:
targetEntity: Acme\Bundle\UserBundle\Entity\User
joinColumn:
created_by:
referencedColumnName: id
into
manyToOne:
mother_house:
targetEntity: Customer
inversedBy: entities
joinColumn:
mother_house_id:
referencedColumnName: id
created_by:
targetEntity: Acme\Bundle\UserBundle\Entity\User
joinColumn:
created_by:
referencedColumnName: id

Related

How to create a Doctrine2 Self-referencing Association with no hierarchy using YAML

I have an "article" entity and I want to create an association with "related" articles without any hierarchy. Simply to be able to store the fact that one article is related to multiple others.
I went through this reference but the many-to-many self reflecting had no example using YAML.
What would be the right way to do design it?
This is my current entity:
ContentBundle\Entity\Article:
type: entity
table: articles
repositoryClass: ContentBundle\Repository\ArticleRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
length: 255
content:
type: text
creationDate:
type: datetime
column: creation_date
updateDate:
type: datetime
column: update_date
state:
type: string
length: 255
options:
default: 'public'
oneToOne:
cover:
targetEntity: AppBundle\Entity\Image
joinColumn:
name: image_id
referencedColumnName: id
oneToMany:
notes:
targetEntity: AppBundle\Entity\Note
mappedBy: article
histories:
targetEntity: HistoricalEntry
mappedBy: article
comments:
targetEntity: AppBundle\Entity\Comment
mappedBy: article
secrets:
targetEntity: Secret
mappedBy: article
manyToOne:
author:
targetEntity: AppBundle\Entity\User
inversedBy: articles
joinColumn:
name: user_id
referencedColumnName: id
world:
targetEntity: WorldBundle\Entity\World
inversedBy: articles
joinColumn:
name: world_id
referencedColumnName: id
category:
targetEntity: WorldBundle\Entity\Category
inversedBy: articles
joinColumn:
name: category_id
referencedColumnName: id
manyToMany:
tags:
targetEntity: AppBundle\Entity\Tag
inversedBy: articles
joinTable:
name: articles_tags
joinColumns:
article_id:
referencedColumnName: id
inverseJoinColumns:
tag_id:
referencedColumnName: id
inheritanceType: JOINED
discriminatorColumn:
name: template
type: string
length: 50
discriminatorMap:
article: Article
person: Person
location: Location
organization: Organization
species: Species
options:
charset: utf8
type: InnoDB
lifecycleCallbacks: { }

Add a new column in table using .orm.yml configuration

I have a question for you. It's possible to add a new column for a table using .orm.yml configuration?
For example I have :
Shop\DesktopBundle\Entity\Order:
type: entity
repositoryClass: Shop\DesktopBundle\Repository\OrderRepository
table: order
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
order_adresse:
type: string
length: 255
order_date:
type: datetime
is_active:
type: boolean
nullable: true
created_at:
type: datetime
oneToMany:
Cart:
targetEntity: Cart
mappedBy: carts
manyToOne:
category:
targetEntity: Customer
inversedBy: orders
joinColumn:
name: customer_id
referencedColumnName: id
manyToMany:
categories:
targetEntity: Product
joinTable:
name: order_details
joinColumns:
order_id:
referencedColumnName: id
inverseJoinColumns:
product_id:
referencedColumnName: id
lifecycleCallbacks:
prePersist: [ setCreatedAtValue ]
So I tried to add the column "quantity" in relation many to many > order_details table
You tried to add quantity to the relation not to the table. Try this:
manyToMany:
categories:
targetEntity: Product
joinTable:
name: order_details
joinColumns:
order_id:
referencedColumnName: id
inverseJoinColumns:
product_id:
referencedColumnName: id
fields: # if you already have 'fields' append to it
quantity:
type: int
A good example of setting your entity using yml is here: http://doctrine-orm.readthedocs.org/en/latest/reference/yaml-mapping.html#example

Add default value to a manyToOne field on doctrine yml mapping

I'd need to know how to add a default value for a manyToOne field on my entity.orm.yml mapping in Symfony. This code is not working:
manyToOne:
usertype:
targetEntity: Usertype
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
UserType:
referencedColumnName: ID
orphanRemoval: false
options:
default: 1

Symfony doctrine manyToOne relationship with a composite id

In a Symfony2 app, I have two entites: Club and Member.
I have to use a composite primary keys with four specific fields (edition, distance, district and id). Here is the ORM yml. The Club schema is correctly generated.
MyBundle\Entity\Club:
type: entity
oneToMany:
members:
targetEntity: Member
mappedBy: club
table: null
id:
edition:
type: string
length: 4
distance:
type: string
length: 1
district:
type: integer
id:
type: integer
fields:
name:
type: string
length: 255
location:
type: string
length: 255
lifecycleCallbacks: { }
My problem is now to link the Member entity. Here is my current ORM yml:
MyBundle\Entity\Member:
type: entity
manyToOne:
club:
targetEntity: Club
inversedBy: members
joinColumn:
name: club_id
referencedColumnName: ??? What if composite PK ???
table: null
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
lastname:
type: string
length: 255
firstname:
type: string
length: 255
...
What to set as referencedColumnName? How to set up the reference with a composite FK?
EDIT
I found that is possible to set multiple join colums with annotation #joinColumns{#joinColumn...#joinColumn...}. Something similar for yml configuration? I can't find any example.
Try something like this, I think it's the correct YML syntax:
manyToOne:
club:
targetEntity: Club
inversedBy: members
joinColumns:
- joinColumn:
name: club_id
referencedColumnName: id
- joinColumn:
name: club_edition
referencedColumnName: edition
- joinColumn:
name: club_distance
referencedColumnName: distance
- joinColumn:
name: club_district
referencedColumnName: district
(However, for your continued sanity, I'd strongly recommend trying to avoid composite keys where possible!)

How create an extra field in an unidirectional many-to-many relation?

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?
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.
#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
`

Resources