OneupUploaderBundle relation between 2 entities - php

I'm new on stackoverflow and I hope you can help me.
I use OneupUploaderBundle on symfony2 and it's working I can upload multiple images and I can persist them in my database.
But now I would like to upload multiple images for one 'Annonce' for exemple so, I have 2 entities
Annonce
namespace TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Annonce
*
* #ORM\Table(name="annonce")
* #ORM\Entity(repositoryClass="TestBundle\Repository\AnnonceRepository")
*/
class Annonce
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="titre", type="string", length=255)
*/
private $titre;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titre
*
* #param string $titre
* #return Annonce
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
}
And my Entity Image
namespace TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Image
*
* #ORM\Table(name="image")
* #ORM\Entity(repositoryClass="TestBundle\Repository\ImageRepository")
*/
class Image
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Image
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* #ORM\ManyToOne(targetEntity="TestBundle\Entity\Annonce")
* #ORM\JoinColumn(nullable=false)
*/
private $annonce;
/**
* Set annonce
*
* #param \TestBundle\Entity\Annonce $annonce
* #return Image
*/
public function setAnnonce(\TestBundle\Entity\Annonce $annonce)
{
$this->annonce = $annonce;
return $this;
}
/**
* Get annonce
*
* #return \TestBundle\Entity\Annonce
*/
public function getAnnonce()
{
return $this->annonce;
}
}
My EventListener
namespace TestBundle\EventListener;
use Doctrine\Common\Persistence\ObjectManager;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use TestBundle\Entity\Annonce;
use TestBundle\Entity\Image;
class UploadListener
{
/**
* #var ObjectManager
*/
private $em;
public function __construct(ObjectManager $em)
{
$this->em = $em;
}
public function onUpload(PostPersistEvent $event)
{
$file = $event->getFile();
$annonce = new Annonce();
$image = new Image();
/* TEST */
$annonce->setTitre("I'am a title");
$this->em->persist($annonce);
$image->setName($file->getFilename());
$image->setAnnonce($annonce);
$this->em->persist($image);
$this->em->flush();
$response = $event->getResponse();
$response['files'] = [
'name' => $file->getFilename(),
'size' => $file->getSize()
];
}
The problem is, when I upload many images, I have the same number of row in Annonce as in Image.
Someone can help me please :)

Related

WebfactoryPolyglotBundle use causes error "No mapping found for field 'id' on class 'AppBundle\Entity\Film'."

