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

Claus Due claus at wildside.dk
Wed Jun 22 17:24:52 CEST 2011


Hi Dmitri,

As always you're welcome :)

You probably already know this, but there's a chance this can break unexpectedly if you have any piece of code anywhere, which does not consider the type of an 1:n member property (array / storage) and tries to call ->count() etc. on your $this->types list. This could break if you update an object in FE - better test for that if you intend to have save/update features :)

-----
Venlig hilsen / Kind regards,

Claus Due
World Class Extraterrestrial
http://fedext.net/

On Jun 22, 2011, at 5:16 PM, Dmitri Pisarev wrote:

> Hey Claus,
> you are, as always, correct!
> The reason why this trick worked on images for me before is that I did explode on them and returned them as array.
> I modified the getter, following your advice, to this and it works:
> public function getTypes() {
> 		return $this->types->toArray();
> }
> 
> Thank you!
> 
> Dmitri.
> 22.06.2011 15:11, Claus Due пишет:
>> 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
>> 
> 
> _______________________________________________
> 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