How to populate additive field via JOIN with Doctrine - php

Suppose i have an entity with a ManyToOne relation (with EXTRA_LAZY) and obviously a join column.
eg. Article (main entity) -> Author (external entity)
suppose i add a field author_name to the Article that NOT mapped to the ORM and that in certain contexts this field should contain the article->author->name value (usually it can stay null).
When i query a list of articles, i would like to populate automatically that article-author_name without implementing a getter that perform a select on each element of the query result. I would like Doctrine to fetch and hydrate that value in with a simple JOIN in the original query...
I don't want to set fetch mode to EAGER for performance as in my project Author is a really complex entity.
Is this possible?
Thanks

Probably i've found a solution. Don't know if it is the best way to do it but it works.
I've added to the main class a field getter
public getAuthorName() {
return $this->author->name
}
in my context this getter will only be called by a serializer in certain conditions.
In my repository code i have a special method (that i refactored so that any method could implicitly call it) to impose the population of the Article->author field when queried. The method simply use the querybuilder to add LEFT JOIN to the Author class and temporarily set FetchMode to EAGER on Article->author.
At the end a simple repository method could be this
public findAllWithCustomHydration() {
$qb = $this->createQueryBuilder('obj');
$qb->leftJoin("obj.author", "a")
-> addSelect("a"); //add a left join and a select so the query automatically retrive all needed values to populate Article and Author entities
//you can chain left join for nested entities like the following
//->leftJoin("a.address", "a_address")
//-> addSelect("a_address");
$q = $qb->getQuery()
->setFetchMode(Article::class, "author", ClassMetadata::FETCH_EAGER);
//setFetchMode + EAGER tells Doctrine to prepopulate the entites NOW and not when the getter of $article->author is called.
//I don't think that line is strictly required because Doctrine could populate it at later time from the same query result or maybe doctrine automatically set the field as EAGER fetch when a LEFT JOIN is included in the query, but i've not yet tested my code without this line.
return $q->getResult();
}
The con is that you have to customize each query or better use a DQL / SQL / QueryBuilder for each method of the repo but with a good refactoring, for simple inclusion cases, you can write a generic method that inject that join on a field_name array basis.
Hope this help and add your answer if you find a better way.
PS. i've wrote the above code on the fly because now i'm not on my notebook, hope it works at first execution.

Related

laravel Eloquent join and Object-relationship mapping

