-
-
Save ornicar/592626 to your computer and use it in GitHub Desktop.
| <?php | |
| class Article extends Content | |
| { | |
| protected $comments; // a Collection implementation, ArrayCollection or PersistentCollection | |
| // get the ArrayCollection or PersistentCollection | |
| public function getComments() | |
| { | |
| return $this->comments; | |
| } | |
| // get the CommentCollection to access custom methods. | |
| public function getCommentCollection() | |
| { | |
| return new CommentCollection($this->getComments()); | |
| } | |
| } | |
| class CommentCollection | |
| { | |
| protected $collection; | |
| public function __construct(Collection $collection) | |
| { | |
| $this->collection = $collection; | |
| } | |
| public function getLastComments($limit = null) | |
| { | |
| $lastComments= array_reverse($this->collection->toArray()); | |
| if($limit) { | |
| return array_slice($lastComments,0,$limit); | |
| } | |
| return $lastComments; | |
| } | |
| } |
That's we I did before. It leads to a lot of issues.
J. Wage recommended this approach to me: not modify the way Doctrine works, wrap when necessary.
I see would you mind giving an example of what was going wrong?
@onload is not enough, you have to use @OnUpdate too. If you don't, after the document gets updated, your custom collection is gone. Even with both @onload and @OnUpdate I found cases were the collection was not the expected one.
And if we use such lifecycle events to replace the collection with our custom one, we should make it implement the Collection interface. It makes a lot of methods.
We can not just extend ArrayCollection or PersistentCollection because depending on the collection state, it must be one or the other.
Do I explain it well enough?
Yes, this is very good, I havent though about it. Thank you for the explanation!
ok, why can't you do:
public function getComments()
{
return new CommentCollection($this->comments);
}
to get rid of getCommentCollection
that way your comments will always behave the same way.
Right. And ideally it should implement the Collection interface.
Maybe something like that:
class CommentCollection extends CollectionWrapper
{
public function doFancyStuff()
{
// Do things with $this->collection
}
}
class CollectionWrapper implements Collection
{
protected $collection;
public function __construct(Collection $collection)
{
$this->collection = $collection;
}
/* Implement here all Collection methods, forwarding logic to $this->collection */
public function add($element)
{
return $this->collection->add($element);
}
/* And more... */
}
Wouldn't it be easier if comments were initialized as commentscollection? You could wrap persistent collection in onload event