Multiple One-To-One relationships in same entity with YAML in Symfony2 - symfony

I am designing one entity that should have two One To One relationships to two different tables. My main entity (Player) has these definitions:
oneToOne:
attributesRegular:
targetEntity: AttributesRegular
oneToOne:
attributesNormalized:
targetEntity: AttributesNormalized
Now in the AttributesRegular I have this: (similar to AttributesNormalized)
oneToOne:
player:
targetEntity: Player
inversedBy: attributesRegular
joinColumn:
name: player_id
referenceColumnName: id
The problem is that when I generate the classes (and the DB schemas), only attributesNormalized is added, but not attributesRegular as well.
Can someone point to me what is wrong with my YML ?
Thanks!

You only need to state oneToOne once in Player otherwise you are rewriting the key/value rather than adding a value to it.
oneToOne:
attributesRegular:
targetEntity: AttributesRegular
attributesNormalized:
targetEntity: AttributesNormalized

Related

destroy a one-to-one relationship in doctrine

I have a relationship like the one in doctrine's docs so i'll use it as an example:
Product:
type: entity
oneToOne:
shipping:
targetEntity: Shipping
joinColumn:
name: shipping_id
referencedColumnName: id
I'm attempting to delete a Shipping entity but am getting a foreign key constraint exception because the Product's row holds a reference to it. What's the proper way of handling this? Is there something in the yaml that i can add to take care of this? Or do i need to do something like below:
$product->setShipping(null);
$entityManager->persist($product);
$entityManager->remove($shipping);
$entityManager->flush();
You should set the onDelete option to CASCADE if you want the Shipping to be removed too, or to SET NULL if you want to delete just the Product.
Product:
type: entity
oneToOne:
shipping:
targetEntity: Shipping
joinColumn:
name: shipping_id
referencedColumnName: id
onDelete: "SET NULL"
You can read more about this on the Doctrine docs.

How to diff and intersect Many-To-Many Self-referencing ArrayCollections

