Typo3 Type3,Extbase:设置mm_关系
我只是尝试使用extbase,但在使用两个表(repostiories)和一个mm_表存储关系时遇到了问题 表格如下:Typo3 Type3,Extbase:设置mm_关系,typo3,extbase,Typo3,Extbase,我只是尝试使用extbase,但在使用两个表(repostiories)和一个mm_表存储关系时遇到了问题 表格如下: 可寻址 类别表 地址\类别mm表findByUid(2); //将类别附加到地址 //$go\u map\u address->setCategories($addressCategoryObj) 首先,检查模型内部的函数。您肯定有像addCategory()、getCategories()、RemoveCategority()或setCategories()这样的函数 第二
- 可寻址
- 类别表
- 地址\类别mm表findByUid(2);
//将类别附加到地址
//$go\u map\u address->setCategories($addressCategoryObj) 首先,检查模型内部的函数。您肯定有像addCategory()、getCategories()、RemoveCategority()或setCategories()这样的函数 第二,如果确实需要,不要使用makeInstance函数获取存储库(它已经过时),只需注入它或使用ObjectManager即可:
或/及/** * categoryRepository * * @var \TYPO3\GoMapsExt\Domain\Repository\CategoryRepository * @inject */ protected $categoryRepository;
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'); if (!is_object($this->categoryRepository)) { $this->categoryRepository = $objectManager->get('TYPO3\\GoMapsExt\\Domain\\Repository\\CategoryRepository'); }
因为Typo3可能有bug,所以我通常同时使用两者。在extbase中使用mm和其他关系非常简单。首先,您需要配置与TCA的关系,如果它在后端工作,那么您已经完成了这一部分。其次,您需要在模型中按如下方式配置属性:// get repo $addressRepo = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\GoMapsExt\Domain\Repository\AddressRepository'); $addressCategoryRepo = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\GoMapsExt\Domain\Repository\CategoryRepository'); // get category object (lead = 2)) $addressCategoryObj = $addressCategoryRepo->findByUid(2); // attach category to address //$go_map_address->setCategories($addressCategoryObj); <-- need to add category here
/** * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\GoMapsExt\Domain\Model\Category> * @lazy */ protected $categories; /** * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage $categories */ public function setCategories($categories) { $this->categories = $categories; } /** * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage */ public function getCategories() { // this prevents a fatal error if you have created your model with new instead of the objectManager if ($this->categories === null) { $this->categories = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage(); } return $this->categories; }
请记住,在向存储库添加类别后,您必须在存储库中保存$go\u map\u address,就像对对象进行其他更改一样谢谢。ect go_maps_ext中的添加/删除函数出错。已在本地对其进行了更改,并正在重新运行:-)$go_map_address->getCategories->attach($addressCategoryObj); foreach ($go_map_address->getCategories() as $category) { //$category }