[TYPO3-mvc] Cloning properties in setter and getter methods of models (blog example)

Thomas "Thasmo" Deinhamer thasmo at gmail.com
Mon Feb 14 09:49:48 CET 2011


Sebastian Kurfürst wrote:
> You should only clone if you have to return an ObjectStorage and expect
> it to be modified afterwards. In order to make sure these changes to the
> ObjectStorage are not persisted, you should clone it.
>
> So, this is what you should do inside*Value Objects*  (as VOs should not
> change after creation); but inside entities, it's normally not needed to
> clone the objects for the above reasons.
>
> I'm not sure however if the blog example does this consistently;-)

Hi Sebastian!

Thanks for bringing in your feedback.

What if the addObject($object) method of the model
grabs the $object, does some mighty magic with it
and then attaches it to the object storage?

Example:

public function addObject($object) {
	$object = $this->setStoragePage($object);
	$this->objects->attach($object);
}

public function getObjects() {
	return [clone?] $this->objects;
}

Would it be good to clone the return value
of the getter method or not? And if not, how
could I ensure, that the developer _must_ use
the addObject() method, to add objects?

What I'm thinking about is a method like
"persistObject()" which is triggered when
the parent object gets persisted:

public function persistObject() {
	foreach($this->objects as $object) {
		$object = $this->setStoragePage($object);
	}
}

In this case it doesn't matter if I use this:

$objects = $parentObject->getObjects();
$objects->attach($newObject);

or this:
$parentObject->add($newObject);

...because the mighty magic happens when the object gets persisted.
(The only thing bad about this is, that those $objects will have a
wrong or no storage page assigned, until they get persisted. So you
can't rely on a correct storage page, because the lazy properties
are not yet correct. Hopefully that's understandable.)

Or am I totally wrong or missing something?

Thanks,
Thomas


More information about the TYPO3-project-typo3v4mvc mailing list