1

A Company object can have many Site Objects. For a given company I'd like to know the primary Site. (isPrimary is an attribute of Site).

I wrote a function in the Company class called getPrimarySite() and implemented it like this.

public function getPrimarySiteForCompany()
    {
        foreach($this->getSite() as $site)
        {
            if($site->isPrimary())
            {
                return $site;
            }
        }

        return false;
    }

Is this OK or is it better to write a custom repository function in CompanyRepository where I get the primary Site with DQL?

Simon
  • 1,643
  • 7
  • 30
  • 61
  • 3
    It's probably fine just the way it is. You can always use the profiler to determine if it's slowing down things. I used to spend a great deal of effort making custom queries in order to minimize lazy loading. But for my models, simple bench marks showed very little gain for the effort. – Cerad Jun 05 '13 at 21:14

2 Answers2

1

If a company can only have one primary site, the natural solution would be to add a new property Company::$primarySite to the Company entity. This would be a [One|Many]ToOne relation (depending on whether or not one site can belong to more than one company). Lookups will be fast, and your data layer then enforces the logic that a company can have only one primary site. That makes more sense than having an isPrimary flag on the site, and then having to make sure that two sites belonging to the same company don't have that flag set at the same time.

timdev
  • 61,857
  • 6
  • 82
  • 92
1

While it wouldn't be hard to write a custom repository function to join the data and prevent lazy loading, this is a good example of premature optimization.

Leave it as is for now and once everything works in production, you can go back to dev and use the profiler to see what could be improved performance-wise. Additionally, if you only expect a few Site objects, then it probably doesn't matter all that much, if at all.

Community
  • 1
  • 1
Pier-Luc Gendreau
  • 13,553
  • 4
  • 58
  • 69