Symfony2/Doctrine manyToOne-relationship does not appear in tables - symfony

I'm working with a Doctrine schema, but ran into trouble because one of the manyToOne-relationships won't persist in the database. I am clueless why this is the case, because to my eye, the syntax looks correct.
Can anyone identify the problem?
Below is my schema in yaml. There are no tables relating these two entities in my mysql-database after running
php app\console doctrine:schema:update --force.
Me\MyBundle\Entity\FreeTextField:
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
name:
type: string
flagPrivate:
type: boolean
description:
type: text
nullable: TRUE
oneToMany:
entries:
targetEntity: FreeTextEntry
mappedBy: xfield
lifecycleCallbacks: { }
Me\MyBundle\Entity\FreeTextEntry:
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
content:
type: text
manyToOne:
xfield:
targetEntity: FreeTextField
inversedBy: entries
manyToOne:
registration:
targetEntity: Registration
inversedBy: freeTextEntries
lifecycleCallbacks: { }

Propably the same problem as here. You need to put all the manyToOne -type associations under the same type declaration in Entity\FreeTextEntry, like so:
manyToOne:
xfield:
targetEntity: FreeTextField
inversedBy: entries
registration:
targetEntity: Registration
inversedBy: freeTextEntries

Related

How generate get/set functions for related collections

I have some yml-configs:
#Story.orm.yml
pv\MyBundle\Entity\Story:
type: entity
table: story
fields:
id:
id: true
type: integer
generator:
strategy: IDENTITY
And
pv\MyBundle\Entity\News:
type: entity
table: news
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
column: news_id
generator:
strategy: IDENTITY
manyToOne:
story:
targetEntity: Story
joinColumn:
name: story_id
referencedColumnName: id
I want to add function getNews to Story for do something like $story_entity->getNews()
I had corrected oneToOne-relation:
manyToOne:
story:
targetEntity: Story
inversedBy: news
joinColumn:
name: story_id
referencedColumnName: id
But when I try to run command
php app/console doctrine:generate:entities --path=src/ MyBundle:Story
symfony doesn't update News entity and doesn't create function getNews()
How to generate this function? Maybe I make some errors?

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
`

SonataDoctrineORMAdminBundle throws array_combine error

I have a many-to many relation which is throwing a warning:
Warning: array_combine(): Both parameters should have an equal number of elements in vendor/sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Model/ModelManager.php line 179
The admin class in question was working fine until updated SonataDoctrineORMAdminBundle to version 2.2.4 with composer.
I think that the problem may be on my models but not sure what it is.
bundle\Entity\EntityOne:
type: entity
table: entityOne
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
title:
type: string
length: '100'
oneToMany:
entityRel:
targetEntity: EntityRel
mappedBy: entityOne
cascade: ["persist", "remove"]
lifecycleCallbacks: { }
bundle\Entity\EntityRel:
type: entity
table: entityRel
id:
entityOne:
associationKey: true
entityTwo:
associationKey: true
fields:
amount:
type: decimal
oneToOne:
entityOne:
targetEntity: EntityOne
entityTwo:
targetEntity: EntityTwo
lifecycleCallbacks: { }
bundle\Entity\EntityTwo:
type: entity
table: entityTwo
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
name:
type: string
length: '100'
oneToMany:
entityRel:
targetEntity: entityRel
mappedBy: entityTwo
cascade: ["persist", "remove"]
lifecycleCallbacks: { }
The idea is that EntityRel connects EntityOne and EntityTwo with an amount field. Each connection between the two tables must be unique thus enforcing the key to be composite.
Any idea?
It seems like you miss the EntityTwo's table:
bundle\Entity\EntityTwo:
type: entity
table: entityTwo

Failed on insert entity with many related entities

This is my first post because until now I had always found solution with your answers, until now...
I have three Entities (Bike, Category and Extra) that have some field translatable. I know there is a Translatable Extension in symfony but I discovered a bit late and I have my solution always working.
The thing is I have then 3 other tables (BikeI18N, CategoryI18N and ExtraI18N). These table link with their associated with a column bike_id (for example). Here you have the schema of Bike and Category and their I18N's tables:
Bike:
BEM\BikeBundle\Entity\Bike:
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
marca:
type: string
length: 255
model:
type: string
length: 255
preu1Dia:
type: float
column: preu_1dia
[...]
manyToOne:
category:
targetEntity: BEM\CategoryBundle\Entity\Category
inversedBy: bikes
oneToMany:
translation:
targetEntity: BikeI18N
mappedBy: bike
cascade: [persist]
pedals:
targetEntity: Pedal
mappedBy: bike
talles:
targetEntity: Talla
mappedBy: bike
manyToMany:
accessoris:
targetEntity: Accessori
inversedBy: bikes
lifecycleCallbacks: { }
BikeI18N:
BEM\BikeBundle\Entity\BikeI18N:
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
descripcio:
type: text
locale:
type: string
length: '2'
manyToOne:
bike:
targetEntity: Bike
inversedBy: translation
joinColumn:
bike_id:
referencedColumnName: id
lifecycleCallbacks: { }
Category:
BEM\CategoryBundle\Entity\Category:
type: entity
table: null
repositoryClass: BEM\CategoryBundle\Repository\CategoryRepository
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
oneToMany:
translation:
targetEntity: CategoryI18N
mappedBy: category
cascade: [persist]
bikes:
targetEntity: BEM\BikeBundle\Entity\Bike
mappedBy: category
lifecycleCallbacks: { }
and finally CategoryI18N:
BEM\CategoryBundle\Entity\CategoryI18N:
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
categoryId:
type: integer
column: category_id
locale:
type: string
length: '2'
translation:
type: string
length: 255
manyToOne:
category:
targetEntity: Category
inversedBy: translation
joinColumn:
category_id:
referencedColumnName: id
lifecycleCallbacks: { }
In my "new" form I have a Category form with 4 embeded CategoryI18N forms. It works properly. The problem is the Bike and BikeI18N does not even if I did the same.
If I write $translation->setAccessori($this); in Bike::addTranslation(BikeI18N $translation) I get the error Entities passed to the choice field must be managed. Maybe persist them in the entity manager?.
If I don't write $translation->setAccessori($this); the form is saved but the relation is lost or, if I set the DB to not_nullable bike_id I get an error.
The most surprising thing that makes no sense to me is that I have setCategory($this) inside the addTranslation of the class Category...any ideas?
I resolved the issue.
The think was in my BikeI18N form builder.
class BikeI18NType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('descripcio')
->add('locale', 'hidden')
//->add('bike', 'hidden')
;
}
}
When I commented out the bike additions the problem was solved.
Thanks for reading!

Resources