Symfony Doctrine Count ManyToMany Relationship - php

I have an User entity with a many-to-many entity (Wish) linked to it, I now need the top 10 wishes now using the Symfony QueryBuilder.
The problem here is that i do not have direct access to the ManyToMany Table/Entity/Repository as its automatically created and managed by Doctrine ORM.
Making a manual OneToMany, ManyToOne Entity is not really an option as it will break existing code (mostly the automatic collection population and the add / remove functions of it)
Doing a manual SQL is kinda dodgy since the table name is generated and might change some update (even if not I would prefer to keep it clean)
some code:
class User {
// .........
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Wish")
*/
private $wish;
public function __construct()
{
$this->wish = new ArrayCollection(); // with get/set/add/remove etc
}
}
class Wish {
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
// ... get set etc
}

Related

Cascade delete, each one is deleted

class DistCache
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\PlaceInfo")
* #ORM\JoinColumn(nullable=false)
*/
private $placeOne;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\PlaceInfo")
* #ORM\JoinColumn(nullable=false)
*/
private $placeTwo;
This table has two members, both related to PlaceInfo class.
And PlaceInfo class doesn't have any member related to DistCache class.
Then I want to delete the DistCache entry when one of two members(placeOne or placeTwo) is deleted
I googled around and found cascade="remove" or orphanRemoval=true, but both looks bit different my purpose.
How can I solve?
I can see that for both PlaceInfo object you set nullable=false , so when deleting a PlaceInfo, not only have to delete the DistCache entities managed by entityManager, you have to delete the ones in the database too.
I suggest you can use preRemove event from Doctrine life cycle callbacks.
On the remove event of a PlaceInfo record, you query all the DistCache objects which use the deleted PlaceInfo object and remove them first.
In short, you need to :
Add #ORM\HasLifecycleCallbacks before your class to enable life cycles.
Add preRemove function in PlaceInfo class :
/**
* #ORM\PreRemove
* #param LifecycleEventArgs $event
*/
public function removeDistCache(LifecycleEventArgs $event)
{
$em = $event->getEntityManager();
// Use $em to query and delete the DistCache entities
}

[Doctrine][Symfony] Multiple Joins in a single annotation request (is it possible ?)

I'm working on a Symfony project (V3.4) and I'm using an existing DB (can't change it).
To interact with it I use doctrine annotations and the work is well done!
I manage to submit requests using JoinTable and JoinColumns but there is one last thing I don't know how to deal with ...
I have the following tables :
tables
I have the id from table A and I'm trying to get the libelle from the E table.
Is there a way to do it using annotations? For now I've already done it between 3 tables but I don't know how to do it for more :
#ORM\ManyToMany(targetEntity="")
* #ORM\JoinTable(name="",joinColumns={#ORM\JoinColumn(
name="",referencedColumnName="")}, inverseJoinColumns={#ORM\JoinColumn(
name="",referencedColumnName="", unique=true)})
If it's not possible I'm open to suggestions.
Thanks!
By looking at your need and your schema, i can tell you that you don't need many to many relationship. You should bidirectional relation across the entities(tables).
I don't know which kind of relation you have right now, but assuming one-to-one, you can setup relations following way:
EntityA
/**
* #OneToOne(targetEntity="EntityB", mappedBy="entityA")
*/
private $entityB;
EntityB
/**
* #OneToOne(targetEntity="EntityA", inversedBy="entityB")
* #JoinColumn(name="entityA_id", referencedColumnName="id")
*/
private $entityA;
/**
* #OneToOne(targetEntity="EntityC", mappedBy="entityB")
*/
private $entityC;
EntityC
/**
* #OneToOne(targetEntity="EntityB", inversedBy="entityC")
* #JoinColumn(name="entityB_id", referencedColumnName="id")
*/
private $entityB;
/**
* #OneToOne(targetEntity="EntityD", mappedBy="entityC")
*/
private $entityD;
EntityD
/**
* #OneToOne(targetEntity="EntityC", inversedBy="entityD")
* #JoinColumn(name="entityC_id", referencedColumnName="id")
*/
private $entityC;
/**
* #OneToOne(targetEntity="EntityE", mappedBy="entityE")
*/
private $entityE;
EntityE
/**
* #OneToOne(targetEntity="EntityD", inversedBy="entityE")
* #JoinColumn(name="entityD_id", referencedColumnName="id")
*/
private $entityD;
I didn't have time to test it. But it should be straight forward.
You can get libelle from E table following way:
$entityA = $entityRepositoryForA->find(a_id_here);
$entityA->getEntityB()->getEntityC()->getEntityD()->getEntityE->getLibelle();

