Doctrine Relationships - referenced Column name as identifier for entity - symfony

So I have these 2 Bundles:
UserBundle
BlogBundle
and these 3 Entities:
UserBundle:User
BlogBundle:User
BlogBundle:Article
BlogBundle:User extends UserBundle:User using a bidirectional one to one relationship. The join column name is user_id and it is an association key:
BlogBundle\Entity\User:
type: entity
table: blog_users
id:
user:
associationKey: true
oneToOne:
user:
targetEntity: UserBundle\Entity\User
inversedBy: blog_user
joinColumn:
name: user_id
referencedColumnName: id
Now I want to create a bidirectional one to Many relationship between BlogBundle:User and BlogBundle:Article.
Currently I'm trying this:
BlogBundle:User
oneToMany:
articles:
targetEntity: Article
mappedBy: author
BlogBundle:Article
manyToOne:
author:
targetEntity: User
inversedBy: Article
joinColumn:
name: author
referencedColumnName: user_id
My Problem is, it works, I can access data from UserBundle:User through an Article object, but in the profiler it shows them as not mapped correctly. My guess is it would be possible to do what I'm trying, but I'm just doing something wrong.
What am I missing?

I think the problem is with the inversedBy, where you have to write the name of the field, not the name of the entity.
Here que documentation:
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional
Your code will be:
BlogBundle:User
oneToMany:
articles:
targetEntity: Article
mappedBy: author
BlogBundle:Article
manyToOne:
author:
targetEntity: User
inversedBy: articles
joinColumn:
name: author
referencedColumnName: user_id

Related

Simple Derived Identity issue

I'm struggling with an issue based on the use case that is described here :
Use-Case 2: Simple Derived Identity
I have the following Doctrine entities and mapping in my Symfony app:
class User
{
private $entity_id;
private $address;
...
}
class Address
{
private $user;
...
}
AppBundle\Entity\User:
type: entity
id:
entity_id:
type: integer
generator:
strategy: AUTO
oneToOne:
address:
targetEntity: Address
mappedBy: user
cascade: ["persist"]
AppBundle\Entity\Address:
type: entity
id:
user:
associationKey: true
oneToOne:
user:
targetEntity: User
inversedBy: address
joinColumn:
name: entity_id
referencedColumnName: entity_id
Every time I perform a DQL query that involves the User entity, Doctrine performs one additional query per matching User to retrieve the corresponding Address entity. That happen every time, even if the Address data are never used in the code.
I tried to reproduce this issue on a vanilla Symfony installation, and I faced another issue, I'm not able to perform the following code as I get an error (Entity of type AppBundle\Entity\Address is missing an assigned ID for field 'user'):
$user = (new User())->setAddress(new Address());
$entityManager->persist($user);
$entityManager->flush();
Do you have any hint or what is wrong?
Best regards

Unique constraints on foreign key in Doctrine

Is there any way to add constraints by foreign key in Doctrine? This is my config for an entity in Symfony 3.3.
doctrine:scheme:validation command gives me an answer like "There is no column with name 'product' on table 'tariff'"
Rg\ApiBundle\Entity\Tariff:
fields:
price:
type: float
column: price
manyToOne:
product:
targetEntity: Product
inversedBy: tariffs
timeunit:
targetEntity: Timeunit
inversedBy: tariffs
uniqueConstraints:
no_double_tariff_idx:
columns:
- product
- timeunit
You need to reference the column's names (not the name of the relation used by doctrine). By default doctrine will suffix the relation's name with _id but you can configure the exact name of the join-column as shown in the following example configuration:
'Your\Entity\ProductVariant':
manyToOne:
image:
targetEntity: 'Your\Entity\Product\Image'
joinColumn:
name: '`image_id`'
referencedColumnname: 'id'
nullable: false
options:
unique: false
color:
targetEntity: 'Your\Entity\Product\Color'
joinColumn:
name: '`color_id`'
referencedColumnname: 'id'
# [..]
uniqueConstraints:
only_one_image_of_same_product_color_idx:
columns:
- 'image_id'
- 'color_id'

Doctrine2 YML Mapping on Symfony2

