File Uploads with Doctrine - symfony

I am trying to upload files with Doctrine in Symfony2.
I followed all the steps in this tutorial tutorial but whene I submit my form I get no errors but the file is not uploaded and the path in the table "document" is null.
This is my entity
<?php
namespace projet\ClasseBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Projet
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="projet\ClasseBundle\Entity\ProjetRepository")
* #ORM\HasLifecycleCallbacks
*/
class Projet
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="idMembre", type="integer")
*/
private $idMembre;
/**
* #var integer
*
* #ORM\Column(name="idAssociation", type="integer")
*/
private $idAssociation;
/**
* #var integer
*
* #ORM\Column(name="StatutProjet", type="integer")
*/
private $StatutProjet;
/**
* #var string
*
* #ORM\Column(name="nomProjet", type="string", length=255)
*/
private $nomProjet;
/**
* #var boolean
*
* #ORM\Column(name="visibilite", type="boolean")
*/
private $visibilite;
/**
* #var string
*
* #ORM\Column(name="dateDebut", type="string", length=255)
*/
private $dateDebut;
/**
* #var string
*
* #ORM\Column(name="dateFin", type="string", length=255)
*/
private $dateFin;
/**
* #var float
*
* #ORM\Column(name="budgetActuel", type="float", nullable=true)
*/
private $budgetActuel;
/**
* #var float
*
* #ORM\Column(name="budget", type="float")
*/
private $budget;
/**
* #var string
*
* #ORM\Column(name="description", type="text")
*/
private $description;
/**
* #var string
*
* #ORM\Column(name="ficheProjet", type="string", length=255, nullable=true)
*/
private $ficheProjet;
/**
* #Assert\File(maxSize="6000000")
*/
public $file;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set idMembre
*
* #param integer $idMembre
* #return Projet
*/
public function setIdMembre($idMembre)
{
$this->idMembre = $idMembre;
return $this;
}
/**
* Get idMembre
*
* #return integer
*/
public function getIdMembre()
{
return $this->idMembre;
}
/**
* Set idAssociation
*
* #param integer $idAssociation
* #return Projet
*/
public function setIdAssociation($idAssociation)
{
$this->idAssociation = $idAssociation;
return $this;
}
/**
* Get idAssociation
*
* #return integer
*/
public function getIdAssociation()
{
return $this->idAssociation;
}
/**
* Set StatutProjet
*
* #param integer $StatutProjet
* #return Tache
*/
public function setStatutProjet($StatutProjet)
{
$this->StatutProjet = $StatutProjet;
return $this;
}
/**
* Get StatutProjet
*
* #return integer
*/
public function getStatutProjet()
{
return $this->StatutProjet;
}
/**
* Set nomProjet
*
* #param string $nomProjet
* #return Projet
*/
public function setNomProjet($nomProjet)
{
$this->nomProjet = $nomProjet;
return $this;
}
/**
* Get nomProjet
*
* #return string
*/
public function getNomProjet()
{
return $this->nomProjet;
}
/**
* Set visibilite
*
* #param boolean $visibilite
* #return Projet
*/
public function setVisibilite($visibilite)
{
$this->visibilite = $visibilite;
return $this;
}
/**
* Get visibilite
*
* #return boolean
*/
public function getVisibilite()
{
return $this->visibilite;
}
/**
* Set dateDebut
*
* #param string $dateDebut
* #return Projet
*/
public function setDateDebut($dateDebut)
{
$this->dateDebut = $dateDebut;
return $this;
}
/**
* Get dateDebut
*
* #return string
*/
public function getDateDebut()
{
return $this->dateDebut;
}
/**
* Set dateFin
*
* #param string $dateFin
* #return Projet
*/
public function setDateFin($dateFin)
{
$this->dateFin = $dateFin;
return $this;
}
/**
* Get dateFin
*
* #return string
*/
public function getDateFin()
{
return $this->dateFin;
}
/**
* Set budget
*
* #param float $budget
* #return Projet
*/
public function setBudget($budget)
{
$this->budget = $budget;
return $this;
}
/**
* Get budget
*
* #return float
*/
public function getBudget()
{
return $this->budget;
}
/**
* Set budgetActuel
*
* #param float $budgetActuel
* #return Projet
*/
public function setBudgetActuel($budgetActuel)
{
$this->budgetActuel = $budgetActuel;
return $this;
}
/**
* Get budgetActuel
*
* #return float
*/
public function getBudgetActuel()
{
return $this->budgetActuel;
}
/**
* Set description
*
* #param string $description
* #return Projet
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set ficheProjet
*
* #param string $ficheProjet
* #return Projet
*/
public function setFicheProjet($ficheProjet)
{
$this->ficheProjet = $ficheProjet;
return $this;
}
/**
* Get ficheProjet
*
* #return string
*/
public function getFicheProjet()
{
return $this->ficheProjet;
}
public function getAbsolutePath()
{
return null === $this->ficheProjet ? null : $this->getUploadRootDir().'/'.$this->ficheProjet;
}
public function getWebPath()
{
return null === $this->ficheProjet ? null : $this->getUploadDir().'/'.$this->ficheProjet;
}
protected function getUploadRootDir()
{
// le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche
// le document/image dans la vue.
return 'uploads/documents';
}
/**
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->file) {
// faites ce que vous voulez pour générer un nom unique
$this->ficheProjet = sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
}
}
/**
* #ORM\PostPersist()
* #ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
// s'il y a une erreur lors du déplacement du fichier, une exception
// va automatiquement être lancée par la méthode move(). Cela va empêcher
// proprement l'entité d'être persistée dans la base de données si
// erreur il y a
$this->file->move($this->getUploadRootDir(), $this->ficheProjet);
unset($this->file);
}
/**
* #ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
}
NB : I mean ficheProjet by path.
Anny suggestion ? And thanks.

You should try to use Doctrine Uploadable Extension
You have to install the StofDoctrineExtensionBundle
After you should be able to add on your entity :
namespace My\Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* MyEntity
*
* #ORM\Table(name="my_entity")
* #ORM\Entity()
* #Gedmo\Uploadable(
* path="uploads/my_entity",
* allowOverwrite=true,
* allowedTypes="image/jpeg,image/pjpeg,image/png,image/x-png"
* )
*/
class MyEntity
{
//...
/**
* #var string
*
* #ORM\Column(name="picture", type="string", length=255, nullable=true)
* #Gedmo\UploadableFilePath
* #Assert\File(
* mimeTypes={"image/jpeg", "image/pjpeg", "image/png", "image/x-png"}
* )
*/
private $picture;
//...
}
Don't forget to create the upload folder and set right permissions:
mkdir -p web/uploads/my_entity
chmod -R 777 web/uploads

