Questions tagged [eager-loading]

Eager loading is a way to load objects of a certain class and a number of named associations in the same request.

When you load records from the database and also want to access the associated objects for each of these records, it’s a good idea to make use of eager loading. Eager loading reduces the amount of queries made to the database and therefore increases performance.

This is one of the easiest ways of to prevent the dreaded 1+N problem in which fetching 100 posts that each need to display their author triggers 101 database queries. Through the use of eager loading, the 101 queries can be reduced to 2.

# Rails 2
Blogpost.all(:include => :comments)

# Rails 3
Blogpost.includes(:comments).all

# Entity Framework
Blogpost.Include(bp => bp.Comments);
1515 questions
0
votes
2 answers

Possible to eagerload arrays instead of Models in Laravel Eloquent?

Is it possible to eager load an array of key => values using eloquent's with() or load() functions instead of a collection of Model objects? I only need a couple values from the relations, and don't need any object functionality, so I really don't…
EpicWally
  • 287
  • 3
  • 14
0
votes
1 answer

Laravel eager loading with field limit

I have three tables: users id name role_id password ... role_user id role_id user_id roles id name slug ... Now I want to get a user list with users.id,users.name, roles.id. That's my code: $query = User::with(['roles'=>function($q){ …
0
votes
2 answers

Nested Eager Loading with Joins in Laravel

I have the following Tables and their relationships products Table: id product_name product_price 1 Product A 2 USD 2 Product B 3 USD components Table id component_name component_price 1 Component A 5 USD 2 …
Saaz Rai
  • 262
  • 5
  • 15
0
votes
2 answers

When must we use eager loading in NHibernate? What is it's usage?

When must we use eager loading in NHibernate? What is it's usage?
masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
0
votes
0 answers

Laravel 5: Cannot access Eloquent relationship data

After trying out every solution I found on Google, I still cannot seem to access my relationship's data. I keep getting the following error: Trying to get property of non-object (View: /home/eneko/foundry/biome/resources/views/home.blade.php) Here…
Eneko
  • 61
  • 1
  • 5
0
votes
0 answers

Partial eager load Entity Framework

C# 4.0 + Entity Framework In the database project that I'm currently working on, we have a very large growing dataset of multiple tables. They are nested pretty deep (about 60 tables in all). Most of the time we need a large chunk of the data. I had…
Gary Smith
  • 61
  • 1
  • 10
0
votes
2 answers

Can I make a belongs_to association use eager loading by default?

I am connecting to one of my company's SQL Server databases, and trying to set up ActiveRecord so I can treat them just the same as Rails objects. I have these two models: class Change < ActiveRecord::Base belongs_to :affected_contact, class_name:…
PJSCopeland
  • 2,818
  • 1
  • 26
  • 40
0
votes
2 answers

Accessing Parent relations Laravel

I have a situation where I need to iterate through a models relationships (1 to many) and call a function on the children. i.e. $parent = ParentClass::find(1) $children = $parent->children; foreach($children as $child) { …
EpicWally
  • 287
  • 3
  • 14
0
votes
1 answer

Eager Loading on 3 Models in Laravel 5

I have 3 Models that are related so : "Tutorial" belongs To "Title" that belongs to "Course". Or (the other way) . A "Course" has many "Titles" that have many "Tutorials" . And I want to find a course based on its id and grab all its titles and…
arakibi
  • 441
  • 7
  • 24
0
votes
0 answers

Filtering related entities for a list of values

I have the following entity classes in my application: public class Project { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual ICollection ProjectManagers { get; set; } } public…
Impworks
  • 2,647
  • 3
  • 25
  • 53
0
votes
1 answer

Laravel Eager Loading Order By date/other fields and last record inside the relationship

I have 3 models with the fields LEAD id first_name surname //in lead model public function trxn() { return $this->hasMany('lead_trxn', 'lead_id', 'id'); } LEAD_TRXN id lead_id create_datetime expiration_datetime …
rfpdl
  • 956
  • 1
  • 11
  • 35
0
votes
0 answers

Can a Controller be Eagerly Loaded in AngularJS?

I am using UI router to define several states and their child views. I set their controllers accordingly as well. I'd like one or more of those controllers to be active before the view is loaded, because of data fetching and some other…
0
votes
2 answers

eager loading eloquent for relationship tables

how to load eloquent egar loading for below table. I want to get paul->donation[{eldery,material->clothes}, {orphan,material->aircon}] How can I get it by laravel eloquent eager loading ? Below is the database structure and…
Ryan Exlay
  • 361
  • 1
  • 3
  • 12
0
votes
1 answer

Simple.Data Eager Load SimpleQuery object

It appears that Simple.Data will lazy load by default. I simply want Simple.Data to query the database and put the results in an object. For example, as soon as this piece of code is executed the results from the database should be stored in…
MrWhippy
  • 15
  • 4
0
votes
1 answer

Rails ActiveRecord where condition eager load

I have a situation where I need to access 2 databases to construct a certain data set. This is a ruby code using ActiveRecord gem. Not RAILS. I am dealing with legacy databases and developing an extraction script using Ruby. From the first db users…