[TYPO3-mvc] Database queries/mapping with associated tables

Michael Ruhdorfer michael.ruhdorfer.1line at gmail.com
Wed Aug 18 11:37:31 CEST 2010


hi franz,


> i need a list with all employees something like that:
>>
>> <:f foreach="{company.branches}" as="{branch}">
>>     employee {employees.name}: Richard Meyer
>>     company {company.name}: Industrial Electronics
>>     country {company.country.name}: Netherlands
>>     region {company.country.regions.name}: Friesland
>>     branches {branch.name}: Electronics
>>     ...
>>     branches {branch.name}: Electricity
>> </f:foreach>
>>
>
> Somehow your example doesn't match your description. If you need a list of
> all employees, why do you loop the branches and not the employees?
> Where do your employees come from? Do you prepare those in your controller?
> If so, why? In my eyes it looks like your domain is not well formed.


yeah sry, of course the emplyees are looped - i just wanted to show that one
employee can be displayed more often due to the relation that one company
can have more branches (which is also passed on the employee).

my employees are in the employees table - they are called in the controller
over the company:
$this->companyRepository =&
t3lib_div::makeInstance('Tx_MyExtension_Domain_Repository_CompanyRepository');
with $this->companyRepository->findAll() i can access the employees which
are mapped in the model on the object:
    /**
     * @return
Tx_Extbase_Persistence_ObjectStorage<Tx_MyExtension_Domain_Model_Employee>
employees
     */
    public function getEmployees() {
        return $this->employees;
    }



>  that works so far
>> {branch.name} is only once inside the foreach - the data inside the {}
>> braces is just for relation demonstration
>>
>>
>> so because of the mapping it works with
>> $this->companyRepository->findAll()
>> to access data the following way:
>> $name = $company->getName();
>>
>> $employees = $company->getEmployees();
>> foreach($employees as $e)
>>     $e->getName();
>> ...
>>
>
> why do you need to do this in your controller? It again looks like a design
> issue in your domain missing some back references.
>

my controller looks like this:

$companyQuery = $this->companyRepository->findAll();
$employees = $this->companyRepository->listAllEmployees($companyQuery);
$this->view->assign('employees', $employees);

and the repository:

    public function listAllEmployees($companyQuery)
    {
        foreach ($companyQuery as $c)
        {
            //filter information of employees of each company
            foreach($c->getEmployees() as $e)
            {
                //looping branches
                foreach($c->getBranches() as $b)
                {
                    $employee[$j]['employee'] = $e->getName();
                    $employee[$j]['company'] = $c->getName();
                    $employee[$j]['country'] = $c->getCountry()->getName();
                    $employee[$j]['region'] = $c->getRegion()->getName();
                    $employee[$j]['branches'] = $b->getName();
                    $j++;
                }
            }
        }
        return $employee;
    }

and the view:

        <f:for each="{employees}" as="e">
            <tr>
              <td>{e.employee}</td>
              <td>{e.company}</td>
              <td>{e.country}</td>
              <td>{e.region}</td>
              <td>{e.branches}</td>
            </tr>
        </f:for>



>  now i want to filter the results by countries / branches / regions
>> i have been thinking on something like this:
>>
>> $query->matching(
>>     $query->equals('tx_myextension_domain_model_company.country.uid',
>> $countryId)
>> );
>>
>> but it doesn't work... i have to do that in the repository - not in the
>> model, right?
>>
>
> What do you like to filter? Companies or employees? For companies it would
> look like this:
>
> $query->matching(
>      $query->equals('country', /*Tx_YourExt_Domain_Model_Country or a plain
> UID*/ $country)
> );
> --
> kind regards,
> Franz Koch
>
> _______________________________________________
> TYPO3-project-typo3v4mvc mailing list
> TYPO3-project-typo3v4mvc at lists.typo3.org
> http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-project-typo3v4mvc
>

i have a search formular and i want to filter by countries, branches and
regions - where all three types are optional
i want to filter employees of the company

thx, michael


More information about the TYPO3-project-typo3v4mvc mailing list