I use the Symfony version 3.4.0 and I try to use this bundle to translate entities : https://github.com/webfactory/WebfactoryPolyglotBundle
So I created two entities ( Film and FilmTranslation ) for the test
Here my Film.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Annotation as Polyglot;
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Film
*
* #ORM\Table(name="film")
* #ORM\Entity(repositoryClass="AppBundle\Repository\FilmRepository")
* #Polyglot\Locale(primary="fr_FR")
*/
class Film
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #Polyglot\TranslationCollection
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="Titre", type="string", length=255, unique=true)
* #Polyglot\Translatable
* #var string|TranslatableInterface
*/
private $titre;
/**
* #ORM\OneToMany(targetEntity="FilmTranslation", mappedBy="entity")
* #Polyglot\TranslationCollection
* #var Collection
*/
private $translations;
public function __construct()
{
$this->translations = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titre
*
* #param string $titre
*
* #return Film
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Add translation
*
* #param \AppBundle\Entity\FilmTranslation $translation
*
* #return Film
*/
public function addTranslation(\AppBundle\Entity\FilmTranslation $translation)
{
$this->translations[] = $translation;
return $this;
}
/**
* Remove translation
*
* #param \AppBundle\Entity\FilmTranslation $translation
*/
public function removeTranslation(\AppBundle\Entity\FilmTranslation $translation)
{
$this->translations->removeElement($translation);
}
/**
* Get translations
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTranslations()
{
return $this->translations;
}
}
And here my FilmTranslation.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Entity\BaseTranslation;
/**
* FilmTranslation
*
* #ORM\Table(name="film_translation")
* #ORM\Entity(repositoryClass="AppBundle\Repository\FilmTranslationRepository")
* #ORM\Table(
* uniqueConstraints = {
* #ORM\UniqueConstraint(columns={"entity_id", "locale"})
* }
* )
*/
class FilmTranslation extends BaseTranslation
{
/**
* #var string
*
* #ORM\Column(name="titre", type="string", length=255, unique=true)
*/
private $titre;
/**
* #ORM\ManyToOne(targetEntity="Film", inversedBy="translations")
* #var Film
*/
protected $entity;
/**
* Set titre
*
* #param string $titre
*
* #return FilmTranslation
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set locale
*
* #param string $locale
*
* #return FilmTranslation
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
/**
* Set entity
*
* #param \AppBundle\Entity\Film $entity
*
* #return FilmTranslation
*/
public function setEntity(\AppBundle\Entity\Film $entity = null)
{
$this->entity = $entity;
return $this;
}
/**
* Get entity
*
* #return \AppBundle\Entity\Film
*/
public function getEntity()
{
return $this->entity;
}
}
I'm able to create a form but when I try to persist and flush I've the following error :
No mapping found for field 'id' on class 'AppBundle\Entity\Film'.
Is something I'm doing wrong ?
So remove #Polyglot\TranslationCollection from $id annotation like this ;)
class Film
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

Symfony 3 - VichUploader Serving files with a controller

I use Symfony 3.4 with PHP 5.6.
I want to use the vichuploader Bundle to download files. I managed to make it work normally. But now I want to be able to use the files directly from the controller to be able to use them in my database. After reading the documentation, I tried to do something.
I have on my index.html.twig the line :
<td><a href="{{ path('paquet_file', { 'id': uneInfo.id}) }}"</a>{{ uneInfo.urlPaquet }} </td>
On my controller :
namespace Site\PagesBundle\Controller;
use Site\PagesBundle\Entity\Paquet;
use Site\PagesBundle\Entity\TypeUser;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Vich\UploaderBundle\Handler\DownloadHandler;
//........
/**
* Serves an uploaded file.
*
* #Route("/{id}/file", name="paquet_file")
*/
public function fileAction(Paquet $paquet)
{
$downloadHandler = $this->get('vich_uploader.download_handler');
return $downloadHandler->downloadObject($paquet, $fileField = 'paquetFile', Paquet::class, true);
}
My entity :
<?php
namespace Site\PagesBundle\Entity;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Site\PagesBundle\Entity\Paquet;
use Site\PagesBundle\Entity\TypeUser;
use Symfony\Component\HttpFoundation\File\File;
use Doctrine\Common\Collections\ArrayCollection;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Paquet
*
* #ORM\Table(name="paquet")
* #ORM\Entity(repositoryClass="Site\PagesBundle\Repository\PaquetRepository")
* #Vich\Uploadable
*/
class Paquet
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \Doctrine\Common\Collections\Collection
* #ORM\ManyToMany(targetEntity="TypeUser")
* #ORM\JoinTable(name="Packages_des_TypesUser")
* #ORM\JoinColumn(nullable=false)
*/
private $typeUser;
public function __construct()
{
$this->typeUser = new ArrayCollection();
}
/**
* Get TypeUser
*
* #return Site\PagesBundle\Entity\TypeUser
*/
public function getTypeUser()
{
return $this->typeUser;
}
public function deleteTypeFromTypesUser(TypeUser $type)
{
$this->typeUser->removeElement($type);
}
/**
* Set typeUser
*
* #param Site\PagesBundle\Entity\TypeUser $typeUser
*
* #return Paquet
*/
public function setTypeUser(Site\PagesBundle\Entity\TypeUser $typeUser)
{
$this->typeUser = $typeUser;
return $this;
}
/**
* #var string
*
* #ORM\Column(name="titre", type="string", length=255)
*/
private $titre;
/**
* #var string
*
* #ORM\Column(name="urlPaquet", type="string", length=255)
*/
private $urlPaquet;
/**
* #Vich\UploadableField(mapping="paquet", fileNameProperty="urlPaquet")
* #var File
*/
private $paquetFile;
/**
* #ORM\Column(type="datetime")
*
* #var \DateTime
*/
private $updatedAt;
/**
* #param File|UploadedFile $unPaquetFile
*
* #return Paquet
*/
public function setPaquetFile(File $unPaquetFile = null)
{
$this->paquetFile = $unPaquetFile;
if ($unPaquetFile)
{
$this->updatedAt = new \DateTimeImmutable();
}
return $this;
}
/**
* Set updatedAt
*
* #param \DateTime $updatedAt
*
* #return Paquet
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* #return File|null
*/
public function getPaquetFile()
{
return $this->paquetFile;
}
/**
* #var string
*
* #ORM\Column(name="urlNotice", type="string", length=255)
*/
private $urlNotice;
/**
* #Vich\UploadableField(mapping="notice", fileNameProperty="urlNotice")
* #var File
*/
private $noticeFile;
/**
* #var string
*
* #ORM\Column(name="commentaire", type="text")
*/
private $commentaire;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set titre
*
* #param string $titre
*
* #return Paquet
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Set urlPaquet
*
* #param string $urlPaquet
*
* #return Paquet
*/
public function setUrlPaquet($urlPaquet)
{
$this->urlPaquet = $urlPaquet;
return $this;
}
/**
* Get urlPaquet
*
* #return string|null
*/
public function getUrlPaquet()
{
return $this->urlPaquet;
}
/**
* #return File|null
*/
public function getNoticeFile()
{
return $this->noticeFile;
}
/**
* #param File|UploadedFile $uneNoticeFile
*
* #return Paquet
*/
public function setNoticeFile(File $uneNoticeFile = null)
{
$this->noticeFile = $uneNoticeFile;
if ($uneNoticeFile)
{
$this->updatedAt = new \DateTimeImmutable();
}
return $this;
}
/**
* Set urlNotice
*
* #param string $urlNotice
*
* #return Paquet
*/
public function setUrlNotice($urlNotice)
{
$this->urlNotice = $urlNotice;
return $this;
}
/**
* Get urlNotice
*
* #return string
*/
public function getUrlNotice()
{
return $this->urlNotice;
}
/**
* Set commentaire
*
* #param string $commentaire
*
* #return Paquet
*/
public function setCommentaire($commentaire)
{
$this->commentaire = $commentaire;
return $this;
}
/**
* Get commentaire
*
* #return string
*/
public function getCommentaire()
{
return $this->commentaire;
}
}
But when I click on the URL file :
Screen - My page
I have this file :
Screen - Downloaded file
Thanks for your help !
I don't use this bundle yet (I plan to work with it soon though).
But from what I see in the doc page : https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/downloads/serving_files_with_a_controller.md the 4th argument is the filename while you set true to it.
So maybe it's just a filename issue ? You could try to set filename to null (I guess it will take the real filename by default) or set your own filename (with extension) as the 4th argument in the downloadObject() call.

