TYPO3 Extbase:流畅的嵌套模型

时间:2012-07-02 13:15:09

标签: model nested typo3 fluid extbase

我正在为嵌套模型编写TYPO3的第一个extbase扩展。

以下模型存在:

作者 - 属性:名称和说明

新闻 - 属性:标题,日期,作者

作者被包含在这样的新闻模型中

/**
 * @var Tx_Extbase_Persistence_ObjectStorage<Tx_Simplenews_Domain_Model_Author>
 * @lazy
 * @cascade remove
 **/
protected $author = 0;

Fluid中的调试也有效,但作者对象有一个密钥uuid(例如“000000007d9412bd000000000217f7d0”),它会在每次请求时更改。

我只想在每条新闻上都显示作者的姓名。一个名字。

所以我必须循环通过作者对象,找到键并显示如下名称:

<f:for each="{oneNews.author}" as="author">
    <td>{author.name}</td>`
</f:for>

对此有更好的解决方案吗?

<f:for each="{news}" as="oneNews">
    <td>{oneNews.author.name}</td>
</f:for>

无效。

提前致谢!

3 个答案:

答案 0 :(得分:1)

得到了答案

我刚刚在News.php(模型)中更新了以下代码:

/**
* @var Tx_Extbase_Persistence_ObjectStorage<Tx_Simplenews_Domain_Model_Author>
* @lazy
**/
protected $author;

构造

public function __construct() {
    $this->author = new Tx_Extbase_Persistence_ObjectStorage();
}

吸气剂:

/**
 * @return Tx_Simplenews_Domain_Model_Author
 */
public function getAuthor() {
    $author = $this->author;
    $author->rewind(); // rewinds the iterator to the first storage element
    return $author->current(); // returns the current storage entry.
}

现在我可以使用{oneNews.author.name}

访问作者的姓名

答案 1 :(得分:1)

那么,为什么你首先使用objectStorage for Author? ObjectStorage用于存储多个对象。因此,只要您的新闻不能同时拥有两个或更多作者,您根本不需要对该属性的objectStorage。然后你不需要通过getAuthor()方法返回objectStorage的第一个对象。这个,顺便说一句,使得孔对象存储使用已经过时了。

我认为新闻只有一位作者。试试这个:

新闻模式:

/**
 * @var Tx_Simplenews_Domain_Model_Author
 **/
protected $author;

/**
 * @param Tx_Simplenews_Domain_Model_Author $author
 * @return void
 */
public function setAuthor(Tx_Simplenews_Domain_Model_Author $author) {
  $this->author = $author;
}

/**
 * @return Tx_Simplenews_Domain_Model_Author
 */
public function getAuthor() {
  return $this->author;
}

在您的流体模板中,您仍然拥有:

{oneNews.author.name}

所以,如果你不需要它,就不要使用objectStorage。

答案 2 :(得分:0)

我创建了一个虚拟新闻扩展,以您希望的方式列出新闻。见this git repository。我不知道你的案子出了什么问题。顺便说一句,我使用 Extension Builder 来创建扩展,只是更改了List.html的{​​{1}}模板。