OK, now I understand. I always do that a bit different, but take care: the 
way I do it is not usual (and maybe seen as completely wrong by some). I 
try to avoid as much logic as possible in controllers (or its 
"subroutines", called: Services); I think that is a kind of procedural 
style of coding, common in Symfony. I'm stubborn and have weird ideas about 
it, as you see... 

Instead, I try to build a domain-model with rich behaviour. That means: 
giving the entities all methods they need. In this case: I would make a 
method in the Company-entity to (eagerly) retrieve the categories and their 
items. So, a getCategoriesJoinedItems() in the Company-entity. But then 
probably with a differnt name, that is more descriptive of what I want to 
accomplish with that. And if you need a Service, I then would only use that 
entity in it, not the repositories. But I would try to refactor it in such 
a way that I don't need a Service for that Company. 

But if you code in the Symfony-way, with those Services.... then I'm 
probably not the right person to answer this. 


On Monday, 14 April 2014 11:21:49 UTC+2, Dominik Barann wrote:
>
> I'm sorry for that weird formulation. I think some code will bring 
> clearness...
>
> class CompanyService {
>     private $companyRepository;
>
>     private $categoryRepository;
>
>     // setters and getters for repos
>
>     public function getCategoriesJoinedItems(Company $company) {
>         return 
> $this->getCategoryRepository()->findByCompanyJoinedItems($company);
>     }
>
>     public function getCompany($id) {
>         return $this->getCompanyRepository()->find($id);
>     }
> }
>
> The repos are injected via a factory class. So my service depends on many 
> repos, depending on how many different associations i want to receive via 
> the service. So if i have to use my service in a request, for e.g. 
> receiving a company, all the repos are injected but i dont really need 
> them. But maybe there's another mistake in my architecture? 
>
> Greets
>
>
> Am Montag, 14. April 2014 10:44:34 UTC+2 schrieb Herman Peeren:
>>
>> A rule of thumb is: put the method in the reository that gives the 
>> corresponding entities back. So, in you last example it is in the 
>> category-repo, bcause you retrieve categories, and in the example in your 
>> first posting in the opening hour repo, giving back opening hours. Just 
>> cannot find where I recently read about this.
>>
>> However, I don't understand your remark " i would have to inject all the 
>> repositories into my company service to receive the associations with its 
>> own associations" when using the category-repository. 
>>
>> On Monday, 14 April 2014 09:55:14 UTC+2, Dominik Barann wrote:
>>>
>>> Hey,
>>>
>>> the main problem is not to order the result. Maybe the following example 
>>> would be better:
>>>
>>> Company 1...n Category
>>> Category 1...n Items
>>>
>>> To receive the categories of a company with its items i could do
>>>
>>> foreach ($company->getCategories() as $category) {
>>>     foreach ($category->getItems() as $item) {
>>>         ...
>>>     }
>>> }
>>>
>>> The problem here is, that this will result in a lot of database queries 
>>> because the associations not loaded until i need them. I know that i could 
>>> use the annotation "fetch=EAGER" in my entity but i dont want to load the 
>>> categories all the time. So i have to write a method in a repository to 
>>> receive the categories with its items in one database query using joins. 
>>> But the question is in which repo i have to write that method? The company 
>>> or the category repo?
>>>
>>> If it would be category repo with a method called 
>>> findByCompanyJoinedItems(Company $company) i would have to inject all the 
>>> repositories into my company service to receive the associations with its 
>>> own associations. 
>>>
>>> The other way would be a method called 
>>> findCategoriesJoinedItemsByCompany(Company $company) in the company repo 
>>> and only inject this repo into my service. But somwhow that feels weird...
>>>
>>> Hat it being a perfectionism :(
>>> Greets Dominik
>>>
>>> Am Freitag, 11. April 2014 17:49:27 UTC+2 schrieb Holtkamp:
>>>>
>>>> @Nima, I meant to define it in the association metadata as described 
>>>> quite well here:
>>>> - 
>>>> http://doctrine-orm.readthedocs.org/en/latest/tutorials/ordered-associations.html
>>>> - 
>>>> http://doctrine-orm.readthedocs.org/en/latest/reference/annotations-reference.html#annref-orderby
>>>>
>>>> As soon as an Entity is loaded, this particular association would be 
>>>> ordered as requested. So no need to 'think' about it anymore as a 
>>>> developer, which can be useful in specific cases, which this was.
>>>>
>>>> have a nice weekend!
>>>>
>>>>
>>>> On 11 April 2014 14:52, Nima Sadjadi <[email protected]> wrote:
>>>>
>>>>> Menno,
>>>>> Do you mean somethibg like this?
>>>>> find(...)->orderedBy(...)
>>>>> Does this method work with find() too?
>>>>>
>>>>> -- 
>>>>> You received this message because you are subscribed to the Google 
>>>>> Groups "doctrine-user" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>>> an email to [email protected].
>>>>> To post to this group, send email to [email protected].
>>>>> Visit this group at http://groups.google.com/group/doctrine-user.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>>

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to