I am looking for a way to return an immutable collection from a domain object in Doctrine 2. Let's start with this example from doc :
class User {
From the DDD point of view, if User is the aggregate root, then we would prefer:
$user = new User(); $user->addGroup($group);
But still, if we also need the getGroups() method, then ideally we do not want to return an internal reference to the collection, as this may allow someone to bypass the addGroup() method.
Is there a built-in way to return an immutable collection instead of creating a custom immutable immutable proxy? Such as...
public function getGroups() { return new ImmutableCollection($this->groups); }
source share