Many to One to Many doctrine, persist only one side

I have a system with two tables and a join table. Reuniao have many Servidor and a Servidor have many Reuniao. For this I have a reuniao table, servidor table and reuniaoservidor table. I want to persist a reuniao with many existing servidor, but I don't want to persist a servidor when a reuniao is persisted. The problem is...
When I try to persist a reuniao doctrine says to me:
"A new entity was found through the relationship 'SistemaIfnmg\Entity\Reuniao#servidores' that was not configured to cascade persist operations for entity: SistemaIfnmg\Entity\ReuniaoServidor#000000006a69473200007f33ddb737e2. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example #ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement 'SistemaIfnmg\Entity\ReuniaoServidor#__toString()' to get a clue."
The Entyties:
Reuniao.php
namespace SistemaIfnmg\Entity;
use SistemaIfnmg\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="reuniao")
* #ORM\HasLifecycleCallbacks()
*/
class Reuniao
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $titulo;
/**
* #ORM\Column(type="string", length=255)
*/
private $descricao;
/**
* #ORM\Column(type="datetime")
*/
private $data;
/**
* #ORM\OneToMany(targetEntity="SistemaIfnmg\Entity\ReuniaoServidor", mappedBy="reuniaofk")
*/
private $servidores;
public function __construct(){
$this->servidores = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* #param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return string
*/
public function getTitulo()
{
return $this->titulo;
}
/**
* #param string $titulo
*/
public function setTitulo($titulo)
{
$this->titulo = $titulo;
}
/**
* #return string
*/
public function getDescricao()
{
return $this->descricao;
}
/**
* #param string $descricao
*/
public function setDescricao($descricao)
{
$this->descricao = $descricao;
}
/**
* #return string
*/
public function getData()
{
return $this->data;
}
/**
* #param string $data
*/
public function setData($data)
{
$this->data = $data;
}
/**
* Add servidores
* #param \SistemaIfnmg\Entity\ReuniaoServidor $servidor
* #return Reuniao
*/
public function addServidor(\SistemaIfnmg\Entity\ReuniaoServidor $servidor){
$this->servidores[] = $servidor;
}
/**
* Remove servidor
*
* #param \SistemaIfnmg\Entity\ReuniaoServidor $servidor
*/
public function removeServidor(\SistemaIfnmg\Entity\ReuniaoServidor $servidor){
$this->servidores->removeElement($servidor);
}
/**
* Get servidores
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getServidores(){
return $this->servidores;
}
}
?>
Servidor.php
<?php
namespace SistemaIfnmg\Entity;
use SistemaIfnmg\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity()
* #ORM\Table(name="servidor")
*/
class Servidor
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
* #var integer $id
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="nome", type="string", length=255)
*/
private $nome;
/**
* #var string
*
* #ORM\Column(name="sobrenome", type="string", length=255)
*/
private $sobrenome;
/**
* #var string
*
* #ORM\Column(name="cargo", type="string", length=255)
*/
private $cargo;
/**
* #var string
*
* #ORM\Column(name="matricula", type="string", length=255)
*/
private $matricula;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set id
* #param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* Get nome
*
* #return string
*/
public function getNome()
{
return $this->nome;
}
/**
* Set nome
*
* #param string $nome
*/
public function setNome($nome)
{
$this->nome = $nome;
}
/**
* Get sobrenome
*
* #return string
*/
public function getSobrenome()
{
return $this->sobrenome;
}
/**
* Set sobrenome
* #param string $sobrenome
*/
public function setSobrenome($sobrenome)
{
$this->sobrenome = $sobrenome;
}
/**
* Get cargo
*
* #return string
*/
public function getCargo()
{
return $this->cargo;
}
/**
* Set cargo
*
* #param string $cargo
*/
public function setCargo($cargo)
{
$this->cargo = $cargo;
}
/**
* Get matricula
*
* #return string
*/
public function getMatricula()
{
return $this->matricula;
}
/**
* Set matricula
*
* #param string $matricula
*/
public function setMatricula($matricula)
{
$this->matricula = $matricula;
}
}
?>
ReuniadoServidor.php
<?php
namespace SistemaIfnmg\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* ReuniaoServidor
*
* #ORM\Table(name="reuniaoservidor", indexes={#ORM\Index(name="fk_Reuniao_has_Servidor_Reuniao1_idx", columns={"reuniaofk"}), #ORM\Index(name="fk_Reuniao_has_Servidor_Servidor1_idx", columns= {"servidorfk"})})
* #ORM\Entity
*/
class ReuniaoServidor
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="SistemaIfnmg\Entity\Reuniao", inversedBy="servidores", cascade={"persist"})
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="reuniaofk", referencedColumnName="id")
* })
*/
private $reuniaofk;
/**
* #ORM\ManyToOne(targetEntity="SistemaIfnmg\Entity\Servidor", inversedBy="servidores")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="servidorfk", referencedColumnName="id")
* })
*/
private $servidorfk;
/**
* Get id
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* #param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* Get reuniaofk
* #return \SistemaIfnmg\Entity\Reuniao
*/
public function getReuniaofk()
{
return $this->reuniaofk;
}
/**
* Set reuniaofk
* #param \SistemaIfnmg\Entity\Reuniao $reuniaofk
* #return ReuniaoServidor
*/
public function setReuniaofk($reuniaofk)
{
$this->reuniaofk = $reuniaofk;
return $this;
}
/**
* Get servidorfk
* #return \SistemaIfnmg\Entity\Servidor
*/
public function getServidorfk()
{
return $this->servidorfk;
}
/**
* Set servidorfk
* #param \SistemaIfnmg\Entity\Servidor $servidorfk
* #return ReuniaoServidor
*/
public function setServidorfk($servidorfk)
{
$this->servidorfk = $servidorfk;
return $this;
}
}
?>
I've been trying to fix this error for a long time, please, help??
Thanks a lot
You don't need the entity that joins Reuniao to Servidor (the ReuniaoServidor entity) as Doctrine takes care of the join table for you. You just need to specify the join columns in the JoinTable mapping.
In Reuniao.php change the mapping of servidores to:
/**
* #ORM\ManyToMany(targetEntity="SistemaIfnmg\Entity\Servidor")
* #ORM\JoinTable(name="reuniao_servidor",
* joinColumns={#ORM\JoinColumn(name="ruuniao_id", referencedColumnName="id")},
* inverseJoinColumns{#ORM\JoinColumn(name="servidor_id", referencedColumnName="id")}
* )
*/
private $servidores;
also add a constructor that initializes the array for the collection
public function __construct()
{
$this->servidores = new ArrayCollection();
}
See https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#many-to-many-unidirectional