How to handle Primary/Secondary/Normal statuses for entity in Symfony2

I am developing an application and I came across the following: Lets say I have an entity called Contact, that Contact belongs to a Company and the Company has a Primary Contact and a Secondary Contact and also has the remaining Contacts which I've named Normal.
My question is, what is the best approach for this when talking about entities properties and also form handling. I've though about two things:
Having 2 fields on the Company entity called PrimaryContact and SecondaryContact and also have a one-to-many relationship to a property called contacts.
What I don't like (or I'm not 100% how to do) about this option is that on the Contact entity I would need an inversedBy field for each of the 2 one-to-one properties and also 1 for the one-to-many relationship and my personal thought is that this is kind of messy for the purpose.
Having a property on the Contact entity called Type which would hold if it's primary, secondary or normal and in the Company methods that has to do with Contacts I would modify it and add the getPrimaryContact, getSecondaryContact, etc.
What I don't like about this option is that I would need to have 2 unmapped properties for the Company and I would need to do a lot on the form types in order to get this to work smoothly.
My question is what is the best approach for this structure and how to deal with forms and these dependencies. Let me know if this is not clear enough and I will take time and preparate an example with code and images.
I'm not yet a Symfony expert but i'm currently learning entites manipulation and relations !
And there is not simple way to do relations with attributes.
You have to create an entity that represent your relation.
Let's suppose you have an entity Company and and entity Contact
Then you will have an entity named CompanyContact whick will represent the relation between your objects. (you can have as many attributes as you wish in your relation entity). (Not sure for the Many-to-One for your case but the idea is the same)
<?php
namespace My\Namespace\Entity
use Doctrine\ORM\Mapping as ORM
/**
* #ORM\Entity(repositoryClass="My\Namespace\Entity\CompanyContactRepository")
*/
class CompanyContact
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="contact_type", type="string", length=255)
*/
private $contactType;
/**
* #ORM\ManyToOne(targetEntity="My\Namespace\Entity\Company")
* #ORM\JoinColumn(nullable=false)
*/
private $company;
/**
* #ORM\ManyToOne(targetEntity="My\Namespace\Entity\Contact")
* #ORM\JoinColumn(nullable=false)
*/
private $contact;
}
And in your controller you can do this:
$em = $this->getDoctrine()->getManager();
$company = $em->getRepository('YourBundle:Company')->find($yourCompanyId);
$yourType = "primary";
$companyContacts = $em->getRepository('YourBundle:CompanyContact')
->findBy(array('company' => $company, 'type' => $yourType));
What do you think about this approach ?
If i learn more soon i will get you posted ;)
Thanks to #Cerad this is the following approach I took:
I have a OneToMany property on the Company to hold all the contacts.
Implemented the getPrimaryContact/setPrimaryContact methods and looped through all the contacts and retrieving the one of the type I want. Did the same for the secondary.
On the Form type of the company my issue was that I had the 'mapped' => 'false' option, I removed this since I implemented the getters and setters SF2 knows it has to go to these methods.
`
<?php
namespace XYZ\Entity;
/**
* #ORM\Entity
* #ORM\HasLifecycleCallbacks()
*/
class Company
{
...
/**
* #ORM\OneToMany(targetEntity="\XYZ\Entity\Contact", mappedBy="company", cascade={"persist", "remove"})
*/
private $contacts;
public function getPrimaryContact() { ... }
public function setPrimaryContact(Contact $contact) { //Set the type of $contact and add it $this->addContact($contact) }
public function getSecondaryContact() { ... }
public function setSecondaryContact(Contact $contact) { //Set the type of $contact and add it $this->addContact($contact) }
}`
And for the Form Type I have:
`
class CompanyType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
...
->add('primaryContact', new ContactType())
->add('secondaryContact', new ContactType())
}
...
}`
With this set everything runs smoothly and I can CRUD without much struggle.

Accessing relations of a table with inheritance

