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
1 answer

Laravel 4 automatically create relationships in BaseModel - using "with"-array or scopes or what?

I have successfully created a few relations in a Model. This is the user-model belongsTo('UserGroup'); } ... }…
molerat
  • 946
  • 4
  • 15
  • 43
0
votes
1 answer

Laravel - Eager loaded object is not set

Something strange is happening with my laravel 4.0 application. I have the table users and the table profiles. In their models: // app/models/User.php: public function profile(){ return $this->hasOne('Profile', 'user_id'); } //…
cawecoy
  • 2,359
  • 4
  • 27
  • 36
0
votes
0 answers

Laravel 4: Updating table Joins with form model binding

I once did an eager loading query and it worked. Then using model binding i'm trying to update my two tables at once with no lucky. No error thrown, all validations are fine and when i tried to var_dump to see the values after validation , they are…
Daniel Chikaka
  • 1,432
  • 4
  • 17
  • 28
0
votes
2 answers

Laravel 4 eager loading

I have a User model, a Recipe model, and a Cookbook model. A User can 'own' many recipes. (User -OneToMany- Recipe) A user has 'only one' Cookbook. (User -OneToOne- Cookbook) Recipes that a user owns belong to his Cookbook. One Recipe can belong to…
Anindit Karmakar
  • 825
  • 1
  • 8
  • 26
0
votes
3 answers

Laravel 4.0 : Eager loading error - Trying to get property of non-object

I'm trying to utilize the eager loading feature in laravel but it doesn't work as i expected. When i try to check the profile of user who doesn't have information in Profiles table it throws "Trying to get property of non-object" instead of giving a…
Daniel Chikaka
  • 1,432
  • 4
  • 17
  • 28
0
votes
2 answers

Entity Framework - Eager loading, all properties to list

I have a simple project in which i have the following model. Customer 1--->N Addresses N<----1 City N<----1 Country ^ N| | AddressTypes 1-------+ Here is my code: public partial class…
Christos Baziotis
  • 5,845
  • 16
  • 59
  • 80
0
votes
0 answers

How to eagerly load unassociated active model records

I have two models: Hurricane and Problem and each Hurricane has many Problem Now in my database I want to let users update data about each Hurricane, so I have one copy of the data that is set by the admin and each user has access to this data. When…
0
votes
2 answers

Eager loading for globalize2 translations

i have 2 models - Issue and Answers (issue has many answers) and both have translations with globalize2. Every time i attempting to load Issue with answers via @issue = Issue.find(params[:id]) @answers = @issue.answers causes loading of…
Alexey Poimtsev
  • 2,845
  • 3
  • 35
  • 63
0
votes
1 answer

Laravel Eloquent - Eager loading

I have three tables: Client, User, Position. Client to User is many-to-many, Client to Position is one-to-many and User to Position is many-to-many. Now, a User can be part of two different Clients, and so for each, this user is assigned to some…
Kousha
  • 32,871
  • 51
  • 172
  • 296
0
votes
1 answer

Lazy Eager loading Grails GORM

Ok i'm wondering what is the best way to implement my scenario. My objects are as follows: ProjectCategory has many Projects. Projects have a ProjectStatus. ProjectStatus has a property called name and name can be either "Closed" or "Open"…
bouboule
  • 119
  • 9
0
votes
2 answers

Lazy Loading in Java Spring exception

In my controller I have the following code getting a specific presentation in my database, adding it as a model-attribute and returning the detail view: @RequestMapping(value = "/detail", method = RequestMethod.GET) public String detail(Model model,…
Jelle Elaut
  • 17
  • 1
  • 7
0
votes
1 answer

Hibernate LazyLoading no session

I have two jsps trying to access this database routine (via action class). When i have no try/catch/finally routine and no commiting transaction or closing of session then i can get the results but only the first time. When the next jsp tries to…
jsky
  • 2,225
  • 5
  • 38
  • 54
0
votes
1 answer

HIbernate FetchMode Join

I have a Parent class with a List of children. I would like to load the Parent by something other than the id e.g. by name...I am using criteria.setFetchMode("children",FetchMode.JOIN); and criteria.add(Restrictions.eq("name", name)) to eagerly load…
DD.
  • 21,498
  • 52
  • 157
  • 246
0
votes
1 answer

Select item by specific property

I'm working in Laravel 4, I can my models simplify to these: class DayVariant extends \Eloquent { public function chod() { return $this->belongsTo('Chod', 'chod_id'); } } class Chod extends \Eloquent { public function…
Martin Filek
  • 103
  • 1
  • 1
  • 6
0
votes
1 answer

L3 Select w/ Eager Loading

I'm using laravel 3 do to eager loading. What I've noticed is it works great! Until I use either ->select(['fields') or ->paginate( , ['fields']) to select a subset of the model's attributes. When I do this, it automatically culls any eager loaded…
Authman Apatira
  • 3,994
  • 1
  • 26
  • 33