3

While trying to assign an object to a view in my controller action I get the following message because this object is not persisted:

Could not serialize Domain Object Vendor\Extension\Domain\Model\Object. It is neither an Entity with identity properties set, nor a Value Object.

Is there any possibility to add this object to the view without creating a databaseentry?

Jonas
  • 349
  • 3
  • 21

1 Answers1

5

The exception [InvalidArgumentValueException('Could not serialize Domain Object $className. It is neither an Entity with identity properties set, nor a Value Object.', 1260881688)][1] is thrown in the UriBuilder, thus when a model shall be used as argument for creating a link.

The instance of Vendor\Extension\Domain\Model\Object must either fulfill these requirements:

  • can be represented as array (is array or implements Iterator interface) OR
  • extends TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject AND one of
    • extends TYPO3\CMS\Extbase\DomainObject\AbstractValueObject OR
    • having a valid uid, not null

Thus, if you instantiated the object directly in the controller, the uid property is not defined yet. This property is assigned if domain objects are fetched or added with a repository.

TypeConverters

TypeConverters allow to convert from a given identifier (some string representation, hash-value, ...) to a proper domain object. The following links show how to do that for the concept of an IBAN (International Bank Account Identifier).

TypeConverters have to registerd in ext_localconf.php like this:

\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerTypeConverter(
    \H4ck3r31\BankAccountExample\Domain\Property\TypeConverter\IbanTypeConverter::class
);

The Iban object can be used then in your controller:

public function someAction(Iban $iban) { ... }

Use array representation of your object

Another alternative could be to assign an array representation of the domain object to the view and use that to fill the link arguments:

$this->view->assign('myObject', $object->toArray());

When invoking a controller action, the object is reconstituted from the submitted array keys and are used as properties - thus array keys and properties must have the same naming, or a persistence column mapping is defined.

public function someAction(MyObject $object) { ... }

In my previously mentioned bank account example it looks like this:

The term "Dto" is the abbreviation for "Data Transfer Object", thus it's not a real domain entity, does not have a proper UID and is just used to encapsulate information in a domain object when passing that to different components.

Oliver Hader
  • 4,093
  • 1
  • 25
  • 47
  • All in all it can be said that there is no visualisation without persisting? – Jonas Nov 22 '16 at 14:28
  • Better: Linking to entities without having a valid UID is not possible. The UID is assigned when objects get persisted and written to a database table. I'll update the answer to use type-converters or to use arrays directly. – Oliver Hader Nov 23 '16 at 09:29