[TYPO3-mvc] associations

Jochen Rau jochen.rau at typoplanet.de
Thu Aug 27 00:06:48 CEST 2009


Hi Christoph.

Christoph Koehler wrote:
> Does extbase handle associations as follows?
>
> user has_many posts
> post belongs_to user
>
> All that I have in the database would be a user_id field in the post
> table; no changes to user required.

Yes. That is the recommended way (foreign key mapping).
The User object has a property "posts" holding the the related "Post" 
objects. But the Post object has no property like userId (or user_id).

> Now I would like to do this from a post to find other posts from the
> same user:
>
> $userposts = $post->user->posts();

This is not recommended. A possible solution could be to add a property 
"user" to the Post object holding the (single) User object. And then say 
inside your Controller

$post = $this->postRepository->findLatestPost(); // for example
$userPosts = $this->postRepository->findByUser($post->getUser());

> Could those methods be added to my models automatically, kind of like in
> Rails with has_many and belongs_to?

In Extbase (and FLOW3) Domain Models are plain PHP classes with no 
(public) magic methods.

Regards
Jochen

-- 
Every nit picked is a bug fixed



More information about the TYPO3-project-typo3v4mvc mailing list