[TYPO3-mvc] [Extbase] Object doesn't get initialized.

Franz Koch typo3.RemoveForMessage at elements-net.de
Fri Oct 15 10:50:25 CEST 2010


Hi,

> Try to remove the assignment in your constructor: $this->items = new Tx_Extbase_Persistence_ObjectStorage;
> Normally it should get automagically populated by your repository.

just for clarification, AFAIK you should initiate objectStorages in the 
constructor of your model. This is needed for cases where a new object 
is created and only populated partly. That way you don't have to check 
if your property is really a objectStorage before you can use some of 
it's methods anywhere in your code, but can use it right away, like:

-------------------------------
$myObject = t3lib_div::makeInstance('Tx_MyExt_Domain_Model_Foo');
$myObjectService->doSomething($myObject);

$mySecondObject = $this->fooRepository->findByUid(1);
$myObjectService->doSometing($mySecondObject);

-----------

class myObjectService {
	public function doSomething(Tx_MyExt_Doamin_Model_Foo $object) {
		if ($myObject->getProducts()->count() > 10) {
			...
		}
	}
}
-------------------------------

But note, that you must not put any domain logic inside of the 
constructor, as it will only be called ONCE in a lifetime of a object 
(lifetime = from initial creation to deletion in DB). So it won't be 
called again once the object is stored in the repository and fetched any 
time later!

-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list