kskaradzinski
kskaradzinski

Reputation: 5084

Getting multiple rows using ParamConverter

Hi Im trying to use ParamConverter to get multiple rows from DB but profiler show query with limi 1. Is it possible to get it like that

/**
* @Route("/localization/{code}", name="pkt-index")
* @ParamConverter("localizations", class="PriceBundle:Localization")
*/

after entering localization/0003 I should get more than 100 rows.

EDIT: I have used repository_method option and

/*
* @Route("/localization/{id}", name="pkt-index")
* @ParamConverter("localizations", class="PriceBundle:Localization", options={
* "repository_method": "getByLocalizationCode"
* })
*/

but funny thing is that when I change {id} in route it does not work it throws and exception SQLSTATE[HY093]: Invalid parameter number: parameter was not defined even if variable exists in entity class, if variable dont exist it throws Unable to guess how to get a Doctrine instance from the request information.

EXPLANATION

when I change {id} in route it does not work it throws and exception SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

Here I think symfony treads id like primary key and as parameter to repository method it pass string when I changed this id to something else it pass array

Example

/**
* @Route("/localization/{id}", name="pkt-index")
*/

pass string to method

/**
* @Route("/localization/{code}/{name}", name="pkt-index")
*/

pass array to method

array(
    'code' => 003
    'name' => localization_name
)

and last

/**
* @Route("/localization/{id}/{name}", name="pkt-index")
*/

will pass string id omit the name

Hope this sounds reasonable.

Upvotes: 0

Views: 2178

Answers (2)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52513

forgottenbas's answer isn't completely right. @ParamConverter will first try to find one entity by id ...

... then try to match the route variables against db columns to find an entity ... but essentially it will only convert one entity at a time.

If you would still like to use a paramconverter you would need to write a custom one.

or just use a one-liner inside your controller action:

/**
 * @Route("/localization/{code}", name="pkt-index")
 */
public function yourAction($code)
{
     $localizations = $this->getDoctrine()->getRepository("YourBundle:Localization")->findBy(array("code" => $code));
     // ...

Upvotes: 3

Alexey B.
Alexey B.

Reputation: 12033

ParamConverter currently can only extract id from request and find one entity from db. Look at DoctrineParamConverter code. But you can specify your own param converter with some extra logic.

Upvotes: 1

Related Questions