doctrine create manyToMany over third entity does not work properly - symfony

I want to creta a manyToMany relation between two entites not directly but by using a third entity with double manyToOne relations:
AppBundle\Entity\AttributeKey:
type: entity
table: attribute_keys
repositoryClass: AppBundle\Repository\AttributeKeyRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
label:
type: string
length: 255
unique: true
sort:
type: integer
AppBundle\Entity\AttributeValue:
type: entity
table: attribute_values
repositoryClass: AppBundle\Repository\AttributeValueRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
label:
type: string
length: 255
unique: true
AppBundle\Entity\AttributeKeyValue:
type: entity
table: attribute_keys_values
repositoryClass: AppBundle\Repository\AttributeKeyValueRepository
manyToOne:
attributeKey:
targetEntity: AttributeKey
attributeValue:
targetEntity: AttributeValue
manyToMany:
documents:
targetEntity: Document
mappedBy: attributes
id:
id:
type: integer
id: true
generator:
strategy: AUTO
In the database everything looks fine, the foreign keys are properly built. But the AttributeKey Entity that is generated by doctrine does not contain a method getValues() and AttributeValue does not have the method getKeys() or addKey()
What did I do wrong?

You mapped only the party that owns the relationship. You need to set up a two-way relationship.
Everything is explained here :
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional
and
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-bidirectional
That's all :)

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?

Symfony2 > Doctrine > double oneToMany combined key

I'm trying to setup the relation between 'Products' and 'Merchants' through a linking table 'MerchantProduct' where I can store/override additional information. So the primary key consists of a combined key. I've tried to simplify the amount of fields per Entity to enhance the readability. The three Entities already worked fine before I tried to add the relations in YML, but I would like to go 'all the way' with Doctrine.
The error I get is:
[Doctrine\ORM\ORMException]
Column name id referenced for relation from
BLAAT\Bundle\AdminBundle\Entity\Core\Product towards
BLAAT\Bundle\AdminBundle\Entity\Core\MerchantProduct does not exist.
I'm not really sure why I get this error and don't know how to solve it. I'm afraid it has to do with the combined primary key.
Linking-entity with extra override information
BLAAT\Bundle\AdminBundle\Entity\Core\MerchantProduct:
type: entity
table: MerchantProduct
repositoryClass: BLAAT\Bundle\AdminBundle\Entity\Core\MerchantProductRepository
id:
merchantId:
type: integer
column: merchant_id
productId:
type: string
length: 255
column: product_id
fields:
inStock:
type: boolean
column: in_stock
nullable: TRUE
productPrice:
type: float
column: product_price
nullable: TRUE
productDescription:
type: text
column: product_description
nullable: TRUE
oneToMany:
products:
targetEntity: Product
mappedBy: product_id
oneToMany:
outlets:
targetEntity: Merchant
mappedBy: merchant_id
lifecycleCallbacks: { }
Product-entity
BLAAT\Bundle\AdminBundle\Entity\Core\Product:
type: entity
table: Product
repositoryClass: BLAAT\Bundle\AdminBundle\Entity\Core\ProductRepository
id:
id:
type: string
length: 255
id: true
generator:
strategy: NONE
fields:
title:
type: string
length: 255
defaultDescription:
type: text
column: default_description
nullable: TRUE
defaultPrice:
type: float
column: default_price
nullable: TRUE
manyToOne:
sellers:
targetEntity: MerchantProduct
inversedBy: products
joinColumn:
name: product_id
referencedColumnName: id
lifecycleCallbacks: { }
Merchant-entity
BLAAT\Bundle\AdminBundle\Entity\Core\Merchant:
type: entity
table: null
repositoryClass: BLAAT\Bundle\AdminBundle\Entity\Core\MerchantRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
length: 255
address:
type: string
length: 255
manyToOne:
stock:
targetEntity: MerchantProduct
inversedBy: outlets
joinColumn:
name: merchant_id
referencedColumnName: id
lifecycleCallbacks: { }
I think the mappedBys in MerchantProduct should be sellers and stock and that the referencedColumnNames should be productId and merchantId (or possibly product_id and merchant_id -- I haven't tested it, but something that exists in MerchantProduct, in any case).
It's the latter that seems to cause the error, because there is no column id: it should be productId.
To deal with a compound primary key, I think you need to use multiple JoinColumns to define the manyToOne relation in Merchant and Product. If I'm reading the documentation correctly, you can combine multiple JoinColumns under JoinColumns.
So:
manyToOne:
stock:
targetEntity: MerchantProduct
inversedBy: outlets
joinColumns:
joinColumn:
name: merchant_id
referencedColumnName: merchantId
joinColumn:
name: product_id
referencedColumnName: productId

Gedmo Tree Extension: Unable to find ancestor/parent child relation

I'm trying to use tree extension of Gedmo with YAML format but when I try to update the schema it returns the following error:
[Gedmo\Exception\InvalidMappingException]
Unable to find ancestor/parent child relation through ancestor field - [parent] in class -
The following code is the MyEntity.orm.yml file:
Project\MyBundle\Entity\MyEntity:
type: entity
repositoryClass: Gedmo\Tree\Entity\Repository\NestedTreeRepository
gedmo:
tree:
type: nested
id:
id:
type: string
nullable: false
generator:
strategy: UUID
fields:
name:
type: string
length: 200
locale:
type: string
length: 6
lft:
type: integer
gedmo:
- treeLeft
lvl:
type: integer
gedmo:
- treeLevel
rgt:
type: integer
gedmo:
- treeRight
root:
type: string
nullable: true
gedmo:
- treeRoot
manyToOne:
parent:
targetEntity: Project\MyBundle\Model\MyEntityInterface
inversedBy: children
joinColumn:
name: parent_id
referencedColumnName: id
onDelete: CASCADE
gedmo:
- treeParent
oneToMany:
children:
targetEntity: Project\MyBundle\Model\MyEntityInterface
mappedBy: parent
orderBy:
lft: ASC
It cant be interface, it has to be a entity
find
targetEntity: Project\MyBundle\Model\MyEntityInterface
change to:
targetEntity: Project\MyBundle\Entity\MyEntity

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

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