Related

symfony2 the target-entity OC\PlateformBundle\Entity\Image cannot be found in 'OC\PlatformBundle\Entity\Advert#image

I can't find the solution of the problem when I do the update : the target-entity OC\PlateformBundle\Entity\Image cannot be found in 'OC\PlatformBundle\Entity\Advert#image'.
The Advert.php :
<?php
namespace OC\PlatformBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Advert
*
* #ORM\Table(name="oc_advert")
* #ORM\Entity(repositoryClass="OC\PlatformBundle\Entity\AdvertRepository")
*/
class Advert
{
public function __construct()
{
// Par défaut, la date de l'annonce est la date d'aujourd'hui
$this->date = new \Datetime();
}
/**
* #ORM\OneToOne(targetEntity="OC\PlatformBundle\Entity\Image", cascade={"persist"})
*/
private $image;
public function getImage()
{
return $this->image;
}
public function setImage(Image $image = null)
{
$this->image = $image;
}
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="author", type="string", length=255)
*/
private $author;
/**
* #var string
*
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set date
*
* #param \DateTime $date
*
* #return Advert
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set title
*
* #param string $title
*
* #return Advert
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set author
*
* #param string $author
*
* #return Advert
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set content
*
* #param string $content
*
* #return Advert
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* #ORM\Column(name="published", type="boolean")
*/
private $published = true;
/**
* Set published
*
* #param boolean $published
*
* #return Advert
*/
public function setPublished($published)
{
$this->published = $published;
return $this;
}
/**
* Get published
*
* #return boolean
*/
public function getPublished()
{
return $this->published;
}
}
I have looked for any uppercase ou lowercase problem... but none noticed.

