I'm wondering if it's possible to querying doctrine entities by a property from a relation.
Here is an example :
Entity A fields :
-> title
-> content
-> description
-> date
Entity B fields :
-> title
-> link ( entity b )
-> date
Is it possible to querying Entity B by link->title property such as the following :
$this->getDoctrine()->getManager()->getRepository("acmeAppBundle:EntityB")->findBy(array( "title" => "test", "link.title" => "example" ) );
Currently I'm achieving this with a custom function from Entity B repository, but may be I'm missing something.
You can't use findBy like this. FindBy is only there to fetch very basic things. Generally it's considered as a best practice to use repository calls, because for example, if you here fetch all objects where title is test and then you get from A all B entity, then entity B will be fetched separately, while in a repository call you can use join, so only one query will be sent to your DB.
Related
Usually when querying in a custom repository class, I use something like this :
SELECT * FROM BundleName:Entity
But how do I do for associative entity ?
I have an entity "Ticket" and an entity "Tag".
It's a ManyToMany relation.
In phpMyAdmin, I've got a ticket_tag associative table but how do I get it with Doctrine ?
Thank you
You should use createQueryBuilder to handle your custom query requirement, in case if you are having a valid relationship over entities. for example:
Inside ticket repository you should handle like this, if you want to do more operations then you should learn more from here: https://symfony.com/doc/3.3/doctrine/repository.html
$query = $this->createQueryBuilder('t')
->select('count(t.id) as total_ticket, tag.id as tagId')
->leftJoin('t.tags', 'tag')
->groupBy('tag.id')
;
return $query->getQuery()->getResult();
My model has a "parent" entity called Application and a "child" entity called "role" which joins to application via a ManyToOne relationship.
However, Application does not have an inverse "oneToMany" relationship to Role for performance reasons.
What I'm trying to do is to join a role entity to the application entity on the Application's Sonata Admin List.
The closest I've been able to come is using createQuery on the applicationAdmin class like so:
public function createQuery($context = 'list'){
$query = parent::createQuery($context);
$query
->leftJoin('fnBundle:Role','r','WITH','r.application = ' . $query->getRootAlias() . '.id')
->leftJoin('fnBundle:User','u','WITH','ar.user = u')
->addSelect('u.email');
return $query;
}
This dies with the following error:
"Warning: get_class() expects parameter 1 to be object, array given")
in "SonataAdminBundle:CRUD:base_list_field.html.twig"
I believe (though I've had trouble proving) this is because the return application object is nested in an array with two keys like
array(0 => <Application Object>, 1 => <User Email>);
instead of just the application object.
So how would I go about fixing this? Can I select the u.email AS a property of the application (I don't think so, I've been looking for a solution)...should I override the DataGrid somehow, or is there no elegant solution to this problem?
Thanks!
I have an object $user that has a one to many relation with $establishment. I can use:
$user->getEstablishments();
The user can select a stablishment to work on. I have this method that I call in the controller:
$user->setCurrentEstablishment($establishment);
And this one that I call in the view:
$establishment = $user->getCurrentEstablishment();
I want to be able to call:
$user->setCurrentEstablishmentBy Slug($establishment_slug);
where the slug is a string, and let the user object look for the establishment.
Doctrine discourages the practice of accessing the Entity Manager inside the Entity object, but I think that using it in the controller is even worse.
I suspect that some special Doctrine annotation exists that takes care of non persistent relations like this, or some method other than serving the Entity Manager through a service should be used here. Some easy way of referencing other entities from inside the model.
¿Is there any? ¿How could I do that?
There is no Annotation in Doctrine which could convert slug into object.
What can help You is ParamConverter, with it you can automatically convert slug from query into object. But it still must be used in Controller.
Example usage:
/**
* #Route("/some-route/{slug}")
* #ParamConverter("object", class="AppBundle:Establishment", options={"id" = "slug", "repository_method" = "findEstablishmentBySlug"})
*/
public function slugAction(Establishment $object)
{
...
Docs about param converter: http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
I have a situation where i must use an entity for persistence and associations with many other entities in several forms.
For example :
Entities A,B,C and D
Relation between A (supposing I want to use the entity A with id 10) and B is oneToMany.
How to get the entity A with Id 10 in the controller of the entity B when creating a new entity B ( b->setA(A) )
Knowing that i load the entity A only in one form by search
Or how to use loaded entity A with Id 10 and use it in others controllers
Thanks
In the first controller where entity A is created and therefore you presumably have access to the id:
$this->get('session')->set('entityAId' => $entityA->getId());
In the second controller:
$entityAId = $this->get('session')->get('entityAId');
$em = $this->getDoctrine()->getManager();
$entityA = $em->getRepository('YourBundle:EntityA')->findOneBy(array('id' => $entityAId));
You will need to cater for the scenario where no entity with that id is found and if you haven't used the session before you should read about Symfony Session Management
I'm using Symfony2 / Doctrine2.
I'm trying to override the BaseEntityRepository Class so the magic findBy method automatically adds a JOIN on a "translations" relation. It was easy to do in Symfony 1.4/Doctrine 1, because it was manipulating a Doctrine_Query object, so I simply had to $query->addJoin() and it did the trick.
Unfortunately, in Doctrine 2 you only receive an array of criteria as parameter, and that's where I'm stucked.
I got many entities which have a one-to-many relationship with a translation entity.
For exemple : Section as a one-to-many relationship with SectionTranslation
The goal is to retrieve only the "active" sections (active is in SectionTranslation) when using SectionRepository->findAll(); (or even findBy).
The wanted DQL result is : Select * from Section INNER JOIN SectionTranslation ON Section.id = SectionTranslation.translatable_id WHERE SectionTranslation.locale = $locale AND SectionTranslation.active = 1;
Any idea?
The solution could be adding event listener for particular entity and particular method.