在Symfony3.1中请求页面时找不到实体对象

时间:2016-11-14 15:51:44

标签: php annotations symfony-forms symfony-3.1

情况

我目前正在Symfony 3中创建一个CMS,我已经自学了Symfony,我遇到了一个我似乎无法找到问题的问题。

环境

该应用程序的完整环境如下:

src:
   - AuthBundle
      - Controller
         - DefaultController.php
         - SecurityController.php
      - Resources
         - config
            - services.yml
         - views
            - Default
               - index.html.twig
            - Security
               - login.html.twig
      - AuthBundle.php
   - CoreBundle
      - Controller
         - DefaultController.php
      - Entity
         - Album.php
         - Bargroep.php
         - BargroepenRepository.php
         - Barpersoneel.php
         - BarpersoneelRepository.php
         - Carousel.php
         - Data.php
         - EvPkmn.php
         - Event.php
         - Image.php
         - Inschrijvingen.php
         - Logs.php
         - Maillijst.php
         - Post.php
         - User.php
         - UserRepository.php
         - Variabel.php
         - Vote.php
      - Form
         - AlbumType.php
         - EventType.php
         - ImageType.php
         - MailLijstType.php
         - PostType.php
         - UserType.php
      - Repository
         - MaiLIjstRepository.php
      - Resources
         - config
            - routing.yml
            - services.yml
         - views
            - Default
               - index.html.twig
      - CoreBundle.php
   - EventBundle
      - Controller
         - DefaultController.php
      - Resources
         - config
            - services.yml
         - views
            - Admin
               - event.html.twig
               - events.html.twig
               - index.html.twig
            - Default
               - event.html.twig
               - events.html.twig
               - index.html.twig
      - EventBundle.php
   - MailBundle
      - Controller
         - DefaultController.php
         - nieuwsbrief.php
      - Resources
         - config
            - services.yml
         - views
            - Admin
               - lijst.html.twig
               - maken.html.twig
            - Default
               - index.html.twig
            - Emails
               - nieuwsbrief.html.twig
               - registration.html.twig
      - MailBundle.php
   - MainBundle
      - Controller
         - DefaultController.php
      - Resources
         - config
            - routing.yml
            - services.yml
         - views
            - Admin
               - dashboard.html.twig
            - Default
               - index.html.twig
      - T
      - Twi
      - Twig
         - Extentions.php
      - MainBundle.php
   - MediaBundle
      - Controller
         - DefaultController.php
      - Resources
         - config
            - services.yml
         - views
            - Admin
               - album.html.twig
               - albums.html.twig
               - create.html.twig
               - uploader.html.twig
            - Default
               - album.html.twig
               - index.html.twig
      - MediaBundle.php
   - PostBundle
      - Controller
         - DefaultController.php
      - Resources
         - config
            - services.yml
         - views
            - Admin
               - new.html.twig
               - post.html.twig
               - posts.html.twig
            - Default
               - archief.html.twig
               - index.html.twig
      - PostBundle.php
   - UserBundle
      - Controller
         - DefaultController.php
      - Resources
         - config
            - services.yml
         - views
            - Admin
               - bekijk.html.twig
               - edit.html.twig
               - new.html.twig
               - users.html.twig
            - Default
               - index.html.twig
      - UserBundle.php

问题

当我运行MediaBundle:DefaultController:createAction时,我收到以下错误:

  

未找到CoreBundle \ Entity \ Album对象。

代码

MediaBundle \控制器\ Defaultcontroller.php

<?php

namespace MediaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use ImageOptim\API;
use CoreBundle\Entity\Album;
use CoreBundle\Entity\Image;
use CoreBundle\Form\AlbumType;
use CoreBundle\Form\ImageType;

class DefaultController extends Controller {

    /**
     * @Route("/album/{naam}", name="home_album")
     */
    public function home(Album $album){
        $em = $this->getDoctrine()->getManager();

        $fotos = $this->getDoctrine()->getRepository("CoreBundle:Image")->findBy(Array('albumId' => $album->getId()));
        $counter = $this->getDoctrine()->getRepository('CoreBundle:Variabel')->findOneBy(Array('type' => 'counter'));
        $counter->setWaarde($counter->getWaarde() + 1);
        $em->persist($counter);
        $em->flush();
        return $this->render("MediaBundle:Default:album.html.twig", Array(
                    'album' => $album,
                    'media' => $fotos,
                    'counter' => $counter,
        ));
    }
    /**
     * @Route("/albums", name="albums")
     * @Security("has_role('ROLE_MEDEWERKER')")
     */
    public function indexAction() {
        $albums = $this->getDoctrine()->getRepository('CoreBundle:Album')->findAll();
        return $this->render('MediaBundle:Admin:albums.html.twig', Array('page' => 'media', 'albums' => $albums));
    }