I have an Inheritance class as shown here:
As you can easily see users, buildings and hotels have addresses (more than one) and address table keeps the id of the owner in whose column.
Is my logic correct?
Let's say I want to get the address of user (or buildings or hotels) whose id is 2; must I run a DQL statement (and how?) or can I get it with find() function without DQL?
And I'll be happy if you give example since Doctrine documentation doesn't help much.
Thanks.
Edit: users, buildings and hotels are just symbolic names that is why they can have multiple addresses otherwise buildings and hotels would have only one address.
Edit 2:I think I couldn't make myself clear, when I talk about the Class Table Inheritance I mean entity class has the Discriminator column as
/**
* ...
*
* #DiscriminatorColumn(name="classname", type="string")
* #DiscriminatorMap({"Entities\users" = "Entities\users",
* "Entities\buildings" = "Entities\buildings"}) ... etc
*/
Each and every subclass is related to parent (Entity) with the foreign key relation as "id". But of course doctrine creates this relation already for me.
Usually an Address is a typical value object. Value objects are usually stored with the entity compositing the value object so it is neither about relations nor about class table inheritance. If your domain indicates otherwise (e.g. you can do something with your address, meaning), they might be an entity, than entity Hotel holds an entity Address (persisted in a n:m relation table) and entity Building holds and Address too (in a different n:m relation table).
If you go the value object route, things are different. You would store the address with the Building entity as well as with the Hotel entity (as you would do it with other value objects may it be Moneyor Email or Password). So you don’t need relations at all, just a few more fields. The issue with Doctrine 2 is, that it does not support Component mapping. Component mapping would be used to nicely store value objects. T accomplish the same thing with Doctrine 2, you would implement a #prePersist and a #postLoad handler like that:
class Hotel
{
private ;
/** These fields are persisted */
/** #Column(type=string) */
private $addressStreet;
/** #Column(type=string) */
private $addressCity;
/** #Column(type=string) */
private $addressZip;
/** #Column(type=string) */
private $addressCountry;
/** #prePersist */
public function serializeValueObjects()
{
$this->addressStreet = ->address->getStreet();
$this->addressCity = ->address->getCity();
$this->addressZip = ->address->getZip();
$this->addressCountry = ->address->getCountry();
}
public function unserializeValueObjects()
{
$this->address = new Address(->addressStreet, ->addressCity, ->addressZip, ->addressCountry);
}
}
As you need to serialize/unserialize Address value objects in various places, you might want to extract the serializing code into a separated class.
/**
*
* #Entity
* #Table(name="proposaltemplate")
* #InheritanceType("JOINED")
* #DiscriminatorColumn(name="entitytype", type="string")
* #DiscriminatorMap({"proposal" = "ProposalTemplate","page" = "PageTemplate"})
*
*/
abstract class AbstractProposalTemplate
{
/**
*
* #var integer
* #Id
* #Column(type="integer")
* #generatedValue(strategy="AUTO")
*
*/
private $id;
}
next
#Entity
class ProposalTemplate extends AbstractProposalTemplate
{
#Id
#Column(type="integer")
#generatedValue(strategy="AUTO")
private $id;
}
next another class
#Entity
class PageTemplate extends AbstractProposalTemplate
{
/**
*
* #var integer
* #Id
* #Column(type="integer")
* #generatedValue(strategy="AUTO")
*
*/
private $id;
}
So you've got a superclass called "Entity", which has subclasses "User", "Building", and "Hotel".
Your "Entity" entity should have a OneToMany relation to Address. Let's imagine it looks like this, in your Entity definition:
/**
* #OneToMany(targetEntity="Address", mappedBy="whose"
*/
protected $addresses;
This is a more-or-less fine approach, though the use of inheritance is a little smelly.
Then if you want to iterate over the addresses, from inside User, Building, or Hotel:
foreach($this->addresses as $address){
//do something with adderess
}
Does that answer your question?

Doctrine 2 Inheritance Mapping with Association

