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

Entity object not found upon requesting a page in Symfony3.1

情况

我目前正在 Symfony 3 中制作 CMS,我自学了如何使用 Symfony,但我 运行 遇到了一个我似乎找不到问题的问题。


环境

应用完整环境如下:

来源:
   - AuthBundle
      - 控制器
         - DefaultController.php
         - SecurityController.php
      - 资源
         - 配置
            - services.yml
         - 观点
            - 默认
               - index.html.twig
            - 安全
               - login.html.twig
      - AuthBundle.php
   - 核心包
      - 控制器
         - DefaultController.php
      - 实体
         - 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
      - 形式
         - AlbumType.php
         - EventType.php
         - ImageType.php
         - MailLijstType.php
         - PostType.php
         - UserType.php
      - 资料库
         - MaiLIjstRepository.php
      - 资源
         - 配置
            - routing.yml
            - services.yml
         - 意见
            - 默认
               - index.html.twig
      - CoreBundle.php
   - 事件包
      - 控制器
         - DefaultController.php
      - 资源
         - 配置
            - services.yml
         - 观点
            - 行政
               - event.html.twig
               - events.html.twig
               - index.html.twig
            - 默认
               - event.html.twig
               - events.html.twig
               - index.html.twig
      - EventBundle.php
   - 邮件包
      - 控制器
         - DefaultController.php
         - nieuwsbrief.php
      - 资源
         - 配置
            - services.yml
         - 意见
            - 行政
               - lijst.html.twig
               - maken.html.twig
            - 默认
               - index.html.twig
            - 电子邮件
               - nieuwsbrief.html.twig
               - registration.html.twig
      - MailBundle.php
   - 主包
      - 控制器
         - DefaultController.php
      - 资源
         - 配置
            - routing.yml
            - services.yml
         - 意见
            - 行政
               - dashboard.html.twig
            - 默认
               - index.html.twig
      -T
      - 双胞胎
      - 树枝
         - Extentions.php
      - MainBundle.php
   - 媒体包
      - 控制器
         - DefaultController.php
      - 资源
         - 配置
            - services.yml
         - 意见
            - 行政
               - album.html.twig
               - albums.html.twig
               - create.html.twig
               - uploader.html.twig
            - 默认
               - album.html.twig
               - index.html.twig
      - MediaBundle.php
   - PostBundle
      - 控制器
         - DefaultController.php
      - 资源
         - 配置
            - services.yml
         - 观点
            - 行政
               - new.html.twig
               - post.html.twig
               - posts.html.twig
            - 默认
               - archief.html.twig
               - index.html.twig
      - PostBundle.php
   - 用户包
      - 控制器
         - DefaultController.php
      - 资源
         - 配置
            - services.yml
         - 观点
            - 行政
               - bekijk.html.twig
               - edit.html.twig
               - new.html.twig
               - users.html.twig
            - 默认
               - index.html.twig
      - UserBundle.php

问题

当我 运行 MediaBundle:DefaultController:createAction 我得到以下错误:

CoreBundle\Entity\Album object not found.


代码

MediaBundle\Controller\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\Entity\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 file exists in the location: CoreBundle\Entity\Album.php.

  2. 检查 Album 文件是否与同一控制器中的其他函数一起工作。

    Album file is working/found corrently with other functions of the same controller.

  3. 删除缓存是否有效。

    successfully removed the cache, did not change a thing.

  4. 标注是否正确。

    as far as I can see, the annotation is made properly.


如您所见,我已经尝试了一些,但没有任何效果。

(代表OP发表).

已修复,路由顺序不正确。