0
votes

Is it possible to inverse a 1:1 Relation without adding a second field into DB in Extbase?

Example: The extension has Contact-Persons which can have an fe_user. The Contact-Person-Domain-Model is the owning site of the relation.

Domain-Model

Now you can use $contactPerson->getFrontendUser();

Is there any way to add an inversed property to FrontendUser without adding it to the DB?
So you can use $frontendUser->getContactPerson(),
or even more important: $frontendUserRepository->findByContactPerson();


I tried adding the property to the FrontendUser-Model:

/**
 * FrontendUser
 */
class FrontendUser extends \TYPO3\CMS\Extbase\Domain\Model\FrontendUser
{

    /**
     * @var \Vendor\ExtKey\Domain\Model\ContactPerson
     */
    protected $contactPerson = null;
}

And overriding the fe_users TCA:

$GLOBALS['TCA']['fe_users']['columns']['contact_person'] = array(
    'exclude' => 1,
    'label' => 'LLL:EXT:ExtKey/Resources/Private/Language/locallang_db.xlf:tx_ExtKey_domain_model_contactperson',
    'config' => array(
        'type' => 'inline',
        'foreign_table' => 'tx_ExtKey_domain_model_contactperson',
        'foreign_field' => 'frontend_user',
        'minitems' => 0,
        'maxitems' => 1,
    ),
);

But when I call:

/**
 * The repository for Customers
 */
class FrontendUserRepository extends \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserRepository
{
    /**
     * @param \Vendor\ExtKey\Domain\Model\ContactPerson $contactPerson
     * @param boolean $ignoreEnableFields
     * @param boolean $respectStoragePage
     * @return object
     */
    public function findByContactPerson(ContactPerson $contactPerson, $ignoreEnableFields = false, $respectStoragePage = true){
        $query = $this->createQuery();
        $query->getQuerySettings()
            ->setIgnoreEnableFields($ignoreEnableFields)
            ->setRespectStoragePage($respectStoragePage);

        $query->matching($query->equals('contactPerson', $contactPerson));
        return $query->execute()->getFirst();
    }
}

It creates following SQL-Error:

Unknown column 'fe_users.contact_person' in 'where clause'

2

2 Answers

2
votes

Computed bidirectional 1:1 relations are not supported in TYPO3, only m:n relations support that with an extra definition in TCA - which also requires an additional field on the opposite site of the relation.

Concerning you scenario, you have to create the additional property and database field in an extended FrontendUser domain model on your own.

0
votes

There is a possibility like described here: http://www.oliver-weiss.com/blog/einzelansicht/article/relationen-in-typo3-teil-1-11-relation/News/detail/ . Additionally, instead of having an "inline" relation on both sides, this also works if only one side is "inline" and the other side is "select". But the single (inverted) "inline" relation needs to have "foreign_field" defined. Following this, the "foreign" uid is only stored on one side of the relation.