Giovanni Le Grand Giovanni Le Grand - 3 years ago 118
PHP Question

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

The situation



I am currently making an CMS in Symfony 3, I have taught myself to work with Symfony, and I have ran into an problem in which I cannot seem to find the problem.




The Environment



The complete environment of the application is as follows:

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





The Problem



When I run
MediaBundle:DefaultController:createAction
I get the following error:


CoreBundle\Entity\Album object not found.





The Code



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;
}
}





Tests I've Ran




  1. Check if the
    Album
    file exists.


    Album
    file exists in the location:
    CoreBundle\Entity\Album.php
    .

  2. Check if
    Album
    file works with other functions in the same controller.


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

  3. Does removing the cache work.


    successfully removed the cache, did not change a thing.

  4. Is the Annotation properly made.


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






As you can see I've tried a bit already but nothing worked for me.

Answer Source

(Posted on behalf of the OP).

Fixed it, was the routing that was in an incorrect order.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download