0
votes

I'm writing my first extbase extension for TYPO3 with nested models at the moment.

Following models exists:

Author - attributes: name and description

News - attributes: title, date, author

The author is included in the news model like this

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

Debugging in Fluid also works, but the author object has an key uuid ("000000007d9412bd000000000217f7d0" for example), which changes on every request.

I just want to show the author's name on every news. One name.

So i have to loop trough the author object, to find the key and display the name like this:

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

Is there a better solution for this?

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

won't work.

Thanks in advance!

3
Do you have appropriate getters for the $authors? Post the whole News model class.Rico Leuthold
You might want to compare your code to the news extension: git.typo3.org/TYPO3v4/Extensions/news.gitpgampe
This plugin is useful. I'll try. Thank you!Patrick Hafner

3 Answers

1
votes

Got the answer

I just updated following code in News.php (Model):

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

Constructor:

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

Getter:

/**
 * @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.
}

Now I can access the author's name with {oneNews.author.name}

1
votes

So, why do you use an objectStorage for Author in the first place? ObjectStorages are for storing multiple Objects. So as long as a news of yours cant have two or more authors at the same time, you dont need an objectStorage at all for that property. Then you dont need to just return the first object of the objectStorage via your getAuthor() method. This, btw, makes the hole object Storage usage obsolete.

I assume a news only has one author. Try this:

News Model:

/**
 * @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;
}

In your Fluid-Template you still have:

{oneNews.author.name}

So, just dont use an objectStorage if you dont need it.

0
votes

I created a dummy news extension which lists the news the way you want it. See this git repository. I don't know what went wrong in your case. BTW, I used the Extension Builder to create the extension and just changed the List.html template for the News.