Ok so i'm kind of newish to eloquent and laravel (not frameworks tho) but i hit a wall here.
I need to perform some queries with conditions on different tables, so the eager load (::with()) is useless as it creates multiples queries.
Fine, let use the join. But in that case, it seems that Laravel/Eloquent just drops the concept of Object-relationship and just return a flat row.
By exemple:
if i set something like
$allInvoicesQuery = Invoice::join('contacts', 'contacts.id', '=', 'invoices.contact_id')->get();
and then looping such as
foreach ($allInvoicesQuery as $oneInvoice) {
... working with fields
}
There is no more concept of $oneInvoice->invoiceFieldName and $oneInvoice->contact->contactFieldName
I have to get the contacts fields directly by $oneInvoice->contactFieldName
On top of that the same named columns will be overwrited (such as id or created_at).
So my questions are:
Am i right assuming there is no solution to this and i must define manually the field in a select to avoid the same name overwritting like
Invoice::select('invoices.created_at as invoice.create, contacts.created_at as contact_create)
In case of multiple joins, it makes the all query building process long and complex. But mainly, it just ruins all the Model relationship work that a framework should brings no?
Is there any more Model relationship oriented solution to work with laravel or within the Eloquent ORM?
Instead of performing this join, you can use Eloquent's relationships in order to achieve this.
In your Invoice model it would be:
public function contact(){
return $this->belongsTo('\App\Contact');
}
And then of course inside of your Contact model:
public function invoices(){
return $this->hasMany('\App\Invoice');
}
If you want to make sure all queries always have these active, then you'd want the following in your models:
protected $with = ['Invoice']
protected $with = ['Contact'];
Finally, with our relationships well defined, we can do the following:
$invoices = Invoice::all();
And then you can do:
foreach($invoices as $invoice)[
$invoice->contact->name;
$invoice->contact->phone;
//etc
}
Which is what I believe you are looking for.
Furthermore, you can find all this and much more in The Eloquent ORM Guide on Laravel's site.
Maybe a bit old, but I've been in the same situation before.
At least in Laravel 5.2 (and up, presumably), the Eloquent relationships that you have defined should still exist. The objects that are returned should be Invoice objects in your case, you could check by dd($allInvoiceQuery); and see what the objects are in the collection. If they are Invoice objects (and you haven't done ->toArray() or something), you can treat them as such.
To force only having the properties in those objects that are related to the Invoice object you can select them with a wildcard: $allInvoicesQuery = Invoice::select('invoices.*')->join('contacts', 'contacts.id', '=', 'invoices.contact_id')->get();, assuming your corresponding table is called invoices.
Hope this helps.

Doctrine2 update relations many to many (without Symfony)

I have 2 Doctrine Entities with many-to-many relations. When I edit the first entity I want to be able to select the checkboxes that have the data from the 2nd entity to establish the joins for particular entry.
It works fine on creating a new Entry (using Array Collection), but when I want to edit an Entry - it adds the ones that I have selected without removing the previous choice (unchecking).
Which way would be the correct way to do that and how?
Remove all the Join table data for the Entry that is being updated,
then set the new data. (How can I remove it from the join table that
is not an Entity?)
Pass all the data from the 2nd Entity and remove
those that aren't checked (seems super-clumsy?)
Some other way I am not aware of?
I am not using Symfony, just Doctrine.
Doctrine makes working with the many-to-many associations quite easy. Your associations are stored into an ArrayCollection class that has some methods that can help you. First of all, check all the available methods for the ArrayCollection here (Doctrine API - ArrayCollection)
In your case, I'd use this approach: use the clear method on your ArrayCollection that contains the relationship with the 2nd entity and populate it again with the checked elements. After this, call the flush method on the entitymanager.
Another approach consists in filtering your collection (with the filter method) for getting a brand new ArrayCollection that contains only the elements that are checked. Like the first approach, associate this new collection to the relationship's ArrayCollection and call the flush method on the entitymanager.

Custom sorting/filtering in Doctrine 2 associations

I often sort associated entities by a foreign attribute and I'm wondering how to best handle it and still be able to use collections of the main entity. Let's have this example:
Author { $name, Comment[] $comments}
Comment { $name, Category $category}
Category { $name, $position }
I want $author->getComments() sorted by the comment's category position. In terms of DQL:
SELECT com.* FROM comment com JOIN com.category cat ORDER BY cat.position
Truth be told, my sorting criteria are rather more complex, this is just to get us started.
I know about #OrderBy annotation for *ToMany associations but it won't help me because ordering by a joined table's attribute is not supported.
I also know I can use DQL to fetch the comments but I need to access them in many places and I'd prefer referencing $author->getComments() rather than calling $commentRepository->findByAuthorSorted($author). I don't want having to remember to call my custom method to fetch the comments in the right way. I want it to be automatic.
I was considering somehow passing commentRepository to Author entity and use it in getComments() but I didn't figure out how to do it, plus it doesn't feel right to begin with.
I also thought about doing the sorting in PHP in getComments(). I don't think I can use Criteria + $author->comments->matching() because joined attributes don't seem to be supported. I'm fine with casting the collection to a read-only array but I hope there's a better, more Doctrine-way solution.
I don't want to work around the problem by adding $categoryPosition to each Comment.
I'd like to hear how you people handle this problem. I bet I'm not alone :-)

Doctrine Inheritance and MySQL Join Table Limit

I have a problem with 61 join table limit of mysql. I have 57+ different classes extending Base Class which contain association to comments, likes, tags. And MySQL is crashing when i get most commented. Doctrine has to join whole discriminator maps and comments itself and order by COUNT(comments).
Is there way to fix that ?
And is there another way to achieve comments for different types of entities without inheritance and copying same association all over again?
Here is sample schema of Entities. When I want to add new Entity Type with comments,likes I just extends BaseClass to receive these features.
If I understand correctly what you're trying to do is have many different entity types which can be commented on.
First thing to do would be to step back from Doctrine and thing about the simplest table structure you would need to accomplish this.
Something like this may suffice:
Comments
_______________________
| id | type | entity_id |
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
It is nice in Doctrine to have bi-directional relationships in your base class for convenience but sometimes they are not the best choice. Maybe it would simplify your architecture to perform a query directly on the comments table by entity type and id.
You may also want to consider removing the Base class and having each entity be standalone.
Since a blog post can exist in a context where it does not have comments (on a blog that doesn't allow commenting for example) then $blog->getComments() wouldn't make much sense.
Making this change you could do something like this instead:
$comments = $commentsRepository->findCommentsForEntity($entity);
$commentsCount = count($comments);
and the repository could generate the needed query passing the entity as the entity_id parameter and setting the required comment type based on the entity type.

Doctrine2 - (how to) fetch associated objects with main

Assume that i have an association mapping in Doctrine2.
How to define that i need to fetch all associated objects while querying the main one?
Practically, if i define (Main 1-* Sub) and then access all items in Sub collection, Doctine will execute a single DB request to get every one Sub object. I need to Sub objects be retrieved in a main query (JOIN).
Comments or (preferably) Doctrine RTMs welcome )
If you need that on constant basis (i.e. always fetch all association), declare your associations eager. Consult with Doctrine manual chapters 17-19 for details.
If you need it in just several pieces of code - use DQL for quering them (manual). This way you'll have to specify all your associations.
$query = $em->createQuery("SELECT u, a FROM User u JOIN u.address a WHERE a.city = 'Berlin'");
$users = $query->getResult();
If you are going to use that query often, it isa good idea to encapsulate it in the Entity class (simple, but not best solution), or create a Repository for an entity (a bit more involved, but it's "the right way").
UserRepository
public function getUsersFromCity($city): Collection
{
return $this->createQueryBuilder('u')
->leftJoin('u.address', 'a')
->addSelect('a')
->andWhere('a.city = :city')
->setParameter('city', $city)
->getQuery()
->getResult()
;
}

Categories