[TYPO3-dev] Re: [TCA/Extbase] Bidirectional IRRE and 'weak entities'

Daniel Haring daniel at haring.co.at
Mon Jan 11 17:00:14 CET 2016


OK, to be honest, I've expected such a high count of qualified answers ;)

However, as mentioned before I achieved this by providing a special »find« method (see beneath).

Although I'm not entirely satisfied with this solution, it works quite well.

Thanks anyway.

Topic can be closed.

Cheers,
Daniel


[php]
namespace Vendor\MyExt\Domain\Repository;

use \Vendor\MyExt\Utility\DomainObjectUtility;





/**
 * Repository for ingredients.
 *
 * @since 1.0.0
 */
class IngredientRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
    
    
    
    
    
    /**
     * Internal table class mapping.
     * 
     * @var array
     */
    protected $classMapping = [
        'tx_myext_domain_model_product' => \Vendor\MyExt\Domain\Repository\ProductRepository::class,
        'tx_myext_domain_model_product_variety' => \Vendor\MyExt\Domain\Repository\ProductVarietyRepository::class
    ];
    
    
    
    
    
    /**
     * Returns all products and product varieties which contain the given nutrient.
     * 
     * @param \Vendor\MyExt\Domain\Model\Nutrient $nutrient The nutrient to match against
     * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage The matching results
     */
    public function findProductsByNutrient(\Vendor\MyExt\Domain\Model\Nutrient $nutrient) {
        
        /* @var $objectStorage \TYPO3\CMS\Extbase\Persistence\ObjectStorage */
        $objectStorage = $this->objectManager->get(
                \TYPO3\CMS\Extbase\Persistence\ObjectStorage::class);
        
        /* @var $query \TYPO3\CMS\Extbase\Persistence\QueryInterface */
        $query = $this->createQuery();
        
        $query->matching($query->logicalAnd(
                $query->equals('uidLocal', $nutrient), 
                $query->greaterThan('uidForeign', 0),
                $query->equals('fieldname', 'ingredients'),
                $query->in('tablenames', \array_keys($this->classMapping))));
        
        /* @var $nutrientReference \Vendor\MyExt\Domain\Model\Ingredient */
        foreach($query->execute() as $nutrientReference) {
            
            /* @var $repository \TYPO3\CMS\Extbase\Persistence\Repository */
            $repository = $this->objectManager->get(
                    $this->classMapping[$nutrientReference->getTablenames()]);
            
            /* @var $product \TYPO3\CMS\Extbase\DomainObject\AbstractEntity */
            $product = $repository->findByUid(
                    (int)$nutrientReference->getUidForeign());
            
            if(DomainObjectUtility::isEnabledDomainModel($product) 
                    && (!$product instanceof \Vendor\MyExt\Domain\Model\Product 
                            || !\count($product->getVarieties()))) {
                
                $objectStorage->attach($product);
                
            }
            
        }
        
        return $objectStorage;
        
    }





}
[/php]



More information about the TYPO3-dev mailing list