Sonata AdminBundle, get the parameter of an entity

I have an entity Prototype with a relation many to one with project and I want to override a function in the CRUDController.
Here is the code :
$idPrototype = $this->get('request')->get($this->admin->getIdParameter());
I get the id of the prototype, but I want the parameter 'project' ( the id of the project )
Here is my prototype entity
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Prototype
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Entity\PrototypeRepository")
*/
class Prototype
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="nom", type="string", length=255)
*/
private $nom;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=255)
*/
private $description;
/**
* #var \DateTime
*
* #ORM\Column(name="dateCreation", type="date")
*/
private $dateCreation;
private $projet;
public function __construct()
{
$this->dateCreation = new \DateTime("now");
$this->nom = "";
$this->description = " ";
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Get nom
*
* #return string
*/
public function getNom()
{
return $this->nom;
}
public function __toString()
{
return $this->getNom();
}
/**
* Set nom
*
* #param string $nom
* #return Prototype
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Set description
*
* #param string $description
* #return Prototype
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set dateCreation
*
* #param \DateTime $dateCreation
* #return Prototype
*/
public function setDateCreation($dateCreation)
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* Get dateCreation
*
* #return \DateTime
*/
public function getDateCreation()
{
return $this->dateCreation;
}
/**
* Set projet
*
* #param \AppBundle\Entity\Projet $projet
* #return Prototype
*/
public function setProjet(\AppBundle\Entity\Projet $projet = null)
{
$this->projet = $projet;
return $this;
}
/**
* Get projet
*
* #return \AppBundle\Entity\Projet
*/
public function getProjet()
{
return $this->projet;
}
}
I've tried to use ModelManager() but I get this error "Attempted to call an undefined method named "getModelManager" of class "AppBundle\Controller\CRUDProtoController". "
I'm new with sonata and symfony and I find difficulties with it.

Symfony 2 - The annotation in property does not exist, or could not be auto-loaded

