Add default value to a manyToOne field on doctrine yml mapping - symfony

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

Related

Doctrine ManyToMany Relation 'The table with name already exists

I have Entity Party. Shortly it looks like this:
AppBundle\Entity\Main\Party:
type: entity
table: main.party
id:
id:
type: integer
nullable: false
id: true
generator:
strategy: IDENTITY
fields:
legalName:
type: string
nullable: false
length: 255
options:
fixed: false
column: legal_name
manyToMany:
partiesForPartner:
targetEntity: AppBundle\Entity\Main\Party
mappedBy: partnersForParty
fetch: EXTRA_LAZY
cache:
usage: NONSTRICT_READ_WRITE
partnersForParty:
cascade: ["all"]
targetEntity: AppBundle\Entity\Main\Party
inversedBy: partiesForPartner
fetch: LAZY
joinTable:
name: main.party_partnership
joinColumns:
party_id:
referencedColumnName: id
inverseJoinColumns:
partner_id:
referencedColumnName: id
Also there is one more entity PartyPartnership. which includes some additional props.
AppBundle\Entity\Main\PartyPartnership:
type: entity
repositoryClass: AppBundle\Repository\Main\PartyPartnershipRepository
table: main.party_partnership
uniqueConstraints:
party_partnership_ukey:
columns:
- party_id
- partner_id
id:
id:
type: integer
nullable: false
options:
unsigned: false
id: true
generator:
strategy: IDENTITY
fields:
codePartyByPartner:
type: string
nullable: true
length: 35
options:
fixed: false
column: code_party_by_partner
codePartnerByParty:
type: string
nullable: true
length: 35
options:
fixed: false
column: code_partner_by_party
manyToOne:
party:
targetEntity: AppBundle\Entity\Main\Party
cascade: { }
fetch: LAZY
mappedBy: null
inversedBy: null
joinColumns:
party_id:
referencedColumnName: id
orphanRemoval: false
partner:
targetEntity: AppBundle\Entity\Main\Party
cascade: { }
fetch: LAZY
mappedBy: null
inversedBy: null
joinColumns:
partner_id:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
All this mapping was imported from DB except of many-to-many association in Party. Now when i try to start doctrine commands like 'doctrine:schema:validate' or 'doctrine:schema:update'. I get an error 'The table with name 'main.party_partnership' already exists.'
Symfony ManyToMany creates a table between the two entities linked.
It means it automatically create the table main.party_partnership between party and partnership.
Party ---(ManyToMany main.party_partnership)---> Partnership
You just need to reference as oneToMany, and remove the manyToMany
Party ---(OneToMany)---> PartyPartnership
Partnership ---(OneToMany)---> PartyPartnership
What worked for me after several hours: clear cache.

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
`

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!

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

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

Symfony2/Doctrine manyToOne-relationship does not appear in tables

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

Resources