I am starting with Symfony2, and so do I with Doctrine2.
I want to make an entity bidirectional, but I don't know what I am doing wrong. Here is my two .orm.yml files :
Categorie.orm.yml :
MY\SUPERBundle\Entity\Categorie:
type: entity
fields:
id:
id: true
type: integer
generator:
strategy: AUTO
nomCategorie:
type: text
nullable: true
column: NomCategorie
oneToMany:
SousCategories:
targetEntity: MY\SUPERBundle\Entity\SousCategorie
mappedBy: Categorie
SousCategorie.orm.yml :
MY\SUPERBundle\Entity\SousCategorie:
type: entity
fields:
id:
id: true
type: integer
generator:
strategy: AUTO
nomSousCategorie:
type: text
nullable: true
column: NomSousCategorie
manyToOne:
Categorie:
targetEntity: MY\SUPERBundle\Entity\Categorie
inversedBy: SousCategories
joinColumns:
categorie_id:
referencedColumnName: id
nullable: false
When I want to run the command :
doctrine:schema:update --dump-sql
I am getting this error :
[ReflectionException]
Property MY\SUPERBundle\Entity\Categorie::$SousCategories does not exist
If you guys have any hint on what I am doing wrong, I would be very grateful.
Thanks !
Are you sure you really have a property named "SousCategories" under your "Categorie" class?
Would be nice if you could add your entity class in your first post.
BTW, if it's a property, it shouldn't begin with an uppercase letter.

How to many to many with repeated relationships (third key in table)

It's possible in Doctrine2 to manage Many to Many relations with a third key to be able to add more than one identic relationship?
I have one "Users" table and one other "Plans" table and i did the normal many to many relationship that produces user_plan table with the two primary keys (user_id and plan_id) but I need in my application to be able to add the same plan to user more than one time. For example: user_plan(generated_id, user_id, plan_id)
My current user yml definition:
Entity\FosUser:
type: entity
table: fos_user
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
manyToMany:
plans:
targetEntity: Plan
inversedBy: users
joinTable:
name: user_plan
joinColumns:
plan_id:
referencedColumnName: id
inverseJoinColumns:
user_id:
referencedColumnName: id
lifecycleCallbacks:
prePersist: [ setUserValue ]
preUpdate: []
My current plan yml definition:
Entity\Plan:
type: entity
table: plan
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
planName:
type: string
length: 50
fixed: false
nullable: false
column: plan_name
manyToMany:
users:
targetEntity: FosUser
mappedBy: plans
LifecycleCallbacks:
prePersist: [ setCreatedAtValue ]
preUpdate: [ setUpdatedAtValue ]
Someone knows if it's possible to do that with symfony2?
I don't know about third key, but i see another solution. You can add another model PlantBed. User has_many PlantBeds (PlantBed has_one User). PlantBed has_one Plant (Plant has_many PlantBeds) and quantityOfPlantsInBed.

Symfony 2 fetch data from many to many relationship with doctrine

I'm stuck on getting this to work with Symfony2 and Doctrine, the situation:
A page with piercing info (general info and caretaking info).
A care taking can have multiple piercings to which it applies and a piercing can have multiple care takings
Database layout:
Piercings:
id
name
...
Caretaking:
id
title
description
piercing_to_caretaking
id
piercing_id
caretaking_id
Now, how would I create the Entity and the corresponding Query/Dql ?
If you are defining your entities using yml:
In Piercing.orm.yml add:
manyToMany:
caretakings:
targetEntity: Caretaking
inversedBy: piercings
joinTable:
name: piercing_caretaking
joinColumns:
caretaking:
referencedColumnName: id
inverseJoinColumns:
piercing:
referencedColumnName: id
In Caretaking.orm.yml add:
manyToMany:
piercings:
targetEntity: Piercing
mappedBy: caretakings
Generate/update the entities in the usual way, i.e.:
app/console doctrine:schema:update --dump-sql (to check results)
app/console doctrine:schema:update --force (to apply changes)
Then when you have a Piercing or Caretaking entity you can access the related entities like this:
$piercing->addCaretaking($caretaking);
$caretakings = $piercing->getCaretakings();
...
$piercings = $caretaking->getPiercings();
For more information, including how to do this using annotations, see sub-section 5.1.4 Many to Many, Bidirectional in Section 5 Association Mapping of the Doctrine documentation.

Resources