I have a weird problem when I want to generate my database schema on Symfony2, one attribut is not accepted...
The annotation "#Doctrine\ORM\Mapping\ManyToOne" in property L3L2\EntraideBundle\Entity\RendezVous::$idDispoProf does not exist, or could not be auto-loaded.
It's even weirder because its working on MacOS X and Windows (Vista & Seven). I tried to make it works on Ubuntu Server VM.
Here is my entities code :
<?php
namespace L3L2\EntraideBundle\Entity;
use L3L2\UserBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
/**
* RendezVous
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="L3L2\EntraideBundle\Entity\RendezVousRepository")
*/
class RendezVous
{
/**
* Constructor
*/
public function __construct()
{
$this->vuEleve = "NON";
$this->vuProf = "NON";
}
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="lieu", type="string", length=255)
*/
private $lieu;
/**
* #var string
*
* #ORM\Column(name="statut", type="string", length=255)
*/
private $statut;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="date")
*/
private $date;
/**
* #var \DateTime
*
* #ORM\Column(name="time", type="time")
*/
private $time;
/**
* #var \DateTime
*
* #ORM\Column(name="datetimeDebut", type="datetime")
*/
private $datetimeDebut;
/**
* #var \DateTime
*
* #ORM\Column(name="datetimeFin", type="datetime")
*/
private $datetimeFin;
//Création de OneToOne vers Evaluation
/**
* #ORM\OneToOne(targetEntity="L3L2\EntraideBundle\Entity\Evaluation", mappedBy="idRdvEval")
* #ORM\JoinColumn(name="id_eval", referencedColumnName="id")
*/
protected $evaluationRdv;
//PROBLEM HERE !!
// Création de ManytoOne vers Disponibilite
/**
* #ORM\ManytoOne(targetEntity="L3L2\EntraideBundle\Entity\Disponibilite")
* #ORM\JoinColumn(name="id_dispo_prof", referencedColumnName="id", onDelete="SET NULL")
*/
protected $idDispoProf;
//Création de ManyToOne vers Cours
/**
* #ORM\ManyToOne(targetEntity="L3L2\EntraideBundle\Entity\Cours", inversedBy="rendezVousCours")
* #ORM\JoinColumn(name="id_cours_rdv", referencedColumnName="id")
*/
protected $idCoursRdv;
//Création de ManyToOne vers User
/**
* #ORM\ManyToOne(targetEntity="L3L2\UserBundle\Entity\User", inversedBy="rendezVousEleve")
* #ORM\JoinColumn(name="id_eleve_rdv", referencedColumnName="id")
*/
protected $idEleveRdv;
/**
* #var string
*
* #ORM\Column(name="vuEleve", type="string", length=255, nullable=true)
*/
private $vuEleve;
/**
* #var string
*
* #ORM\Column(name="vuProf", type="string", length=255, nullable=true)
*/
private $vuProf;
//Création de ManyToOne vers User
/**
* #ORM\ManyToOne(targetEntity="L3L2\UserBundle\Entity\User")
* #ORM\JoinColumn(name="dernierModif", referencedColumnName="id")
*/
private $dernierModif;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set lieu
*
* #param string $lieu
* #return RendezVous
*/
public function setLieu($lieu)
{
$this->lieu = $lieu;
return $this;
}
/**
* Get lieu
*
* #return string
*/
public function getLieu()
{
return $this->lieu;
}
/**
* Set statut
*
* #param string $statut
* #return RendezVous
*/
public function setStatut($statut)
{
$this->statut = $statut;
return $this;
}
/**
* Get statut
*
* #return string
*/
public function getStatut()
{
return $this->statut;
}
/**
* Set evaluationRdv
*
* #param \L3L2\EntraideBundle\Entity\Evaluation $evaluationRdv
* #return RendezVous
*/
public function setEvaluationRdv(\L3L2\EntraideBundle\Entity\Evaluation $evaluationRdv = null)
{
$this->evaluationRdv = $evaluationRdv;
return $this;
}
/**
* Get evaluationRdv
*
* #return \L3L2\EntraideBundle\Entity\Evaluation
*/
public function getEvaluationRdv()
{
return $this->evaluationRdv;
}
/**
* Set idEleveRdv
*
* #param \L3L2\UserBundle\Entity\User $idEleveRdv
* #return RendezVous
*/
public function setIdEleveRdv(\L3L2\UserBundle\Entity\User $idEleveRdv = null)
{
return $this->idEleveRdv = $idEleveRdv;
}
/**
* Get idEleveRdv
*
* #return \L3L2\UserBundle\Entity\User
*/
public function getIdEleveRdv()
{
return $this->idEleveRdv;
}
/**
* Set idDispoProf
*
* #param \L3L2\EntraideBundle\Entity\Disponibilite $idDispoProf
* #return RendezVous
*/
public function setIdDispoProf(\L3L2\EntraideBundle\Entity\Disponibilite $idDispoProf = null)
{
return $this->idDispoProf = $idDispoProf;
}
/**
* Get idDispoProf
*
* #return \L3L2\EntraideBundle\Entity\Disponibilite
*/
public function getIdDispoProf()
{
return $this->idDispoProf;
}
/**
* Set idCoursRdv
*
* #param \L3L2\EntraideBundle\Entity\Cours $idCoursRdv
* #return RendezVous
*/
public function setIdCoursRdv(\L3L2\EntraideBundle\Entity\Cours $idCoursRdv = null)
{
return $this->idCoursRdv = $idCoursRdv;
}
/**
* Get idCoursRdv
*
* #return \L3L2\EntraideBundle\Entity\Cours
*/
public function getIdCoursRdv()
{
return $this->idCoursRdv;
}
/**
* Set date
*
* #param \DateTime $date
* #return RendezVous
*/
public function setDate($date)
{
$this->date = clone $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return clone $this->date;
}
/**
* Set time
*
* #param \DateTime $time
* #return RendezVous
*/
public function setTime($time)
{
$this->time = clone $time;
return $this;
}
/**
* Get time
*
* #return \DateTime
*/
public function getTime()
{
return clone $this->time;
}
/**
* Set vuEleve
*
* #param string $vuEleve
* #return RendezVous
*/
public function setVuEleve($vuEleve)
{
$this->vuEleve = $vuEleve;
return $this;
}
/**
* Get vuEleve
*
* #return string
*/
public function getVuEleve()
{
return $this->vuEleve;
}
/**
* Set vuProf
*
* #param string $vuProf
* #return RendezVous
*/
public function setVuProf($vuProf)
{
$this->vuProf = $vuProf;
return $this;
}
/**
* Get vuProf
*
* #return string
*/
public function getVuProf()
{
return $this->vuProf;
}
/**
* Set datetimeDebut
*
* #param \DateTime $datetimeDebut
* #return RendezVous
*/
public function setDatetimeDebut($datetimeDebut)
{
$this->datetimeDebut = $datetimeDebut;
return $this;
}
/**
* Get datetimeDebut
*
* #return \DateTime
*/
public function getDatetimeDebut()
{
return $this->datetimeDebut;
}
/**
* Set datetimeFin
*
* #param \DateTime $datetimeFin
* #return RendezVous
*/
public function setDatetimeFin($datetimeFin)
{
$this->datetimeFin = $datetimeFin;
return $this;
}
/**
* Get datetimeFin
*
* #return \DateTime
*/
public function getDatetimeFin()
{
return $this->datetimeFin;
}
/**
* Set dernierModif
*
* #param \L3L2\UserBundle\Entity\User $dernierModif
* #return RendezVous
*/
public function setDernierModif(\L3L2\UserBundle\Entity\User $dernierModif = null)
{
$this->dernierModif = $dernierModif;
return $this;
}
/**
* Get dernierModif
*
* #return \L3L2\UserBundle\Entity\User
*/
public function getDernierModif()
{
return $this->dernierModif;
}
}
If I remove the $idDispoProf or annotations before $idDispoProf I can schema:update...
Any idea ?
You have ManytoOne written in lowercase in this specific property instead of ManyToOne in rest of them. Autoloader works in Windows probably because for this system file called ManyToOne.php and ManytoOne.php is the same - Linux is case sensitive.
So solution for you: change ManytoOne to ManyToOne

