0

リンクをクリックすると、所有しているMyAlbumControllerにオブジェクトIDを渡します。

<?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') );
    }
}

そしてそれは動作します。

しかし、stackoverflowで何かが見られます。そこでは、再現したいオブジェクト(S2がそれ自体を見つけることができると聞きました)を渡します。

<?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') );
    }
}

しかし、それは機能しません、私は持っています:

クラスYM\TestBundle \ Controller\Albumが存在しません

なんで ?どうも

4

1 に答える 1

4

まず、useステートメントを忘れました:

use  YM\TestBundle\Entity\Album;

あなたの場合、symfonyは現在の名前空間(Controller)でAlbumクラスを探します。

あなたがParamConverter(あなたが尋ねるメカニズム)についてもっと読みたいと思うかもしれないMoveover:

http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html

そのようなものをメソッドアノテーションに追加してみてください(そして、このアノテーションの適切な名前空間を忘れないでください):

use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

(...)

/**
 * @ParamConverter("album", class="YMTestBundle:Album")
 */
public function deleteAction(Album $album)
{
   (...)

于 2013-02-06T23:02:20.130 に答える