Reputation: 2904
I have two entities a Task and a User. How can I create a "Query with a Join"?
How can I achive this with extbase (TYPO3 6.1). Is there anywhere a good tutorial with examples?
Upvotes: 1
Views: 5481
Reputation: 4558
In Extbase, you can query/address properties of child objects. So assuming that your task model has a relation to the user model, it is quite easy.
To list all tasks including their user names, just query the TaskRepository (inject it in your Controller):
$tasks = $this->taskRepository->findAll();
$this->view->assign('tasks', $tasks);
Now in your Fluid template, you can iterate over tasks:
<f:for each="{tasks}" as="task">
Name of task: {task.name}
Assigned user: {task.user.name}
</f:for>
You can use every property of your task model including properties of child objects.
To find a task by the assigned user, add a method to your taskRepository, e.g.:
public function findByTaskAssignedUser($user) {
$query = $this->createQuery();
$query->matching($query->equals('user.name', $user));
return $query->execute();
}
As a result you will get all task objects where the name of the assigned user matches $user.
You can also use $query->like for a LIKE statement and of course you better use the uid of the user to perform a safe comparison. Needless to say that you need to sanitize $user if it is a user-provided request.
The most condensed source of documentation is the Extbase & Fluid Cheat Sheet on http://www.typovision.de/fileadmin/slides/ExtbaseFluidCheatSheetTypovision.pdf. Please have a look at "Custom repository queries".
There is a free book "TYPO3 extensions with TYPO3 and Fluid", see the chapter about individual database queries: http://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/3-implement-individual-database-queries.html
Upvotes: 2
Reputation: 524
1. You can read all Tasks and add a reference in your TCA and your Domainmodel. If you use the Extensionbuilder this would be build automaticly. Than you can read the userinformation in the view like:
<f:for each="{Tasks}" as="task">
{task.reference.name}
</f:for>
2. You should have a reference in the Tasks entity. So you can write something like 'reference.name' in your taskrepository-function
$query = $this->createQuery();
$query->matching(
$query->equals('reference.name', 'Chuck')
);
$results = $query->execute();
Upvotes: 2