Symfony 2 computer hungs when I make any request

Excuse me for my English.
I have 3 classes: User, Category and Service.
When I do even a simple query to the database for Service, for example findAll(), my computer hangs... At the same time, the query for Category and User are good.
Thank you for your help!
User.php
namespace General\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User extends BaseUser
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=255)
*
* #Assert\NotBlank(message="Please enter your type.", groups={"Registration", "Profile"})
* #Assert\Length(
* min=3,
* max="255",
* minMessage="The type is too short.",
* maxMessage="The type is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $type;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set type
*
* #param string $type
* #return User
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return string
*/
public function getType()
{
return $this->type;
}
}
Category.php
namespace General\AnnuaireBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Category
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="General\AnnuaireBundle\Entity\CategoryRepository")
*/
class Category
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="description", type="text")
*/
private $description;
/**
* #var boolean
*
* #ORM\Column(name="statut", type="boolean")
*/
private $statut;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* #ORM\OneToMany(targetEntity="\General\AnnuaireBundle\Entity\Service", mappedBy="categories")
*/
private $services;
/**
* Constructor
*/
public function __construct() {
$this->createdAt = new \DateTime();
$this->services = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* #param string $description
* #return Category
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set statut
*
* #param boolean $statut
* #return Category
*/
public function setStatut($statut)
{
$this->statut = $statut;
return $this;
}
/**
* Get statut
*
* #return boolean
*/
public function getStatut()
{
return $this->statut;
}
/**
* #ORM\PrePersist()
*/
public function setCreatedAt($createdAt = null) {
$this->createdAt = null === $createdAt ? new \DateTime() : $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Add services
*
* #param \General\AnnuaireBundle\Entity\Service $services
* #return Category
*/
public function addService(\General\AnnuaireBundle\Entity\Service $services)
{
$this->services[] = $services;
return $this;
}
/**
* Remove services
*
* #param \General\AnnuaireBundle\Entity\Service $services
*/
public function removeService(\General\AnnuaireBundle\Entity\Service $services)
{
$this->services->removeElement($services);
}
/**
* Get services
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getServices()
{
return $this->services;
}
}
Service.php
namespace General\AnnuaireBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Service
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="General\AnnuaireBundle\Entity\ServiceRepository")
*/
class Service
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="description", type="text")
*/
private $description;
/**
* #var boolean
*
* #ORM\Column(name="statut", type="boolean")
*/
private $statut;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* #ORM\ManyToOne(targetEntity="\General\AnnuaireBundle\Entity\Category", inversedBy="services")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $categories;
/**
* #ORM\ManyToOne(targetEntity="\General\UserBundle\Entity\User")
*/
private $users;
/**
* Constructor
*/
public function __construct() {
$this->createdAt = new \DateTime();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Service
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* #param string $description
* #return Service
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set statut
*
* #param boolean $statut
* #return Service
*/
public function setStatut($statut)
{
$this->statut = $statut;
return $this;
}
/**
* Get statut
*
* #return boolean
*/
public function getStatut()
{
return $this->statut;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Service
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set categories
*
* #param \General\AnnuaireBundle\Entity\Category $categories
* #return Service
*/
public function setCategories(\General\AnnuaireBundle\Entity\Category $categories = null)
{
$this->categories = $categories;
return $this;
}
/**
* Get categories
*
* #return \General\AnnuaireBundle\Entity\Category
*/
public function getCategories()
{
return $this->categories;
}
/**
* Set users
*
* #param \General\UserBundle\Entity\User $users
* #return Service
*/
public function setUsers(\General\UserBundle\Entity\User $users = null)
{
$this->users = $users;
return $this;
}
/**
* Get users
*
* #return \General\UserBundle\Entity\User
*/
public function getUsers()
{
return $this->users;
}
}
Controller.php
public function showServicesAction()
{
$em = $this->getDoctrine()->getManager();
$services = $em->getRepository('GeneralAnnuaireBundle:Service')->findAll();
return $this->render('GeneralAnnuaireBundle:General:list_services.html.twig',
array('services'=>$services,
)
);
}
If I modified my function as:
$user = $this->container->get('security.context')->getToken()->getUser();
$user_id = $user->getId();
// var_dump($user_id); ALL IS RIGHT
$em = $this->getDoctrine()->getManager();
$services = $em->getRepository('GeneralAnnuaireBundle:Service')->findAll();
// var_dump($user_id); ALL IS RIGHT
// IF: var_dump($services[0]); COMPUTER HUNGS
// IF: echo count($services); RESPONSE RIGHT
// IF:
return $this->render('GeneralAnnuaireBundle:General:list_services.html.twig',
array('services'=>$services, ));
// SCREEN WHITE
If you print object in browser it get hanged because symphony object contains lots of info. suppose if you have relation with other tables it will have current tables info as well as other related tables info (Schema, Data, Relationship etc.) as well so it might 99% chance to hanged the browser. Better to print "echo count($services)" to check object exists or not.
Try Debug::dump($service) or any variable - you will be able to see the objects (without the proxy)

Symfony2 - Doctrine exception: class used in the discriminator map does not exist

I am using Symfony2 with Doctrine and when I query from my controller the next error appears(it appears in the navigator when I call for the page):
Entity class 'Bdreamers\SuenoBundle\Entity\Sueno_video' used in the discriminator map of class 'Bdreamers\SuenoBundle\Entity\Sueno' does not exist.
I have one entity(superclass) called "Sueno" and two entities that extend from it(subclasses): Sueno_foto and Sueno_video.
When I load the fixtures, Doctrine works perfectly and fills the database without any issue, filling correctly the discriminator field "tipo" in the "Sueno" table. It also fills correctly the inherited entity table "Sueno_video" introducing the ID of "Sueno" and the exclusive fields of "Sueno_video"
This is the code of the entity file for "Sueno":
<?php
namespace Bdreamers\SuenoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table()
* #ORM\Entity
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="tipo", type="string")
* #ORM\DiscriminatorMap({"sueno" = "Sueno", "video" = "Sueno_video", "foto" = "Sueno_foto"})
*/
class Sueno
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Bdreamers\UsuarioBundle\Entity\Usuario")
**/
private $usuario;
/**
* #ORM\ManyToMany(targetEntity="Bdreamers\SuenoBundle\Entity\Tag", inversedBy="suenos")
* #ORM\JoinTable(name="suenos_tags")
**/
private $tags;
/**
* #ORM\ManyToMany(targetEntity="Bdreamers\UsuarioBundle\Entity\Usuario", mappedBy="suenos_sigue")
* #ORM\JoinTable(name="usuarios_siguen")
**/
private $usuariosSeguidores;
/**
* #ORM\ManyToMany(targetEntity="Bdreamers\UsuarioBundle\Entity\Usuario", mappedBy="suenos_colabora")
* #ORM\JoinTable(name="usuarios_colaboran")
**/
private $usuariosColaboradores;
/**
* #var \DateTime
*
* #ORM\Column(name="fecha_subida", type="datetime")
*/
private $fechaSubida;
/**
* #var string
*
* #ORM\Column(name="titulo", type="string", length=40)
*/
private $titulo;
/**
* #var string
*
* #ORM\Column(name="que_pido", type="string", length=140)
*/
private $quePido;
/**
* #var string
*
* #ORM\Column(name="texto", type="string", length=540)
*/
private $texto;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set usuario
*
* #param string $usuario
* #return Sueno
*/
public function setUsuario($usuario)
{
$this->usuario = $usuario;
return $this;
}
/**
* Get usuario
*
* #return string
*/
public function getUsuario()
{
return $this->usuario;
}
public function getTags()
{
return $this->tags;
}
/**
* Set usuariosSeguidores
*
* #param string $usuariosSeguidores
* #return Sueno
*/
public function setUsuariosSeguidores($usuariosSeguidores)
{
$this->usuariosSeguidores = $usuariosSeguidores;
return $this;
}
/**
* Get usuariosSeguidores
*
* #return string
*/
public function getUsuariosSeguidores()
{
return $this->usuariosSeguidores;
}
/**
* Set usuariosColaboradores
*
* #param string $usuariosColaboradores
* #return Sueno
*/
public function setUsuariosColaboradores($usuariosColaboradores)
{
$this->usuariosColaboradores = $usuariosColaboradores;
return $this;
}
/**
* Get usuariosColaboradores
*
* #return string
*/
public function getUsuariosColaboradores()
{
return $this->usuariosColaboradores;
}
/**
* Set fechaSubida
*
* #param \DateTime $fechaSubida
* #return Sueno
*/
public function setFechaSubida($fechaSubida)
{
$this->fechaSubida = $fechaSubida;
return $this;
}
/**
* Get fechaSubida
*
* #return \DateTime
*/
public function getFechaSubida()
{
return $this->fechaSubida;
}
/**
* Set titulo
*
* #param string $titulo
* #return Sueno
*/
public function setTitulo($titulo)
{
$this->titulo = $titulo;
return $this;
}
/**
* Get titulo
*
* #return string
*/
public function getTitulo()
{
return $this->titulo;
}
/**
* Set quePido
*
* #param string $quePido
* #return Sueno
*/
public function setQuePido($quePido)
{
$this->quePido = $quePido;
return $this;
}
/**
* Get quePido
*
* #return string
*/
public function getQuePido()
{
return $this->quePido;
}
/**
* Set texto
*
* #param string $texto
* #return Sueno
*/
public function setTexto($texto)
{
$this->texto = $texto;
return $this;
}
/**
* Get texto
*
* #return string
*/
public function getTexto()
{
return $this->texto;
}
public function __construct() {
$this->usuariosColaboradores = new \Doctrine\Common\Collections\ArrayCollection();
$this->usuariosSeguidores = new \Doctrine\Common\Collections\ArrayCollection();
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}
public function __toString()
{
return $this->getTitulo();
}
}
And this is the code for the entity Sueno_video:
<?php
namespace Bdreamers\SuenoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table()
* #ORM\Entity
*/
class Sueno_video extends Sueno
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="link_video", type="string", length=255)
*/
private $linkVideo;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set linkVideo
*
* #param string $linkVideo
* #return Sueno_video
*/
public function setLinkVideo($linkVideo)
{
$this->linkVideo = $linkVideo;
return $this;
}
/**
* Get linkVideo
*
* #return string
*/
public function getLinkVideo()
{
return $this->linkVideo;
}
}
And finally the code in the controller:
public function homeAction()
{
$em = $this->getDoctrine()->getManager();
$suenos = $em->getRepository('SuenoBundle:Sueno')->findOneBy(array(
'fechaSubida' => new \DateTime('now -2 days')
));
return $this->render('EstructuraBundle:Home:home_registrado.html.twig');
}
The autoloader won't be able to resolve those class names to file paths, hence why it can't find your classes.
Changing the file and class names to SuenoVideo and SuenoFoto.

Categories