Limit Clause While Fetching Child Records in Doctrine - doctrine-query

I have two tables Entity and Message. as each entity can have many messages so it's like parent child relation. I dont want to use doctrine custom query so I am loading entity message like
$this->currentUser = Doctrine_Core::getTable('Entity')->find($entityID);
and then "$currentUser->getMessage()" gives me all messages of that person.
So is there any way to get only few message? let's say 3 message only? How can I use LIMIT claue in this case?
Thanks
Ali

Related

Laravel with relationship

I have an order and transaction model in which 1 order can have many transactions.
However, there are certain areas in which I will only want to return certain rows of the transactions table.
I did something like this:
Order::with('transactions')->whereHas('transactions',function($query){
$query->select(array('A','B'));
}))
Where A and B are the columns of the transactions.
How do I get this to work without changing the name of the relationship?

JPA OneToMany and Getting children with a single query

I notice that in hibernate, it queries the child collections of entities an entity at a time. So, for example, I have a Person entity with a OneToMany relationship with PhoneNumber as well as a OneToMany relationship with EmailAddress. If I do a simple query on the Person entity that returns 1,000 people then hibernate will make 1,000 queries to EmailAddress and 1,000 queries to PhoneNumber. Let's forget about eager or lazy fetching for a minute and assume I will be accessing the phone and email collections of every person.
This seems like a naive implementation. Is there a simple way to change this so there is only 1 query into PhoneNumber and only 1 query into EmailAddress? These should be put into a map keyed by their Person foreign key so they are easily retrieved by the Person getter methods.
Any thoughts besides doing a brute force query into the session cache for emails and phone numbers BEFORE executing the Person query?
TIA, let me know if you need additional data.
Apart for doing the queries by yourself, you could simply enable batch fetching, as described in the documentation:
You can also enable batch fetching of collections. For example, if each Person has a lazy collection of Cats, and 10 persons are currently loaded in the Session, iterating through all persons will generate 10 SELECTs, one for every call to getCats(). If you enable batch fetching for the cats collection in the mapping of Person, Hibernate can pre-fetch collections:

stop doctrine from fetching related entities

When usually fetching an entity from database with doctrine, you get all the related entities as actual classes, which causes a huge JOIN query, if you have lots of relations.
But sometimes I just want to get the actual object, not all the associated entities, just their IDs.
Is it possible to tell doctrine to just fetch the main entity and leave alone the relations?
Update: Sorry, missed the version: I'm using Doctrine 1.2 on a old project.
By default Doctrine use "lazy-loading": it will not retrieve the associated entities if you do not try to access them.
If you just use the ID of the main entity, it will never retrieves the associated entities.
If you want it to be even more lazy, try using the EXTRA_LAZY param.

Performace issue using Foreach in LINQ

I am using an IList<Employee> where i get the records more then 5000 by using linq which could be better? empdetailsList has 5000
Example :
foreach(Employee emp in empdetailsList)
{
Employee employee=new Employee();
employee=Details.GetFeeDetails(emp.Emplid);
}
The above example takes a lot of time in order to iterate each empdetails where i need to get corresponding fees list.
suggest me anybody what to do?
Linq to SQL/Linq to Entities use a deferred execution pattern. As soon as you call For Each or anything else that indirectly calls GetEnumerator, that's when your query gets translated into SQL and performed against the database.
The trick is to make sure your query is completely and correctly defined before that happens. Use Where(...), and the other Linq filters to reduce as much as possible the amount of data the query will retrieve. These filters are built into a single query before the database is called.
Linq to SQL/Linq to Entities also both use Lazy Loading. This is where if you have related entities (like Sales Order --> has many Sales Order Lines --> has 1 Product), the query will not return them unless it knows it needs to. If you did something like this:
Dim orders = entities.SalesOrders
For Each o in orders
For Each ol in o.SalesOrderLines
Console.WriteLine(ol.Product.Name)
Next
Next
You will get awful performance, because at the time of calling GetEnumerator (the start of the For Each), the query engine doesn't know you need the related entities, so "saves time" by ignoring them. If you observe the database activity, you'll then see hundreds/thousands of database roundtrips as each related entity is then retrieved 1 at a time.
To avoid this problem, if you know you'll need related entities, use the Include() method in Entity Framework. If you've got it right, when you profile the database activity you should only see a single query being made, and every item being retrieved by that query should be used for something by your application.
If the call to Details.GetFeeDetails(emp.Emplid); involves another round-trip of some sort, then that's the issue. I would suggest altering your query in this case to return fee details with the original IList<Employee> query.

How to retrieve a Doctrine record with all relational records?

I was wondering if there is a way to get a record with all relational data, something like a 'Deep-Fetch'
So if a model Child were related to another model Parent,
can we fetch Child & then access Child->Parent->name thru a single query?
Doctrine today fires a query whenever a relationship is accessed. Is this too costly? does it need to be optimizeD?
thanks
Doctrine automatically hydrates related objects when you select fields from that relations:
Doctrine_Query::create()
->select('a.*, c.*)
->from('Article a')
->innerJoin('Category c');
In this example both Article and Category objects are being hydrated (no additional queries are made).

Resources