-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dont wrap in paginator #111
Conversation
Controller helpers to the rescue? |
Good idea for the controller plugin! I'll do that later :). I've also updated the doc. |
Done @Ocramius ! I've added a paragraph on cookbook to show usage. Looks good to you? |
*/ | ||
public function __invoke(Traversable $data, $criteria = []) | ||
{ | ||
$paginatorAdapter = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ($data instanceof Selectable) {
return new SelectableAdapter($data, $this->createCriteria($criteria));
}
if ($data instanceof Collection) {
return new CollectionAdapter($data);
}
// throw exception
* @return Paginator | ||
* @throws RuntimeException | ||
*/ | ||
public function __invoke(Traversable $data, $criteria = []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may accept $criteria = null
and call createCriteria()
only if it is an array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No because it should work with Criteria also. Let's keep it simple :).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is null
or Criteria
you can pass directly to SelectableAdapter
without calling createCriteria()
@@ -65,7 +65,13 @@ public function createResourceModel(MvcEvent $event) | |||
return; | |||
} | |||
|
|||
$model = new ResourceModel($resource); | |||
// Because we may manipulate the resource data in the controller (for instance wrapping a collection around | |||
// a paginator), we need to create a new resource from the data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not create a method setData()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I had this discussion with @Ocramius and he told me we should not be able to change the resource.
Furthermore you may want to keep the matched resource untouched.
Envoyé de mon iPhone
Le 24 déc. 2013 à 12:11, Daniel Gimenes notifications@github.com a écrit :
In src/ZfrRest/Mvc/CreateResourceModelListener.php:
@@ -65,7 +65,13 @@ public function createResourceModel(MvcEvent $event)
return;
}
$model = new ResourceModel($resource);
// Because we may manipulate the resource data in the controller (for instance wrapping a collection around
Why not create a method setData()?// a paginator), we need to create a new resource from the data
—
Reply to this email directly or view it on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danizord yep. Don't change the resource :-)
* @return Paginator | ||
* @throws RuntimeException | ||
*/ | ||
public function __invoke($data, $criteria = []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If $criteria
is a Criteria
or null
you may pass it directly to the adapter without calling createCriteria()
public function __invoke($data, $criteria = null)
{
if ($data instanceof Selectable) {
if (is_array($criteria)) {
$criteria = $this->createCriteria($criteria);
}
// Otherwise, pass $criteria directly to SelectableAdapter
return new Paginator(new SelectableAdapter($data, $criteria);
}
//...
👍 |
ping @danizord @Ocramius
This PR removes the automatic conversion to Paginator in the router. Instead, the controller now receives exactly what has been matched by the router, so it's up to the user to specify the appropriate type hint (Selectable will be the most useful one I think). Query params filtering can be done directly in the action controller now, this is much more convenient.
Also, the CreateResourceModelListener correctly take into account the return value from the controller. So if you receive a collection, and manually build a paginator from it, the listener will detect the data is not the same (initially a collection, then a paginator), and will therefore create a new resource (reusing the same metadata).
I thing: should we include a utility method in AbstractRestfulController to create a paginator from a collection or is out from our responsibility?