    /**
     * @Route("/album/maken", name="albumMaken")
     * @Security("has_role('ROLE_CONTENT')")
     */
    public function createAction(Request $request) {
        $album = new Album();
        $form = $this->createForm(AlbumType::class, $album);

        $form->handleRequest($request);

        if ($form->isValid() && $form->isSubmitted()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($album);
            $em->flush();

            $fs = new Filesystem();
            if (!$fs->exists('/var/www/vhosts/saturnuslivemusic.nl/httpdocs/web/assets/images/albums/' . $album->getNaam())) {
                $fs->mkdir('/var/www/vhosts/saturnuslivemusic.nl/httpdocs/web/assets/images/albums/' . $album->getNaam());
            }
            return $this->redirectToRoute('bekijkAlbum', Array('titel' => $album->getNaam(), 'id' => $album->getId()));
        }
        return $this->render('MediaBundle:Admin:create.html.twig', Array(
                    'page' => 'media',
                    'form' => $form->createView(),
        ));
    }

    /**
     * @Route("/album/bewerk/{titel}/{id}", name="bewerkAlbum")
     * @Security("has_role('ROLE_CONTENT')")
     */
    public function bewerkAction($titel, $id) {
        // Laden van de template met dropzone
        $image = new Image();
        $form = $this->createForm(ImageType::class, $image);

        $album = $this->getDoctrine()->getRepository("CoreBundle:Album")->findOneBy(Array("id"=>$id));

        return $this->render('MediaBundle:Admin:uploader.html.twig', Array(
            'page'=>'media',
            'album'=>$album,
            'form'=>$form->createView(),
        ));
    }

    /**
     * @Route("/upload/image/{id}", name="uploadImage")
     */
    public function uploadAction($id, Request $request) {
        // uploaden van de fotos vanuit de dropzone
        $files = $request->files;

        $album = $this->getDoctrine()->getRepository('CoreBundle:Album')->findOneBy(Array('id' => $id));

        foreach ($files as $upfile) {
            $name = str_replace(' ', '_', $upfile->getClientOriginalName());
            $file = $upfile->move('/var/www/vhosts/saturnuslivemusic.nl/httpdocs/web/assets/images/albums/' . $album->getNaam() . '/', $name);

            $upload = new Image();
            $upload->setAlbumId($album);
            $upload->setName(str_replace('_', ' ', $name));
            $upload->setPath($name);

            $em = $this->getDoctrine()->getManager();
            $em->persist($upload);
            $em->flush();


            $ap = new API("hwmwskhnhb");
            $img = $ap->imageFromPath('/var/www/vhosts/saturnuslivemusic.nl/httpdocs/web/assets/images/albums/' . $album->getNaam() . '/' . $upload->getPath())
                    ->quality("low")
                    ->getBytes();

            $img = imagecreatefromstring($img);
            unlink('/var/www/vhosts/saturnuslivemusic.nl/httpdocs/web/assets/images/albums/' . $album->getNaam() . '/' . $upload->getPath());

            imagejpeg($img, '/var/www/vhosts/saturnuslivemusic.nl/httpdocs/web/assets/images/albums/' . $album->getNaam() . '/' . $upload->getPath());

            $response = new Response();
            $response->setContent(json_encode(Array(
                'statusCode' => 200,
            )));
            $response->headers->set('Content-Type', 'application/json');

            return $response;
        }
    }

    /**
     * @Route("/album/verwijder/{id}", name="verwijderAlbum")
     * @Security("has_role('ROLE_CONTENT')")
     */
    public function verwijderAction($id) {

    }

    /**
     * @Route("/album/bekijk/{titel}/{id}", name="bekijkAlbum")
     * @Security("has_role('ROLE_MEDEWERKER')")
     */
    public function bekijkAction($titel, $id) {
        $album = $this->getDoctrine()->getRepository("CoreBundle:Album")->findOneBy(Array('id' => $id));
        $fotos = $this->getDoctrine()->getRepository("CoreBundle:Image")->findBy(Array('albumId' => $album->getId()));

        return $this->render("MediaBundle:Admin:album.html.twig", Array(
                    'album' => $album,
                    'media' => $fotos,
                    'page' => 'media',
        ));
    }

}

CoreBundle \实体\ Album.php

<?php

namespace CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 * @ORM\Table(name="foto_albums")
 */
class Album
{
    /**
     * @ORM\Column(type="string", length=255)
     */
    private $naam;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * Set naam
     *
     * @param string $naam
     *
     * @return FotoAlbums
     */
    public function setNaam($naam)
    {
        $this->naam = $naam;

        return $this;
    }

    /**
     * Get naam
     *
     * @return string
     */
    public function getNaam()
    {
        return $this->naam;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
}

测试我已经

  1. 检查Album文件是否存在。

      该位置存在

    Album文件:CoreBundle\Entity\Album.php

  2. 检查Album文件是否与同一控制器中的其他功能一起使用。

      

    Album文件与同一控制器的其他功能正确地工作/找到。

  3. 删除缓存是否有效。

      

    成功删除了缓存,没有改变任何东西。

  4. 是否正确制作了注释。

      

    据我所见,注释正确。

  5. 正如你所看到的,我已经尝试了一下但是没有任何对我有用。

1 个答案:

答案 0 :(得分:3)

(代表OP发布)

修正了,路由的顺序不正确。