Skip to content

Instantly share code, notes, and snippets.

@ornicar
Created September 22, 2010 21:32
Show Gist options
  • Select an option

  • Save ornicar/592626 to your computer and use it in GitHub Desktop.

Select an option

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;
}
}
@avalanche123
Copy link
Copy Markdown

Wouldn't it be easier if comments were initialized as commentscollection? You could wrap persistent collection in onload event

@ornicar
Copy link
Copy Markdown
Author

ornicar commented Sep 23, 2010

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.

@avalanche123
Copy link
Copy Markdown

I see would you mind giving an example of what was going wrong?

@ornicar
Copy link
Copy Markdown
Author

ornicar commented Sep 23, 2010

@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?

@avalanche123
Copy link
Copy Markdown

Yes, this is very good, I havent though about it. Thank you for the explanation!

@avalanche123
Copy link
Copy Markdown

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.

@ornicar
Copy link
Copy Markdown
Author

ornicar commented Sep 24, 2010

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... */
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment