2016-07-29 29 views
1

我有一個擴展defaultLang的訪問REFFERENCE datarecord(讓我們稱之爲的MyStuff)與指向同其它分機(在的MyStuff的模型):extbase庫:當其它郎

/** 
* shoeref 
* 
* @var \My\MyCollection\Domain\Model\MyCollection 
*/ 
protected $shoeref; 

在模板我只需訪問它{mystuff.shoeref.image}

現在的問題是,MyCollection不存在於所有語言,而Mystuff存在於所有語言中。如示例所示,我們使用語言L=1,其中MyCollection不存在。在這種情況下,{mystuff.shoeref}爲NULL。

我想要做的就是訪問默認語言的MyCollection數據記錄(不管在這個語言中是否有MyCollection記錄,只要總是獲取默認語言的數據記錄就可以)。但我不知道如何做到這一點。

的替代方案,工作是創建一個數據記錄的all的langauge(L=-1)。但是我不喜歡這個解決方案,因爲每個編輯器都可以編輯這個數據記錄,它總是隻是默認語言數據記錄的一個副本。

回答

1

這裏的問題是,extbase將使用確實取得與郎UID 1所有子記錄,並忽略默認記錄(郎UID 0)

你可以嘗試設置$querySettings->setRespectSysLanguage(FALSE);但隨後你會得到所有的記錄,關於翻譯設置。

這仍然是extbase中的一個陷阱。

我會寫我自己的查詢語句來首先獲取父記錄,然後根據需要獲取子記錄,而不是依靠extbase魔術獲取機制。

一些信息:

https://wiki.typo3.org/Default_Orderings_and_Query_Settings_in_Repository

https://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/3-implement-individual-database-queries.html

+0

謝謝你的信息。幫助很多。在另一個地方,我有一個類似的問題。在存儲庫中,我嘗試從第一個鏈接中獲取函數:'$ query-> getQuerySettings() - > setSysLanguageUid(0);'這會導致'致命錯誤:調用未定義的方法... \ Typo3QuerySettings :: setSysLanguageUid 。從你的鏈接我看到文檔只用於'extbase 6.0 - 6.2'。但我有extbase 7.6。有沒有這個文檔的更新版本? – nbar

+0

好的,我直接檢查了QuerySettingsInterface.php,發現這個函數現在叫做'setLanguageUid()'。最近找不到任何文檔.. – nbar

0

一種方式來做到這一點是一種簡單的:

1)擴展模型 添加額外的getter模型

public function getParentRecord() { 
    return $this->_localizedUid; 
} 

這將返回父記錄

第二的UID)創建一個額外的視圖助手

創建文件typo3conf/ext/<your-ext>/Classes/ViewHelpers/ObjectViewHelper

<?php 

namespace <YourVendor>\YourExtKey>\ViewHelpers; 

class ObjectViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper { 

    /** 
    * @param int $id 
    * @return mixed 
    */ 
    public function render($id) { 
     $rawRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', '<tx_extkey_domain_model_fo>', 'uid=' . (int)$id); 

     $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'); 
     /* @var $dataMapper \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper */ 
     $dataMapper = $objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper'); 

     $records = $dataMapper->map('YourVendor\YourExtKey\Domain\Model\YourModel', array($rawRecord)); 
     $record = array_shift($records); 

     $this->templateVariableContainer->add('obj', $record); 
     $output = $this->renderChildren(); 
     $this->templateVariableContainer->remove('obj'); 
     return $output; 
    } 
} 

不要忘記採取的所有地方供應商,表名和模型!

3:採用視圖

現在改變你的模板,並使用新的視圖助手

<f:if condition="{model.parentRecord} > 0"> 
    <c:object id="{model.parentRecord}"> 
    <f:debug>{obj}</f:debug> 
    </c:object> 
</f:if> 

現在,您將得到父記錄