Ad

Extending FE-Users With 1:n Relation

In my extension I am extending the FE-Users Model. Besides new properties (i.e. nickname) every FE-User can have multiple development entries. So I have an 1:n relation between the table fe_users and the table tx_squad_domain_model_development.

I started the extension with the extension builder. In the backend I can add the developments to the FE-User but in the frontend I don't get the developments of a specific FE-User. The error message is "Call to a member function getDevelopments() on null". What is going wrong?

I am using TYPO3 8. The extension key is squad. Here is my code:

ext_tables.sql:

#
# Table structure for table 'fe_users'
#
CREATE TABLE fe_users (
...
    nickname varchar(255) DEFAULT '' NOT NULL,
    developments int(11) unsigned DEFAULT '0' NOT NULL,
    tx_extbase_type varchar(255) DEFAULT '' NOT NULL,
...

);

#
# Table structure for table 'tx_squad_domain_model_development'
#
CREATE TABLE tx_squad_domain_model_development (
...
    uid int(11) NOT NULL auto_increment,
    pid int(11) DEFAULT '0' NOT NULL,
    frontenduser int(11) unsigned DEFAULT '0' NOT NULL,
    year varchar(255) DEFAULT '' NOT NULL,
...
);

TCA/Overrides/fe_users.php:

$tmp_squad_columns = [
...
    'nickname' => [
        'exclude' => true,
        'label' => 'LLL:EXT:squad/Resources/Private/Language/locallang_db.xlf:tx_squad_domain_model_frontenduser.nickname',
        'config' => [
            'type' => 'input',
            'size' => 30,
            'eval' => 'trim'
        ],
    ],
    'developments' => [
        'exclude' => true,
        'label' => 'LLL:EXT:squad/Resources/Private/Language/locallang_db.xlf:tx_squad_domain_model_frontenduser.developments',
        'config' => [
            'type' => 'inline',
            'foreign_table' => 'tx_squad_domain_model_development',
            'foreign_field' => 'frontenduser',
            'maxitems' => 9999,
            'appearance' => [
                'collapseAll' => 0,
                'levelLinksPosition' => 'top',
                'showSynchronizationLink' => 1,
                'showPossibleLocalizationRecords' => 1,
                'showAllLocalizationLink' => 1
            ],
        ],
    ],
...
];

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns('fe_users',$tmp_squad_columns);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
    'fe_users',
    'nickname, developments'
);

ext_typoscript_setup.txt

config.tx_extbase {
  persistence {
    classes {
      TYPO3\CMS\Extbase\Domain\Model\FrontendUser {
        subclasses {
          Tx_Squad_FrontendUser = Vendor\Squad\Domain\Model\FrontendUser          
        }
      }
      Vendor\Squad\Domain\Model\FrontendUser {
        mapping {
          tableName = fe_users
          // Don't use recordType
          recordType = 0
        }
      }
    }
  }
}

Model/FrontendUser.php

<?php
namespace Vendor\Squad\Domain\Model;

/**
 * FrontendUser
 */
class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
{
    ...
    /**
     * developments
     * 
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Vendor\Squad\Domain\Model\Development>
     * @cascade remove
     */
    protected $developments = null;

    /**
     * __construct
     */
    public function __construct()
    {
        //Do not remove the next line: It would break the functionality
        $this->initStorageObjects();
    }

    /**
     * Initializes all ObjectStorage properties
     * Do not modify this method!
     * It will be rewritten on each save in the extension builder
     * You may modify the constructor of this class instead
     * 
     * @return void
     */
    protected function initStorageObjects()
    {
        $this->developments = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
    }
   ...
    /**
     * Adds a Development
     * 
     * @param Vendor\Squad\Domain\Model\Development $development
     * @return void
     */
    public function addDevelopment(\Vendor\Squad\Domain\Model\Development $development)
    {
        $this->developments->attach($development);
    }

    /**
     * Removes a Development
     * 
     * @param \Vendor\Squad\Domain\Model\Development $developmentToRemove The Development to be removed
     * @return void
     */
    public function removeDevelopment(\Vendor\Squad\Domain\Model\Development $developmentToRemove)
    {
        $this->developments->detach($developmentToRemove);
    }

    /**
     * Returns the developments
     * 
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Vendor\Squad\Domain\Model\Development> $developments
     */
    public function getDevelopments()
    {
        return $this->developments;
    }

/**
     * Sets the developments
     * 
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Vendor\Squad\Domain\Model\Development> $developments
     * @return void
     */
    public function setDevelopments(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $developments)
    {
        $this->developments = $developments;
    }
...
}

Model/Development.php

namespace Vendor\Squad\Domain\Model;

/**
 * Development
 */
class Development extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
    /**
     * year
     * 
     * @var string
     */
    protected $year = '';

    /**
     * Returns the year
     * 
     * @return string $year
     */
    public function getYear()
    {
        return $this->year;
    }

    /**
     * Sets the year
     * 
     * @param string $year
     * @return void
     */
    public function setYear($year)
    {
        $this->year = $year;
    }
}

Controller/FrontendUserController.php

namespace Vendor\Squad\Controller;

/**
 * FrontendUserController
 */
class FrontendUserController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
    /**
     * frontendUserRepository
     * 
     * @var \Vendor\Squad\Domain\Repository\FrontendUserRepository
     * @inject
     */
    protected $frontendUserRepository = null;

    /**
     * @var \Vendor\Squad\Domain\Repository\DevelopmentRepository
     * @inject
     */
    protected $developmentRepository;

    /**
     * action list
     * 
     * @return void
     */
    public function listAction()
    {
        $frontendUsers = $this->frontendUserRepository->findAll();
        $this->view->assign('frontendUsers', $frontendUsers);
    }

    /**
     * action show
     * 
     * @param \Vendor\Squad\Domain\Model\FrontendUser $frontendUser
     * @return void
     */
    public function showAction(\Vendor\Squad\Domain\Model\FrontendUser $frontendUser)
    {
        $developments = $this->frontendUser->getDevelopments();
        $this->view->assign('developments', $developments);
        $this->view->assign('frontendUser', $frontendUser);
    }
...
}

Repository/DevelopmentRepository.php

namespace Vendor\Squad\Domain\Repository;
class DevelopmentRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
{
    }

Repository/FrontendUserRepository.php

namespace Vendor\Squad\Domain\Repository;

/**
 * The repository for FrontendUsers
 */
class FrontendUserRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
{
    }

Templates/FrontendUser/Show.html

...
<f:for each="{developments}" as="development">
            <tr>
                <td>{development.year}</td>
            </tr>
        </f:for>
...
Ad

Answer

Remove the $this-> in your controller action.

You have $developments = $this->frontendUser->getDevelopments();, but you don't have $this->frontendUser.

As you give the user as $frontendUser to your action, you need to use this user. $this->frontendUser is of course null.

So:

Change first line of your showAction to:

$developments = $frontendUser->getDevelopments();
Ad
source: stackoverflow.com
Ad