[TYPO3-mvc] Casting objects
Xavier Perseguers
typo3 at perseguers.ch
Wed Jul 8 21:57:16 CEST 2009
Hi Jochen,
> have a look at the branch extbase/persistence_rewrite. There you find a
> modified Data Mapper with a much cleaner approach to reconstitute
> objects. Maybe this works for you.
Thanks for the hint. I had a look and I'm happy to see that we got the
same basic idea for the inner working. Though, I cannot use it as for my
case, I'm still not using the persistence part of Extbase and there
would be to much overhead adding what is missing to make it work.
I finally made a castToClass method that dynamically (I mean with
reflection) retrieve properties, search for their associated getter and
setter and recreate the subclass with all properties of the superclass
through calls to _reconstituteProperty() and a final call to
_memorizeCleanState(). I had to dynamically cast setter parameters to
base types (string, integer, ...) when it applies and made it in a
similar way than what you have in the branch. It now works well.
> The branch will be merged back soon.
Good to know. Seems that there will be a few changes as part of my code
did not work anymore with the branch, but for sure nothing to really
worry about ;-)
Regards
Xavier
>
> Regards
> Jochen
>
> Xavier Perseguers wrote:
>> Hi,
>>
>> Is there some official and more important working way of casting objects
>> when using Extbase?
>>
>> I have a class B (in extension FooBar) extending class A (from extension
>> Foo).
>>
>> I only want to extend it for my own purpose in EXT:FooBar, thus no use
>> of XCLASS.
>>
>> Now, I have to override a few methods such as FindOneBy... in order to
>> return a class B object instead of class A. What should I do?
>>
>> I tried to do this:
>>
>> public static function castToClass($class, $object) {
>> return unserialize(preg_replace('/^O:\d+:"[^"]++"/', 'O:' .
>> strlen($class) . ':"' . $class . '"', serialize($object)));
>> }
>>
>> and
>>
>> public function findOneByUid($uid) {
>> return FooBar_Helper::castToClass('B', parent::findOneByUid($uid));
>> }
>>
>> But it does not work:
>>
>> Warning: Invalid argument supplied for foreach() in
>> /path/to/extbase/Classes/DomainObject/AbstractDomainObject.php on line 58
>>
>> It fails after the call to unserialize in my castToClass function, when
>> magic method __wakeup on AbstractDomainObject is called because
>>
>> $GLOBALS['Extbase']['reconstituteObject']['properties']
>>
>> is not defined in my case.
>>
>> Any better idea?
>>
>
--
Xavier Perseguers
http://xavier.perseguers.ch/en
One contribution a day keeps the fork away
More information about the TYPO3-project-typo3v4mvc
mailing list