0
votes

I have two tables: fe_users (for my frontend users) and tx_somethingarticles_domain_model_article. A user should be able to "like" an article, so for every user I need the information if he already liked an article or not, but also do I need the information how many likes an article already has. So I believe what I need is a bidirectional mm relation, but I just cant seem to make it work. I have my User Entity, which looks like this:

/**
 * articleLike
 *
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Package\SomethingArticles\Domain\Model\Article>
 */
protected $articleLike = NULL;

public function addArticlesLike(\Package\SomethingArticles\Domain\Model\Article $articlesLike) {
    $this->articlesLike->attach($articlesLike);
}

public function removeArticlesLike(\Package\SomethingArticles\Domain\Model\Article $remove) {
    $this->articlesLike->detach($remove);
}

public function getArticlesLike() {
    return $this->articlesLike;
}

public function setArticlesLike(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $articlesLike) {
    $this->articlesLike = $articlesLike;
}

My Article Entity has this: protected $articlesLike = NULL;

public function getArticlesLike() {
    return $this->articlesLike;
}

with getArticlesLike I want to get all likes for an article. My tca.php for User has the column articles_like and looks like this:

'articles_like' => array(
        'exclude' => 1,
        'label' => 'LLL:EXT:something_articles/Resources/Private/Language/locallang_db.xlf:tx_somethingarticles_domain_model_user.articles_like',
        'config' => array(
            'type' => 'group',
            'internal_type' => 'db',
            'foreign_table' => 'tx_somethingarticles_domain_model_article',
            'allowed' => '*',
            'MM' => 'tx_somethingarticles_user_articleslike_article_mm',
            'MM_opposite_field' => 'articles_like',
            'size' => 10,
            'autoSizeMax' => 30,
            'maxitems' => 9999,
            'multiple' => 0,
        ),
    ),

and the tca.php for my articles:

        'articles_like' => array(
            'exclude' => 1,
            'label' => 'LLL:EXT:something_articles/Resources/Private/Language/locallang_db.xlf:tx_somethingarticles_domain_model_article.articles_like',
            'config' => array(
                'type' => 'group',
                'internal_type' => 'db',
                'allowed' => '*',
                'foreign_table' => 'fe_users',
                'MM' => 'tx_somethingarticles_user_articleslike_article_mm',
                'maxitems'   => 9999,
            ),
        ),

When I fetch a user and try something like $user->getArticlesLike() it works perfectly, I can also attach new likes, works like a charm. But when I try $article->getArticlesLike() ... its always empty. What am I missing?

I'm really desperate here, I've been trying to solve this for the last 10 hours, I can't seem to find a solution. Any help is appreciated greatly.

1
The relation between article and likes is 1:n. Therefore I guess that the column articles_like in the model article should not have a MM definition. Try to set foreign_table to tx_somethingarticles_user_articleslike_article_mm - Christoph
but what I wanted to do is have a table likes that is a m:n relation between article and user - how do I achieve this? - Chi
In context of this tutorial wiki.typo3.org/Inline_Relational_Record_Editing_1:n your article is a company and your likes are employees. The likes are stored in your mm relation table. So try to add the mm tables as foreign table to the article :) - Christoph
isn't that what I do in my tca.php ? 'foreign_table' => 'fe_users' - Chi
'foreign_table' => 'tx_somethingarticles_user_articleslike_article_mm' - Christoph

1 Answers

1
votes

Use the following column configuration for the article TCA:

'foreign_table' => 'tx_somethingarticles_user_articleslike_article_mm'

And remove the MM :)