Search code examples
typo3typo3-8.x

Add own sys_category to record with extbase


I made the table of one of my models categorizable which works fine if I set categories in the Backend. If I try to add categories via a frontend form I always get the error:

Call to a member function attach() on null

and have no clue why that is so. Maybe anyone of you can help.

In controller I try to add as usual, find the category

$videoCat = $this->categoryRepository->findByUid(28);

and add it like that

$this->video->addTxVideoCat($videoCat);

That's where the error occurs. Find below how I added the category to the model.


ext_tables.sql `tx_video_cat int(11) DEFAULT '0' NOT NULL,`

extended tca in TCA/Overrides/sys_template.php

    \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::makeCategorizable(
        'addvideo',
        'tx_addvideo_domain_model_video',
        // Do not use the default field name ("categories") for pages, tt_content, sys_file_metadata, which is already used
        'tx_video_cat',
        array(
            // Set a custom label
            'label' => 'LLL:EXT:addvideo/Resources/Private/Language/locallang.xlf:video_categories',
            // This field should not be an exclude-field
            'exclude' => FALSE,
            // Override generic configuration, e.g. sort by title rather than by sorting
            // string (keyword), see TCA reference for details
            'l10n_mode' => 'exclude',
            // list of keywords, see TCA reference for details
            'l10n_display' => 'hideDiff',
        )
    );

Created extended category repository

    namespace Pixelink\Addvideo\Domain\Repository;
    
    class CategoryRepository extends \TYPO3\CMS\Extbase\Domain\Repository\CategoryRepository

Created extended model

    namespace Pixelink\Addvideo\Domain\Model;
      
    class Category extends \TYPO3\CMS\Extbase\Domain\Model\Category
    {}

Category mapping in typoscript

    plugin.tx_addvideo{
      persistence {
        classes{
          Pixelink\Addvideo\Domain\Model\Category {
            mapping {
              tableName = sys_category
              columns {
    
              }
            }
          }
        }
      }
    }

and in Model\Video.php I added the following


    /**
     * txVideoCat
     *
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Pixelink\Addvideo\Domain\Model\Category>
     */
    protected $txVideoCat = null;

    /**
     * Get categories
     *
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Pixelink\Addvideo\Domain\Model\Category>
     */
    public function getTxVideoCat()
    {
        return $this->txVideoCat;
    }

    /**
     * Set categories
     *
     * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $txVideoCat
     */
    public function setTxVideoCat($txVideoCat)
    {
        $this->txVideoCat = $txVideoCat;
    }

    /**
     * Add category to a post
     *
     * @param \Pixelink\Addvideo\Domain\Model\Category $txVideoCat
     */
    public function addTxVideoCat(\Pixelink\Addvideo\Domain\Model\Category $txVideoCat)
    {
        $this->txVideoCat->attach($txVideoCat);
    }

Solution

  • You should initialize your property in your Video model constructor:

    public function __construct()
    {
        $this->txVideoCat = new ObjectStorage();
    }