2012-05-09 00:38:38 +02:00
|
|
|
The PropelParamConverter
|
|
|
|
========================
|
|
|
|
|
2012-07-25 22:48:40 +02:00
|
|
|
You can use the `PropelParamConverter` with the [SensioFrameworkExtraBundle](http://github.com/sensio/SensioFrameworkExtraBundle).
|
2012-05-09 00:38:38 +02:00
|
|
|
You just need to put the right _Annotation_ on top of your controller:
|
|
|
|
|
|
|
|
``` php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ParamConverter("post", class="BlogBundle\Model\Post")
|
|
|
|
*/
|
|
|
|
public function myAction(Post $post)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Your request needs to have an `id` parameter or any field as parameter (slug, title, ...).
|
|
|
|
|
|
|
|
The _Annotation_ is optional if your parameter is typed you could only have this:
|
|
|
|
|
|
|
|
``` php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
public function myAction(Post $post)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2013-09-23 22:46:59 +02:00
|
|
|
**New** with last version of `SensioFrameworkExtraBundle`,
|
|
|
|
you can ommit the `class` parameter if your controller parameter is typed,
|
2012-07-25 22:48:40 +02:00
|
|
|
this is usefull when you need to set extra `options`.
|
|
|
|
|
|
|
|
``` php
|
|
|
|
<?php
|
|
|
|
use BlogBundle\Model\Post;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ParamConverter("post")
|
|
|
|
*/
|
|
|
|
public function myAction(Post $post)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2012-05-09 00:38:38 +02:00
|
|
|
#### Exclude some parameters ####
|
|
|
|
|
|
|
|
You can exclude some attributes from being used by the converter:
|
|
|
|
|
|
|
|
If you have a route like `/my-route/{slug}/{name}/edit/{id}`
|
|
|
|
you can exclude `name` and `slug` by setting the option "exclude":
|
|
|
|
|
|
|
|
``` php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ParamConverter("post", class="BlogBundle\Model\Post", options={"exclude"={"name", "slug"}})
|
|
|
|
*/
|
|
|
|
public function myAction(Post $post)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2012-05-23 00:08:07 +02:00
|
|
|
#### Custom mapping ####
|
|
|
|
|
|
|
|
You can map route parameters directly to model column to be use for filtering.
|
|
|
|
|
|
|
|
If you have a route like `/my-route/{postUniqueName}/{AuthorId}`
|
|
|
|
Mapping option overwrite any other automatic mapping.
|
|
|
|
|
|
|
|
``` php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ParamConverter("post", class="BlogBundle\Model\Post", options={"mapping"={"postUniqueName":"name"}})
|
|
|
|
* @ParamConverter("author", class="BlogBundle\Model\Author", options={"mapping"={"AuthorId":"id"}})
|
|
|
|
*/
|
|
|
|
public function myAction(Post $post, $author)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
```
|
2012-05-09 00:38:38 +02:00
|
|
|
|
2012-05-24 00:03:56 +02:00
|
|
|
#### Hydrate related object ####
|
|
|
|
|
|
|
|
You could hydrate related object with the "with" option:
|
|
|
|
|
|
|
|
``` php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ParamConverter("post", class="BlogBundle\Model\Post", options={"with"={"Comments"}})
|
|
|
|
*/
|
|
|
|
public function myAction(Post $post)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
You can set multiple with ```"with"={"Comments", "Author", "RelatedPosts"}```.
|
|
|
|
|
|
|
|
The default join is an "inner join" but you can configure it to be a left join, right join or inner join :
|
|
|
|
|
|
|
|
``` php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ParamConverter("post", class="BlogBundle\Model\Post", options={"with"={ {"Comments", "left join" } }})
|
|
|
|
*/
|
|
|
|
public function myAction(Post $post)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
```
|
2013-09-23 22:46:59 +02:00
|
|
|
Accepted parameters for join :
|
2012-05-24 00:03:56 +02:00
|
|
|
|
2013-09-23 22:46:59 +02:00
|
|
|
* left, LEFT, left join, LEFT JOIN, left_join, LEFT_JOIN
|
2012-05-24 17:03:46 +02:00
|
|
|
* right, RIGHT, right join, RIGHT JOIN, right_join, RIGHT_JOIN
|
|
|
|
* inner, INNER, inner join, INNER JOIN, inner_join, INNER_JOIN
|
2012-05-24 00:03:56 +02:00
|
|
|
|
2013-09-23 22:46:59 +02:00
|
|
|
#### Named converter ####
|
|
|
|
|
|
|
|
If you have a conflict with another ParamConverter you can force the `PropelParamConverter` with the `converter` option.
|
|
|
|
|
|
|
|
``` php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ParamConverter("post", converter="propel")
|
|
|
|
*/
|
|
|
|
public function myAction(Post $post)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2012-05-09 00:38:38 +02:00
|
|
|
[Back to index](index.markdown)
|