You can use the PropelParamConverter
with the SensioFrameworkExtraBundle.
You just need to put the right Annotation on top of your controller:
<?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
public function myAction(Post $post)
{
}
New with last version of SensioFrameworkExtraBundle
,
you can ommit the class
parameter if your controller parameter is typed,
this is usefull when you need to set extra options
.
<?php
use BlogBundle\Model\Post;
/**
* @ParamConverter("post")
*/
public function myAction(Post $post)
{
}
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
/**
* @ParamConverter("post", class="BlogBundle\Model\Post", options={"exclude"={"name", "slug"}})
*/
public function myAction(Post $post)
{
}
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
/**
* @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)
{
}
You could hydrate related object with the "with" option:
<?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
/**
* @ParamConverter("post", class="BlogBundle\Model\Post", options={"with"={ {"Comments", "left join" } }})
*/
public function myAction(Post $post)
{
}
Accepted parameters for join :
- left, LEFT, left join, LEFT JOIN, left_join, LEFT_JOIN
- right, RIGHT, right join, RIGHT JOIN, right_join, RIGHT_JOIN
- inner, INNER, inner join, INNER JOIN, inner_join, INNER_JOIN
If you have a conflict with another ParamConverter you can force the PropelParamConverter
with the converter
option.
<?php
/**
* @ParamConverter("post", converter="propel")
*/
public function myAction(Post $post)
{
}