How to join many to many in createQuery()

I'm working on my portfolio, and I got a page where I show my project. I created 2 cayegorie for now: Programmation and artistic. Each of my project can be a programming project, an artistique project or both. There for, I made a table project and a table categorie and they are join with a many to many relationship. So far, no probleme.
I started to create my query in the repository file of my project entitie. The problem is when I try to join my project entitie and my categorie entitie, it doesn't look like it works, because I specefy that I want just the project that have at least programmation for categorie at least. But it still returns me ALL the project when it should only give me 2 of them.
Did I made my JOIN right?
Here are the entities (watch out for the french!):
Project:
<?php
namespace PublicBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Projet
*
* #ORM\Table(name="pt_projet");
* #ORM\Entity
* #ORM\Entity(repositoryClass="PublicBundle\Entity\ProjetDepot")
*/
class Projet
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
//ID du projet
protected $id;
/**
* #ORM\Column(name="pro_tag", type="string",length=255, unique=true)
*/
//Tag du projet
protected $tag;
/**
* #ORM\OneToMany(targetEntity="ProjetInt", mappedBy="projetId", orphanRemoval=true)
*/
protected $descriptions;
/**
* #ORM\Column(name="pro_img", type="string", length=64, unique=true)
*/
//Nom du fichier de l'image du projet
protected $image;
/**
* #ORM\Column(name="pro_technologie_utilisee", type="text", length=200)
*/
//Text qui liste tout les technologies utilisées pour le projet
protected $technologie;
/**
* #ORM\Column(name="pro_annee", type="integer", length=4)
*/
//Année de réalisation du projet
protected $annee;
/**
* #ORM\ManyToOne(targetEntity="Type", inversedBy="projets")
* #ORM\JoinColumn(name="pro_type", referencedColumnName="id", nullable=false)
*/
//Clef étrangère du type de projet
//Le type de projet ne correspond pas à la catégore. Il peu être Unity, flash, image, vidéo, etc. Il permet de savoir quelle page charger pour pouvoir intégrer le projet dans le portfolio.
protected $type;
/**
* #ORM\Column(name="pro_fichier", type="string", length=64, unique=true)
*/
//Nom du fichier du projet
private $fichier;
/**
* #ORM\ManyToMany(targetEntity="Categorie", cascade={"persist"})
*/
//La ou les catégories du projet
private $categories;
/**
* Constructor
*/
public function __construct()
{
$this->descriptions=new ArrayCollection();
$this->categories=new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set image
*
* #param string $image
* #return Projet
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* #return string
*/
public function getImage()
{
return $this->image;
}
/**
* Set technologie
*
* #param string $technologie
* #return Projet
*/
public function setTechnologie($technologie)
{
$this->technologie = $technologie;
return $this;
}
/**
* Get technologie
*
* #return string
*/
public function getTechnologie()
{
return $this->technologie;
}
/**
* Set annee
*
* #param integer $annee
* #return Projet
*/
public function setAnnee($annee)
{
$this->annee = $annee;
return $this;
}
/**
* Get annee
*
* #return integer
*/
public function getAnnee()
{
return $this->annee;
}
/**
* Set fichier
*
* #param string $fichier
* #return Projet
*/
public function setFichier($fichier)
{
$this->fichier = $fichier;
return $this;
}
/**
* Get fichier
*
* #return string
*/
public function getFichier()
{
return $this->fichier;
}
/**
* Set type
*
* #param Type $type
* #return Projet
*/
public function setType(Type $type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return Type
*/
public function getType()
{
return $this->type;
}
/**
* Add categories
*
* #param Categorie $categories
* #return Projet
*/
public function addCategory(Categorie $categories)
{
$this->categories[] = $categories;
return $this;
}
/**
* Remove categories
*
* #param Categorie $categories
*/
public function removeCategory(Categorie $categories)
{
$this->categories->removeElement($categories);
}
/**
* Get categories
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
/**
* Add description
*
* #param \PublicBundle\Entity\ProjetInt $description
* #return Projet
*/
public function addDescription(\PublicBundle\Entity\ProjetInt $description)
{
$this->description[] = $description;
return $this;
}
/**
* Remove description
*
* #param \PublicBundle\Entity\ProjetInt $description
*/
public function removeDescription(\PublicBundle\Entity\ProjetInt $description)
{
$this->description->removeElement($description);
}
/**
* Get description
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getDescription()
{
return $this->description;
}
}
Category:
<?php
namespace PublicBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Catégorie
*
* #ORM\Table(name="pt_categorie");
* #ORM\Entity
* #ORM\Entity(repositoryClass="PublicBundle\Entity\CategorieDepot")
*/
class Categorie
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
//ID de la catégorie
protected $id;
/**
* #ORM\Column(name="cat_tag", type="string",length=255, unique=true)
*/
//Tag de la catégorie
protected $tag;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set tag
*
* #param string $tag
* #return Categorie
*/
public function setTag($tag)
{
$this->tag = $tag;
return $this;
}
/**
* Get tag
*
* #return string
*/
public function getTag()
{
return $this->tag;
}
}
And the reposetory:
<?php
namespace PublicBundle\Entity;
use Doctrine\ORM\EntityRepository;
/**
* ProjetDepot
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ProjetDepot extends EntityRepository
{
public function rechercherProjets($lang, $cat)
{
return $this->getEntityManager()->createQuery(
'SELECT p.tag,
p.image,
pi.nom,
pi.descriptionCours,
pi.descriptionComplete,
pi.roles,
p.technologie,
pi.aptitudesDeveloppees,
p.annee
FROM PublicBundle:Projet p
JOIN PublicBundle:ProjetInt pi
WITH p.id=pi.projetId
JOIN PublicBundle:Categorie c
WHERE pi.langue=:lang
AND c.tag=:cat'
)->setParameters(array('lang'=>$lang,'cat'=>$cat))->getResult();
}
}
Try this
public function rechercherProjets($lang, $cat) {
$qb = $this->createQueryBuilder('p')
->innerJoin ('p.description', 'pi')
->innerJoin('p.categories', 'pc')
->andWhere('pc.tag = :cat')
->andWhere('pi.langue = :lang')
->setParameters(array('lang'=>$lang,'cat'=>$cat));
return $qb->getQuery()->getResult()
}

Error on update query symfony2

I generated the CRUD for the entity Evenement(event) , which is working. But now I want to update a field in another entity called notification everytime an Evenement is added. Each Evenement has a categorie
Evenement.php:
<?php
namespace Mql14\mqlmeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Mql14\mqlmeBundle\Entity\Evenement
*
* #ORM\Table(name="evenement")
* #ORM\Entity
*/
class Evenement
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string $nom
*
* #ORM\Column(name="nom", type="string", length=45, nullable=true)
*/
private $nom;
/**
* #var datetime $date
*
* #ORM\Column(name="date", type="datetime", nullable=true)
*/
private $date;
/**
* #var string $description
*
* #ORM\Column(name="description", type="string", length=400, nullable=true)
*/
private $description;
/**
* #var integer $ticket
*
* #ORM\Column(name="Ticket", type="integer", nullable=true)
*/
private $ticket;
/**
* #var User
*
* #ORM\ManyToMany(targetEntity="User", mappedBy="evenement")
*/
private $user;
/**
* #var Categorie
*
* #ORM\ManyToOne(targetEntity="Categorie", inversedBy="evenement")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="categorie_id", referencedColumnName="id")
* })
*/
private $categorie;
/**
* #var Lieu
*
* #ORM\ManyToOne(targetEntity="Lieu")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="lieu_id", referencedColumnName="id")
* })
*/
private $lieu;
public function __construct()
{
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* #param string $nom
*/
public function setNom($nom)
{
$this->nom = $nom;
}
/**
* Get nom
*
* #return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set date
*
* #param datetime $date
*/
public function setDate($date)
{
$this->date = $date;
}
/**
* Get date
*
* #return datetime
*/
public function getDate()
{
return $this->date;
}
/**
* Set description
*
* #param string $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set ticket
*
* #param integer $ticket
*/
public function setTicket($ticket)
{
$this->ticket = $ticket;
}
/**
* Get ticket
*
* #return integer
*/
public function getTicket()
{
return $this->ticket;
}
/**
* Add user
*
* #param Mql14\mqlmeBundle\Entity\User $user
*/
public function addUser(\Mql14\mqlmeBundle\Entity\User $user)
{
$this->user[] = $user;
}
/**
* Get user
*
* #return Doctrine\Common\Collections\Collection
*/
public function getUser()
{
return $this->user;
}
/**
* Set categorie
*
* #param Mql14\mqlmeBundle\Entity\Categorie $categorie
*/
public function setCategorie(\Mql14\mqlmeBundle\Entity\Categorie $categorie)
{
$this->categorie = $categorie;
}
/**
* Get categorie
*
* #return Mql14\mqlmeBundle\Entity\Categorie
*/
public function getCategorie()
{
return $this->categorie;
}
/**
* Set lieu
*
* #param Mql14\mqlmeBundle\Entity\Lieu $lieu
*/
public function setLieu(\Mql14\mqlmeBundle\Entity\Lieu $lieu)
{
$this->lieu = $lieu;
}
/**
* Get lieu
*
* #return Mql14\mqlmeBundle\Entity\Lieu
*/
public function getLieu()
{
return $this->lieu;
}
public function getCategorieId(\Mql14\mqlmeBundle\Entity\Categorie $categorie)
{
$idc= $categorie->getId();
return $idc;
}
}
Categorie.php:
<?php
namespace Mql14\mqlmeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Mql14\mqlmeBundle\Entity\Categorie
*
* #ORM\Table(name="categorie")
* #ORM\Entity
*/
class Categorie
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string $nomcat
*
* #ORM\Column(name="nomCat", type="string", length=45, nullable=true)
*/
private $nomcat;
/**
* #var string $photo
*
* #ORM\Column(name="photo", type="string", length=45, nullable=true)
*/
private $photo;
/**
* #var $evenement
*
* #ORM\OneToMany(targetEntity="Evenement", mappedBy="categorie")
*/
private $evenement;
/**
* #var User
*
* #ORM\ManyToMany(targetEntity="User", mappedBy="categorie")
*/
private $user;
public function __construct()
{
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nomcat
*
* #param string $nomcat
*/
public function setNomcat($nomcat)
{
$this->nomcat = $nomcat;
}
/**
* Get nomcat
*
* #return string
*/
public function getNomcat()
{
return $this->nomcat;
}
/**
* Set photo
*
* #param string $photo
*/
public function setPhoto($photo)
{
$this->photo = $photo;
}
/**
* Get photo
*
* #return string
*/
public function getPhoto()
{
return $this->photo;
}
/**
* Add user
*
* #param Mql14\mqlmeBundle\Entity\User $user
*/
public function addUser(\Mql14\mqlmeBundle\Entity\User $user)
{
$this->user[] = $user;
}
/**
* Get user
*
* #return Doctrine\Common\Collections\Collection
*/
public function getUser()
{
return $this->user;
}
/**
* Add evenement
*
* #param Mql14\mqlmeBundle\Entity\Categorie $evenement
*/
public function addEvenement(\Mql14\mqlmeBundle\Entity\Evenement $evenement)
{
$this->evenement[] = $evenement;
}
/**
* Get evenement
*
* #return Doctrine\Common\Collections\Collection
*/
public function getEvenement()
{
return $this->evenement;
}
}
Notification.php:
<?php
namespace Mql14\mqlmeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Mql14\mqlmeBundle\Entity\Notification
*
* #ORM\Table(name="notification")
* #ORM\Entity
*/
class Notification
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string $etat
*
* #ORM\Column(name="etat", type="integer", nullable=true)
*/
private $etat;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="User_id", referencedColumnName="id")
*
* })
*/
private $user;
/**
* #var Categorie
*
* #ORM\ManyToOne(targetEntity="Categorie")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="Categorie_id", referencedColumnName="id")
*
* })
*/
private $categorie;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set etat
*
* #param string $etat
*/
public function setEtat($etat)
{
$this->etat = $etat;
}
/**
* Get etat
*
* #return string
*/
public function getEtat()
{
return $this->etat;
}
/**
* Set user
*
* #param Mql14\mqlmeBundle\Entity\User $user
*/
public function setUser(\Mql14\mqlmeBundle\Entity\User $user)
{
$this->user = $user;
}
/**
* Get user
*
* #return Mql14\mqlmeBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set categorie
*
* #param Mql14\mqlmeBundle\Entity\Categorie $categorie
*/
public function setCategorie(\Mql14\mqlmeBundle\Entity\Categorie $categorie)
{
$this->categorie = $categorie;
}
/**
* Get categorie
*
* #return Mql14\mqlmeBundle\Entity\Categorie
*/
public function getCategorie()
{
return $this->categorie;
}
}
In the createAction that was generated after the execution of the CRUD for the Evenement entity I added the update query :
public function createAction(Request $request)
{
$entity = new Evenement();
$request = $this->getRequest();
$form = $this->createForm(new EvenementType(), $entity);
$form->bindRequest($request);
$entity->upload();
$cat=$entity->getCategorie();
if ($cat) {
$catn = $cat->getNomCat();
$query = $this->container->get('doctrine')->getEntityManager()->createQuery( 'UPDATE Mql14mqlmeBundle:Notification n SET n.etat=1 WHERE n.categorie LIKE :catn ' );
$query->setParameter('categorie', $catn);
$query->execute();
}
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('mql14mqlme_adminshow', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
And the error I'm getting is this:
[Semantical Error] line 0, col 61 near 'categorie LIKE': Error:
Invalid PathExpression. Must be a StateFieldPathExpression.
You set the wrong parameter in the update query.
On this line:
$query = $this->container->get('doctrine')->getEntityManager()->createQuery(
'UPDATE Mql14mqlmeBundle:Notification n SET n.etat=1
WHERE n.categorie LIKE :catn' );
$query->setParameter('categorie', $catn);
$query->execute();
It should be:
$query = $this->container->get('doctrine')->getEntityManager()->createQuery(
'UPDATE Mql14mqlmeBundle:Notification n SET n.etat=1
WHERE n.categorie LIKE :catn' );
$query->setParameter('catn', $catn);
$query->execute();
n.categorie is an entity (of type Mql14\mqlmeBundle\Entity\Categorie) and you can't compare entities using LIKE.
Try replacing LIKE with =.
Change
UPDATE Mql14mqlmeBundle:Notification n SET n.etat=1 WHERE n.categorie LIKE :catn
To
UPDATE Mql14mqlmeBundle:Notification n SET n.etat=1 WHERE n.categorie = :catn

Resources