I have Many-To-Many Self-referencing relation in my user entity.
// Acme\DemoBundle\Resources\config\doctrine\User.orm.yml
Acme\DemoBundle\Entity\User:
type: entity
repositoryClass: Acme\DemoBundle\Entity\Repository\UserRepository
table: users
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
username:
type: string
length: 25
unique: true
manyToMany:
friendsWithMe:
targetEntity: User
mappedBy: myFriends
myFriends:
targetEntity: User
inversedBy: friendsWithMe
joinTable:
name: friends
joinColumns:
user_id:
referencedColumnName: id
inverseJoinColumns:
friend_user_id:
referencedColumnName: id
Now i want to get three different user collections:
MyFriends - collection of entities with myFriend == true and friendWithMe == false
FriendWithMe - collection of users with myFriend == false and friendWithMe == true
MutualFriends - collection of users with myFriend == true and friendWithMe == true
Standart getMyFriends an getFriendsWithMe (generated in user entity) returns all MyFriends and FriendWithMe records if friends are mutual =(
I tried to dig into the side of the Criteria but it does not work with many-to-many relations.
I think there is a general problem in your design structure. Self-Referencing relations in Doctrine are equal to Mutual relations. The states, that not both users are friends with each others, sounds more like a FriendsRequest. Maybe you should change this behavior to have on relation for MutualFriends and two different relations for MyFriendRequests and RecievedFriendRequest.
Antother possiblity is to use an Relationship Entity like "Friendship" this could look like
// Acme\DemoBundle\Resources\config\doctrine\Friendship.orm.yml
Acme\DemoBundle\Entity\Friendship:
type: entity
repositoryClass: Acme\DemoBundle\Entity\Repository\FriendshipRepository
table: friendship
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
user_one_accepted:
type: boolean
user_two_accepted:
type: boolean
manyToOne:
user_one:
targetEntity: User
user_two:
targetEntity: User

Doctrine2 The association mapping 'ManyToMany' misses the 'targetEntity' attribute. Exception

I want to create three tables use symfony2 and doctrine. Firm , Category and CategoryRelation. This two table have manyToMany relation , i will this relations keep in categoryRelation table. My yml mapping is;
Firm:
// other columns
ManyToMany:
categories:
targetEntity: Category
joinTable:
name: category_relation
joinColumns:
firm_id:
referencedColumnName: id
inverseJoinColumns:
category_id:
referencedColumnName: id
Category:
// other columns
manyToMany:
firms:
targetEntity: Firm
mappedBy: categories
and give me this error
The association mapping 'ManyToMany' misses the 'targetEntity' attribute.
How can I resolve this error? Thanks for answers
I compared your code with the example code of http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html, section 5.14. Many-To-Many, Bidirectional. I paste that code below:
User:
type: entity
manyToMany:
groups:
targetEntity: Group
inversedBy: users
joinTable:
name: users_groups
joinColumns:
user_id:
referencedColumnName: id
inverseJoinColumns:
group_id:
referencedColumnName: id
Group:
type: entity
manyToMany:
users:
targetEntity: User
mappedBy: groups
I've seen some differences which could produce that Exception. Firstly, you doesn't write the type: entity directive. Your first manyToMany directive has the first M in capital letter. The inversedBy directive doesn't exist. And finally I think this is the main problem, your directives targetEntity and mappedBy are not indented:
firms:
targetEntity: Firm
mappedBy: categories
And YAML is based on the indentation, therefore, probably this is the targetEntity specified in the Exception. You should write it in this way:
firms:
targetEntity: Firm
mappedBy: categories

Two OnetoMany relationships on an entity causes error?

I have an entity called Reservations that has two relationships with other tables.
reservation.orm.yml
oneToMany:
emails:
targetEntity: Acme\EmailBundle\Entity\Email
mappedBy: reservation
oneToMany:
texts:
targetEntity: Acme\TextBundle\Entity\Text
mappedBy: reservation
email.orm.yml
manyToOne:
reservation:
targetEntity: Acme\ReservationBundle\Entity\Reservation
inversedBy: emails
joinColumn:
name: reservation_id
referencedColumnName: id
text.orm.yml
manyToOne:
reservation:
targetEntity: Acme\ReservationBundle\Entity\Reservation
inversedBy: texts
joinColumn:
name: reservation_id
referencedColumnName: id
Using this code, I have access to the texts by doing
$reservation->getTexts();
However, when I do
$reservation->getEmails();
Symfony returns NULL, even though in my database, the email has a reservation ID of the reservation I'm trying to get the emails of. So, from what it seems like, it creates the email and sets the reservation, but for some reason I'm unable to get the emails for those reservations, in PHP or Twig.
I think this is because I didn't define my relationships correctly, though they are basically cookie-cutter many-to-one. The order that they are placed is screwing it up. In my reserations PHP, I do create an array collection to store emails just like texts. Don't know what's going on....
Try this for the reservations yml file..
oneToMany:
oneToMany:
texts:
targetEntity: Acme\TextBundle\Entity\Text
mappedBy: reservation
emails:
targetEntity: Acme\EmailBundle\Entity\Email
mappedBy: reservation

Symfony2 - EntityNotFoundException

I have a Stats entity that is related to a Journey entity through a ManyToOne association.
id:
index:
type: integer
fields:
idJourney:
type: string
// data fields...
manyToOne:
journey:
targetEntity: Journey
joinColumn:
name: idJourney
referencedColumnName: idJourney
The Journey is related to the Station entity through two ManyToOne association: one for the first Station of the Journey, one for the last.
id:
idjourney:
type: string
fields:
idFirstStation:
type: string
idLastStation:
type: string
// other info fields
manyToOne:
firstStation:
targetEntity: Station
joinColumn:
name: idFirstStation
referencedColumnName: idStation
lastStation:
targetEntity: Station
joinColumn:
name: idLastStation
referencedColumnName: idStation
Finally, the Station entity :
id:
idStation:
type: string
fields:
name:
type: string
// other station info
I retrieve a collection of Stats objects with all related sub-objects via a custom Repository method which works fine.
$statCollection = $statsRepository->getStatsByDateAndArea($date, $area);
//This retrieves the expected data
$statCollection[0]->getJourney()->getFirstStation()->getName();
However, iterating through the collection with a foreach loop doesn't work:
foreach($statCollection as $stat) {
$journey = $stat->getJourney(); // works fine
$firstStationId = $journey->getFirstStationId(); // works too
$firstStation = $journey->getFirstStation(); // still works, but returns a Proxies\AcmeAppPathWhateverBundleEntityStationProxy object instead of a AcmeAppPathWhateverBundleEntityStation, but this should be transparent (as per Doctrine documentation)
$firstStationName = $firstStation->getName(); // throws an EntityNotFoundException
}
Any idea what's going on ? Should I force Doctrine to fetch all sub entities ?
EDIT
The error message is fairly laconic :
EntityNotFoundException: Entity was not found.
Not very helpful...
I ended up querying explicitly for the full set of sub-entities in my custom repository method...
I changed this query :
->select('stats')
->leftJoin('stats.journey', 'j')
->leftJoin('j.firstStation', 'fs')
->leftJoin('j.lastStation', 'ls')
to :
->select('stats, j, fs, ls')
->leftJoin('stats.journey', 'j')
->leftJoin('j.firstStation', 'fs')
->leftJoin('j.lastStation', 'ls')
I guess Doctrine's use of Proxy objects are not all that transparent...

Resources