[TYPO3-mvc] How to get first object of an 1:n relation

Claus Due claus at wildside.dk
Wed Jun 22 13:11:50 CEST 2011


Hi Dmitri,

ObjectStorage can be converted to array but cannot be accessed with numeric indexes like an array can. That's the reason why the .0 trick doesn't work on ObjectStorage.

You can do this though:

$images = $place->getImages()->toArray();
$this->view->assign('images', $images);

and then {images.0.filename} or whatever.

If you are looping {places} in Fluid then your only option is a ViewHelper which can read the first item off of an ObjectStorage and re-assign as a template variable (or simply return, if you only use it inline).

--

Med venlig hilsen / Kind regards

Claus Due
Code Monkey
--
Wildside A/S
Katrinebjergvej 113, DK-8200 Århus N
tlf. 86 12 64 65
http://www.wildside.dk

On Jun 22, 2011, at 12:50 PM, Dmitri Pisarev wrote:

> Hi!
> Sorry for bringing up the old thread.
> I stuck with the same problem - my code that used to work without problem is now broken. This used to work just fine: {place.images.0}
> Now I had to add another getter to the Place model and use the {place.firstImage}
> 
> What has gone wrong with the old approach?
> 
> Regards,
> Dmitri.
> 14.04.2011 17:22, Franz Koch пишет:
>> Hey,
>> 
>>> i've got an 1:n relation called anhaenge.
>>> How i get the first object of anhaenge in a fluid view?
>>> 
>>> Is there a way like
>>> {currentObject.anhaenge.first.propertyIlikeToDisplay} ?
>> 
>> not yet. AFAIK there is a review from Felix pending that's implementing
>> a "getFirst" method in objectStorages which should actually allow what
>> you currently try.
>> 
>> But until this is implemented I'd suggest to do one of the following:
>> 
>> a) simply loop over the "anhaenge"
>> b) add a method "getFirstAttachment" to your "currentObject"s domain
>> model that's doing the work for you
>> c) write a little viewHelper that's able to do what you need
>> 
>> c) would be my favorite because it's most flexible and can be reused for
>> other relations.
>> 
> 
> _______________________________________________
> TYPO3-project-typo3v4mvc mailing list
> TYPO3-project-typo3v4mvc at lists.typo3.org
> http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-project-typo3v4mvc



More information about the TYPO3-project-typo3v4mvc mailing list