xoops forums

luciorota

Module Developer
Posted on: 9/11 9:52
luciorota
luciorota (Show more)
Module Developer
Posts: 197
Since: 2007/4/20
#1

xoopsClone issues

Someone can explain to me the correct use of the xoopsClone() method?

geekwright

Quite a regular
Posted on: 9/12 2:44
geekwright
geekwright (Show more)
Quite a regular
Posts: 276
Since: 2010/10/15
#2

Re: xoopsClone issues

There are not a lot of examples for xoopsClone() that I can find.

Assuming $existingObject is a child of XoopsObject
$newObject $existingObject->xoopsClone();

will create a cloned copy of $existingObject.

It is probably wise to instead use PHP's clone like this:
$newObject = clone $existingObject;


It is possible that an object with custom properties might not produce a proper clone with xoopsClone(), while the PHP clone would clone any custom properties.

The clone, in either case, has all the vars copied, and is set as a new object.

One potential problem is that the XoopsObject has no internal knowledge of it's primary key, so while the object is marked new, it still has any auto-increment PK set. You can work around this issue by doing a $newObject->destroyVars('key-variable-name'); to clean up after a clone.

It probably would have made more sense for a xoopsClone() method to be in the handler rather than the object, but this choice was made a long time ago.

luciorota

Module Developer
Posted on: 9/12 13:58
luciorota
luciorota (Show more)
Module Developer
Posts: 197
Since: 2007/4/20
#3

Re: xoopsClone issues

Thanks

Quote:

One potential problem is that the XoopsObject has no internal knowledge of it's primary key, so while the object is marked new, it still has any auto-increment PK set. You can work around this issue by doing a $newObject->destroyVars('key-variable-name'); to clean up after a clone.


In fact this is a problem I encountered

Also $vars[$key]['changed'] property si cloned as false, for this reason the cloned object cannot be correctly stored in the database.

In your opinion, can it make sense to correct the xoopsClone() method to create working objects?
Thx