Reputation: 4169
When clicking on a link, I pass an object id in My AlbumController I have :
<?php
namespace YM\TestBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class AlbumController extends Controller
{
public function deleteAction($id)
{
if ($id > 0) {
// We get the EntityManager
$em = $this->getDoctrine()
->getEntityManager();
// We get the Entity that matches id: $id
$article = $em->getRepository('YMTestBundle:Album')
->find($id);
// If the Album doesn't exist, we throw out a 404 error
if ($article == null) {
throw $this->createNotFoundException('Album[id='.$id.'] do not exist');
}
// We delete the article
$em->remove($article);
$em->flush();
$this->get('session')->getFlashBag()->add('info', 'Album deleted successfully');
// Puis on redirige vers l'accueil
return $this->redirect( $this->generateUrl('ymtest_Artist') );
}
return $this->redirect( $this->generateUrl('ymtest_dashboard') );
}
}
And it works.
But is saw something on stackoverflow, where they pass an object (I heard S2 is able to find it itself) that I'd like to reproduce:
<?php
namespace YM\TestBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class AlbumController extends Controller
{
public function deleteAction(Album $album)
{
// We get the EntityManager
$em = $this->getDoctrine()
->getEntityManager();
// If the Album doesn't exist, we throw out a 404 error
if ($lbum == null) {
throw $this->createNotFoundException('Album[id='.$id.'] do not exist');
}
// We delete the article
$em->remove($album);
$em->flush();
$this->get('session')->getFlashBag()->add('info', 'Album deleted successfully');
// Puis on redirige vers l'accueil
return $this->redirect( $this->generateUrl('ymtest_Artist') );
return $this->redirect( $this->generateUrl('ymtest_dashboard') );
}
}
But it doesn't work, I have :
Class YM\TestBundle\Controller\Album does not exist
Why ? Thx
Upvotes: 0
Views: 2091
Reputation: 11374
First you forgot about use statement:
use YM\TestBundle\Entity\Album;
In your case symfony looks for Album class in current namespace (which is Controller).
Moveover you may want read more about ParamConverter (the mechanism which you ask about):
http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
Try add something like that to your method annotation (And don't forgot about appropriate namespace for this annotation):
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
(...)
/**
* @ParamConverter("album", class="YMTestBundle:Album")
*/
public function deleteAction(Album $album)
{
(...)
:
Upvotes: 4