NOTE : if what I want is not possible, a "not possible" answer will be accepted
In the Doctrine 2 documentation about inheritance mapping, it says there are 2 ways :
Single table inheritance (STI)
Class table inheritance (CTI)
For both, there is the warning :
If you use a STI/CTI entity as a many-to-one or one-to-one entity you should never use one of the classes at the upper levels of the inheritance hierachy as “targetEntity”, only those that have no subclasses. Otherwise Doctrine CANNOT create proxy instances of this entity and will ALWAYS load the entity eagerly.
So, how can I proceed to use inheritance with an association to the base (abstract) class ? (and keep the performance of course)
Example
A user has many Pet (abstract class extended by Dog or Cat).
What I want to do :
class User {
/**
* #var array(Pet) (array of Dog or Cat)
*/
private $pets;
}
Because of the warning in Doctrine documentation, I should do that :
class User {
/**
* #var array(Dog)
*/
private $dogs;
/**
* #var array(Cat)
*/
private $cats;
}
This is annoying, because I loose the benefits of inheritance !
Note : I didn't add the Doctrine annotations for the mapping to DB, but you can understand what I mean
I'm tired, but this seems like much ado about nothing.
You missed the important bit of that warning:
If you use a STI/CTI entity as a many-to-one or one-to-one entity
That's not the case in your example! If you had not omitted the doctrine annotations, you might have noticed.
The association User::pets is OneToMany, not [One|Many]ToOne. One user has many pets.
The inverse association is OneToOne, but it's targeting User, which has no inheritance.
Robin's answer should have been a good hint -- you can log the sql queries and see what doctrine actually does to your database!
The bad-for-performance scenario is something like:
abstract class Pet { ... }
class Cat extends Pet { ... }
class Dog extends Pet { ... }
class Collar {
/**
* #Column(length="16")
*/
protected $color;
/**
* ManyToOne(targetEntity="Pet")
*/
protected $owner;
}
Now, if you wanted to iterate over all the blue collars, Doctrine runs into some trouble. It doesn't know what class $owner is going to be, so it can't use a Proxy. Instead, it's forced to eagerly load $owner to find out whether it's a Cat or a Dog.
This isn't a problem for OneToMany or ManyToMany relationships, because in that case, lazy loading works fine. Instead of a proxy, you get a PersistentCollection. And a PersistentCollection is always just a PersistentCollection. It doesn't care about it's own contents until you actually ask for them. So lazy loading works fine.
I think you've misunderstood, the section of the manual you've quoted is entitled "Performance impact", they're not telling you you can't do this, only that there are performance implications if you do. This makes sense for lazy loading - for heterogeneous collections of STI entities you have to go to the database and load the entity before you know what class it will be, so lazy loading isn't possible / doesn't make sense. I'm learning Doctrine 2 myself at the moment, so I mocked up your example, the following works OK for more:
namespace Entities;
/**
* #Entity
* #Table(name="pets")
* #InheritanceType("SINGLE_TABLE")
* #DiscriminatorColumn(name="pet_type", type="string")
* #DiscriminatorMap({"cat" = "Cat", "dog" = "Dog"})
*/
class Pet
{
/** #Id #Column(type="integer") #generatedValue */
private $id;
/** #Column(type="string", length=300) */
private $name;
/** #ManyToOne(targetEntity="User", inversedBy="id") */
private $owner;
}
/** #Entity */
class Dog extends Pet
{
/** #Column(type="string", length=50) */
private $kennels;
}
/** #Entity */
class Cat extends Pet
{
/** #Column(type="string", length=50) */
private $cattery;
}
/**
* #Entity
* #Table(name="users")
*/
class User
{
/** #Id #Column(type="integer") #generatedValue */
private $id;
/** #Column(length=255, nullable=false) */
private $name;
/** #OneToMany(targetEntity="Pet", mappedBy="owner") */
private $pets;
}
... and the test script ....
if (false) {
$u = new Entities\User;
$u->setName("Robin");
$p = new Entities\Cat($u, 'Socks');
$p2 = new Entities\Dog($u, 'Rover');
$em->persist($u);
$em->persist($p);
$em->persist($p2);
$em->flush();
} else if (true) {
$u = $em->find('Entities\User', 1);
foreach ($u->getPets() as $p) {
printf("User %s has a pet type %s called %s\n", $u->getName(), get_class($p), $p->getName());
}
} else {
echo " [1]\n";
$p = $em->find('Entities\Cat', 2);
echo " [2]\n";
printf("Pet %s has an owner called %s\n", $p->getName(), $p->getOwner()->getName());
}
All my cats and dogs load as the correct type:
If you look at the generated SQL, you'll notice that when the OneToMany targetEntity is "pet", you get SQL like this:
SELECT t0.id AS id1, t0.name AS name2, t0.owner_id AS owner_id3, pet_type,
t0.cattery AS cattery4, t0.kennels AS kennels5 FROM pets t0
WHERE t0.owner_id = ? AND t0.pet_type IN ('cat', 'dog')
But when it's set to Cat, you get this:
SELECT t0.id AS id1, t0.name AS name2, t0.cattery AS cattery3, t0.owner_id
AS owner_id4, pet_type FROM pets t0 WHERE t0.owner_id = ? AND t0.pet_type IN ('cat')
HTH.

Categories