I've used Translatable with a personal translation; I've implemented PysTranslationand I've used ORM query hint in PysRepository. All of this works fine, the problem has been when I've translated the entity Genero. This entity has a many-to-many relationship with Pys entity and the above method doesn't work. How I can translate the $genNombre attribute of Generoentity?
/**
* Pys
*
* #ORM\Table(name="pys")
* #ORM\Entity(repositoryClass="Filmboot\PYSBundle\Entity\PysRepository")
* #Gedmo\TranslationEntity(class="Filmboot\PYSBundle\Entity\PysTranslation")
*/
class Pys
{
/**
* #var integer
*
* #ORM\Column(name="PYS_ID", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $pysId;
/**
* #var string
*
* #ORM\Column(name="PYS_STR", type="string", length=255, nullable=false)
* #Gedmo\Translatable
*/
private $pysStr;
/**
* #var string
*
* #ORM\Column(name="PYS_TITULO", type="string", length=255, nullable=true)
* #Gedmo\Translatable
*/
private $pysTitulo;
/**
* #var integer
*
* #ORM\Column(name="PYS_DURACION", type="integer", nullable=true)
*/
private $pysDuracion;
/**
* #var integer
*
* #ORM\Column(name="PYS_ANYO", type="integer", nullable=true)
*/
private $pysAnyo;
/**
* #var string
*
* #ORM\Column(name="PYS_PAIS", type="string", length=255, nullable=true)
* #Gedmo\Translatable
*/
private $pysPais;
/**
* #var string
*
* #ORM\Column(name="PYS_SINOPSIS", type="string", length=3000, nullable=true)
* #Gedmo\Translatable
*/
private $pysSinopsis;
/**
* #var string
*
* #ORM\Column(name="PYS_GUIONISTA", type="string", length=255, nullable=true)
*/
private $pysGuionista;
/**
* #ORM\ManyToOne(targetEntity="Filmboot\DirectorBundle\Entity\Director", cascade={"remove"})
* #ORM\JoinColumn(name="DIR_ID", referencedColumnName="DIR_ID", onDelete="CASCADE")
*/
private $director;
/**
* #var string
*
* #ORM\Column(name="PYS_IMAGEN", type="string", length=255, nullable=true)
*/
private $pysImagen;
/**
* #var string
*
* #ORM\Column(name="PYS_IMAGEN_GRANDE", type="string", length=255, nullable=true)
*/
private $pysImagenGrande;
/**
* #ORM\ManyToMany(targetEntity="\Filmboot\ActorBundle\Entity\Actor", mappedBy="peliculas", cascade={"remove"})
*/
private $actores;
/**
* #ORM\ManyToMany(targetEntity="\Filmboot\PYSBundle\Entity\Genero", mappedBy="peliculas", cascade={"remove"})
*/
private $generos;
/**
* #ORM\OneToMany(targetEntity="\Filmboot\PYSBundle\Entity\Premio", mappedBy="pys", cascade={"remove"})
* #ORM\JoinColumn(name="PRE_ID", referencedColumnName="PRE_ID", onDelete="CASCADE")
*/
private $premios;
/**
* #ORM\OneToMany(targetEntity="\Filmboot\UsuarioBundle\Entity\Voto", mappedBy="pys", cascade={"remove"})
* #ORM\JoinColumn(name="PYS_ID", referencedColumnName="PYS_ID", onDelete="CASCADE")
*/
private $votaciones;
/**
* #ORM\OneToMany(targetEntity="PysTranslation", mappedBy="object", cascade={"persist", "remove"})
*/
private $translations;
public function __construct()
{
$this->actores = new ArrayCollection();
$this->generos = new ArrayCollection();
$this->votaciones = new ArrayCollection();
$this->translations = new ArrayCollection();
}
public function getTranslations()
{
return $this->translations;
}
public function addTranslation(PysTranslation $t)
{
if (!$this->translations->contains($t)) {
$this->translations[] = $t;
$t->setObject($this);
}
}
This is Genero
<?php
namespace Filmboot\PYSBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Genero
*
* #ORM\Table(name="genero")
* #ORM\Entity
*/
class Genero
{
/**
* #var integer
*
* #ORM\Column(name="GEN_ID", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $genId;
/**
* #var string
*
* #ORM\Column(name="GEN_NOMBRE", type="string", length=255, nullable=true)
* #Gedmo\Translatable
*/
private $genNombre;
/**
* #ORM\ManyToMany(targetEntity="\Filmboot\PYSBundle\Entity\Pys", inversedBy="generos")
* #ORM\JoinTable(name="P_GENERO",
* joinColumns={#ORM\JoinColumn(name="GEN_ID", referencedColumnName="GEN_ID")},
* inverseJoinColumns={#ORM\JoinColumn(name="PYS_ID", referencedColumnName="PYS_ID")}
* )
*/
private $peliculas;
This is PysTranslation
/**
* #ORM\Entity
* #ORM\Table(name="pys_translation", uniqueConstraints={#ORM\UniqueConstraint(name="lookup_unique_idx", columns={"locale", "object_id", "field"})})
*/
class PysTranslation extends AbstractPersonalTranslation
{
/**
* Convinient constructor
*
* #param string $locale
* #param string $field
* #param string $value
*/
public function __construct($locale, $field, $value)
{
$this->setLocale($locale);
$this->setField($field);
$this->setContent($value);
}
/**
* #ORM\ManyToOne(targetEntity="Pys", inversedBy="translations")
* #ORM\JoinColumn(name="object_id", referencedColumnName="PYS_ID", onDelete="CASCADE")
*/
protected $object;
}
This is PysRepository
class PysRepository extends EntityRepository
{
public function findPeliculas()
{
$em = $this->getEntityManager();
$consulta = $em->createQuery('
SELECT p, a, d, g, pr, v
FROM PYSBundle:Pys p
JOIN p.actores a JOIN p.director d JOIN p.generos g JOIN p.premios pr JOIN p.votaciones v
ORDER BY p.pysTitulo ASC
');
return $consulta->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker')->getResult();
}
/**
*
* #param string $pysStr El slug de la pelĂcula
*/
public function findPys($pysStr)
{
$em = $this->getEntityManager();
$consulta = $em->createQuery('
SELECT p, a, d, g, pr, v
FROM PYSBundle:Pys p
JOIN p.actores a JOIN p.director d JOIN p.generos g JOIN p.premios pr JOIN p.votaciones v
WHERE p.pysStr = :pysStr
');
$consulta->setParameter('pysStr', $pysStr);
return $consulta->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker')->getSingleResult();
}
}
I've solved the problem by adding this line in Genero entity annotations:
/**
* Genero
*
* #ORM\Table(name="genero")
* #ORM\Entity
* #Gedmo\TranslationEntity(class="Filmboot\PYSBundle\Entity\GeneroTranslation")
*/
class Genero
Related
i have two entities 'Panier' and 'Reservation' i want to add a one to many foreign key (the Entity 'Panier' can have many 'Reservation' and 'Reservation' has only one 'Panier' id ) , so i have to add a foreign key 'id' of 'Panier' in my Reservation entity class .
this is my Reservation class :
class Reservation
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="dateReservation", type="datetime", nullable=false)
*/
private $datereservation = 'CURRENT_TIMESTAMP';
/**
* #var integer
*
* #ORM\Column(name="quantite", type="integer", nullable=false)
*/
private $quantite;
/**
* #var float
*
* #ORM\Column(name="total", type="float", precision=10, scale=0, nullable=true)
*/
private $total;
/**
* #var string
*
* #ORM\Column(name="type", type="string", length=255, nullable=false)
*/
private $type;
/**
* #var string
*
* #ORM\Column(name="seat", type="string", length=255, nullable=false)
*/
private $seat;
/**
* #var integer
*
* #ORM\Column(name="payer", type="integer", nullable=true)
*/
private $payer;
/**
* #var string
*
* #ORM\Column(name="nomReservation", type="string", length=255, nullable=true)
*/
private $nomreservation;
/**
* #var \Event
*
* #ORM\ManyToOne(targetEntity="Event")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="event_id", referencedColumnName="id")
* })
*/
private $event;
/**
* #var \User
*
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private $user;
<?php
/** #Entity */
class Reservation {
/**
* #ManyToOne(targetEntity="Panier", inversedBy="reservations")
* #JoinColumn(name="panier_id", referencedColumnName="id")
*/
private $panier;
}
/** #Entity */
class Panier {
/**
* One Panier has many Reservations. This is the inverse side.
* #OneToMany(targetEntity="Reservation", mappedBy="panier")
*/
private $reservations;
public function __construct() {
$this->features = new ArrayCollection();
}
}
Trying to solve this problem with Join in Symfony3.
Somehow the resultset has mixed up values, i.e
ID Exploit Author
ID Author Author
Please see the attached screenshot.
OpenX should have Author Metasploit and the second row shouldn't be there.
Have this code in the controller:
public function indexAction()
{
$exploits = $this->getDoctrine()
->getRepository('AppBundle:Exploit')->createQueryBuilder('e')
->add('select', 'a,e')
->add('from', 'AppBundle:Exploit e')
->leftJoin('AppBundle:Author', 'a')
->where('e.author = a.id')
->getQuery()
->getResult();
return $this->render('exploit/index.html.twig', array(
'exploits' => $exploits
));
}
This view:
<tbody>
{% for exploit in exploits %}
<tr>
<th scope="row">{{ exploit.id }}</th>
<td>{{ exploit.name }}</td>
<td> {{ exploit.author }} </td>
<td>
View
Edit
Delete
</td>
</tr>
{% endfor %}
And these Entities:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Author
*
* #ORM\Table(name="author", indexes={#ORM\Index(name="author_name_id_idx", columns={"id"})})
* #ORM\Entity
*/
class Author
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #var \Exploit
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\OneToOne(targetEntity="Exploit")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id", referencedColumnName="author")
* })
*/
private $id;
/**
* Set name
*
* #param string $name
*
* #return Author
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function getAuthor()
{
return $this->name;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Author
*
* #ORM\Table(name="author", indexes={#ORM\Index(name="author_name_id_idx", columns={"id"})})
* #ORM\Entity
*/
class Author
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #var \Exploit
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\OneToOne(targetEntity="Exploit")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id", referencedColumnName="author")
* })
*/
private $id;
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Category
*
* #ORM\Table(name="category", indexes={#ORM\Index(name="category_name_id_idx", columns={"id"})})
* #ORM\Entity
*/
class Category
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #var \Exploit
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\OneToOne(targetEntity="Exploit")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id", referencedColumnName="category")
* })
*/
private $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;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Category
*
* #ORM\Table(name="category", indexes={#ORM\Index(name="category_name_id_idx", columns={"id"})})
* #ORM\Entity
*/
class Category
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #var \Exploit
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\OneToOne(targetEntity="Exploit")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id", referencedColumnName="category")
* })
*/
private $id;
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Exploit
*
* #ORM\Table(name="exploit", indexes={#ORM\Index(name="exploit_category_idx", columns={"category"}), #ORM\Index(name="exploit_type_idx", columns={"type"}), #ORM\Index(name="exploit_author_idx", columns={"author"})})
* #ORM\Entity
*/
class Exploit
{
/**
* #var integer
*
* #ORM\Column(name="id", type="bigint", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="edb_id", type="string", length=100, nullable=false)
*/
private $edbId;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime", nullable=false)
*/
private $date;
/**
* #var integer
*
* #ORM\Column(name="author", type="bigint", nullable=false)
*/
private $author;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #var integer
*
* #ORM\Column(name="category", type="bigint", nullable=false)
*/
private $category;
/**
* #var string
*
* #ORM\Column(name="version", type="string", length=255, nullable=false)
*/
private $version;
/**
* #var integer
*
* #ORM\Column(name="type", type="bigint", nullable=false)
*/
private $type;
/**
* #var string
*
* #ORM\Column(name="content", type="text", nullable=false)
*/
private $content;
/**
* #var string
*
* #ORM\Column(name="dork", type="string", length=255, nullable=true)
*/
private $dork;
/**
* #var string
*
* #ORM\Column(name="software_link", type="string", length=255, nullable=true)
*/
private $softwareLink;
/**
* #var string
*
* #ORM\Column(name="tested_on", type="string", length=255, nullable=true)
*/
private $testedOn;
/**
* Set edbId
*
* #param integer $edbId
*
* #return Exploit
*/
public function setEdbId($edbId)
{
$this->edbId = $edbId;
return $this;
}
/**
* Get edbId
*
* #return integer
*/
public function getEdbId()
{
return $this->edbId;
}
/**
* Set date
*
* #param \DateTime $date
*
* #return Exploit
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set author
*
* #param integer $author
*
* #return Exploit
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return integer
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set name
*
* #param string $name
*
* #return Exploit
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set category
*
* #param integer $category
*
* #return Exploit
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return integer
*/
public function getCategory()
{
return $this->category;
}
/**
* Set version
*
* #param string $version
*
* #return Exploit
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* Get version
*
* #return string
*/
public function getVersion()
{
return $this->version;
}
/**
* Set type
*
* #param integer $type
*
* #return Exploit
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return integer
*/
public function getType()
{
return $this->type;
}
/**
* Set content
*
* #param string $content
*
* #return Exploit
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set dork
*
* #param string $dork
*
* #return Exploit
*/
public function setDork($dork)
{
$this->dork = $dork;
return $this;
}
/**
* Get dork
*
* #return string
*/
public function getDork()
{
return $this->dork;
}
/**
* Set softwareLink
*
* #param string $softwareLink
*
* #return Exploit
*/
public function setSoftwareLink($softwareLink)
{
$this->softwareLink = $softwareLink;
return $this;
}
/**
* Get softwareLink
*
* #return string
*/
public function getSoftwareLink()
{
return $this->softwareLink;
}
/**
* Set testedOn
*
* #param string $testedOn
*
* #return Exploit
*/
public function setTestedOn($testedOn)
{
$this->testedOn = $testedOn;
return $this;
}
/**
* Get testedOn
*
* #return string
*/
public function getTestedOn()
{
return $this->testedOn;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Exploit
*
* #ORM\Table(name="exploit", indexes={#ORM\Index(name="exploit_category_idx", columns={"category"}), #ORM\Index(name="exploit_type_idx", columns={"type"}), #ORM\Index(name="exploit_author_idx", columns={"author"})})
* #ORM\Entity
*/
class Exploit
{
/**
* #var integer
*
* #ORM\Column(name="id", type="bigint", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="edb_id", type="string", length=100, nullable=false)
*/
private $edbId;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime", nullable=false)
*/
private $date;
/**
* #var integer
*
* #ORM\Column(name="author", type="bigint", nullable=false)
*/
private $author;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #var integer
*
* #ORM\Column(name="category", type="bigint", nullable=false)
*/
private $category;
/**
* #var string
*
* #ORM\Column(name="version", type="string", length=255, nullable=false)
*/
private $version;
/**
* #var integer
*
* #ORM\Column(name="type", type="bigint", nullable=false)
*/
private $type;
/**
* #var string
*
* #ORM\Column(name="content", type="text", nullable=false)
*/
private $content;
/**
* #var string
*
* #ORM\Column(name="dork", type="string", length=255, nullable=true)
*/
private $dork;
/**
* #var string
*
* #ORM\Column(name="software_link", type="string", length=255, nullable=true)
*/
private $softwareLink;
/**
* #var string
*
* #ORM\Column(name="tested_on", type="string", length=255, nullable=true)
*/
private $testedOn;
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Type
*
* #ORM\Table(name="type", indexes={#ORM\Index(name="type_name_id_idx", columns={"id"})})
* #ORM\Entity
*/
class Type
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #var \Exploit
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\OneToOne(targetEntity="Exploit")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id", referencedColumnName="type")
* })
*/
private $id;
/**
* Set name
*
* #param string $name
*
* #return Type
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Type
*
* #ORM\Table(name="type", indexes={#ORM\Index(name="type_name_id_idx", columns={"id"})})
* #ORM\Entity
*/
class Type
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #var \Exploit
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\OneToOne(targetEntity="Exploit")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id", referencedColumnName="type")
* })
*/
private $id;
}
Anybody know how to correct my join query ? Or why it behaves like this?
Thanks,
Try this:
->leftJoin('AppBundle:Author', 'a', "WITH", "e.author = a.id")
Instead of this:
->leftJoin('AppBundle:Author', 'a')
->where('e.author = a.id')
Try
public function indexAction()
{
$exploits = $this->getDoctrine()
->getRepository('AppBundle:Exploit')->createQueryBuilder('e')
->leftjoin('e.author','a')
->addSelect('a')
->getQuery()
->getResult();
return $this->render('exploit/index.html.twig', array(
'exploits' => $exploits
));
}
and in the view
<tbody>
{% for exploit in exploits %}
<tr>
<th scope="row">{{ exploit.id }}</th>
<td>{{ exploit.name }}</td>
<td> {{ exploit.author.name }} </td>
<td>
View
Edit
Delete
</td>
</tr>
{% endfor %}
</tbody>
how i can to do this: I have OrderWork entity this for order and have relation manyToMany with Status entity. All work good, but i want have date for every saved and updated order.
* Order
*
* #ORM\Table(name="order_work")
* #ORM\Entity(repositoryClass="AppBundle\Repository\OrderWorkRepository")
*/
class OrderWork
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Client", cascade={"persist"})
* #ORM\JoinColumn(name="client_id", referencedColumnName="id")
*/
private $client;
/**
* #var string
*
* #ORM\Column(name="orderNumber", type="string", length=255)
*/
private $orderNumber;
/**
* #var string
*
* #ORM\Column(name="orderCity", type="string", length=255)
*/
private $orderCity;
/**
* #var date
*
* #ORM\Column(name="date", type="string", length=255)
*/
private $date;
/**
* #var \DateTime
*
* #ORM\Column(name="orderDate", type="string", length=255, options={"default": NULL})
*/
private $orderDate;
/**
* #var \DateTime
*
* #ORM\Column(name="returnDate", type="string", length=255, nullable=true)
*/
private $returnDate;
/**
* #var string
*
* #ORM\Column(name="device", type="string", length=255)
*/
private $device;
/**
* #ORM\ManyToOne(targetEntity="SurrogatePhone", cascade={"persist"})
* #ORM\JoinColumn(name="surrogate_id", referencedColumnName="id")
*/
private $surrogatePhone;
/**
* #var int
*
* #ORM\Column(name="orderType", type="integer")
*/
public $orderType;
/**
* #ORM\ManyToMany(targetEntity="Status")
* #ORM\JoinTable(name="order_status",
* joinColumns={#ORM\JoinColumn(name="order_id", referencedColumnName="id", unique=false)},
* inverseJoinColumns={#ORM\JoinColumn(name="status_id", referencedColumnName="id", unique=false)}
* )
*/
private $status;
How better resolve this solution?
Define date in your constructor (for creating):
class OrderWork
{
//...
public function __construct()
{
$this->date = new DateTime();
}
}
And update date field when updating:
$orderWork->setDate(new DateTime());
$em->flush();
I created a custom bundle called SimpleMedia. When I try to load a page that loads a company it gives me the Runtime Notice
Runtime Notice: Accessing static property Proxies__CG__\SimpleMediaBundle\Entity\Media::$lazyPropertiesDefaults as non static.
I know it has something to do with my one to one relation
(logoFile(Company Entity) -> id (Media Entity).
Anyone have any ideas? I am using Symfony 2.7
Controller
/**
* #Route("/{id}", name="api_company_show")
* #Method("GET")
* #param $id
* #return Response $response
*/
public function showAction($id) {
$company = $this->findCompanyById($id);
$response = $this->createApiResponse($company, 200);
return $response;
}
/**
* Find company by id. Throw exception if it does not.
* #param $id
* #return Company $company
*/
private function findCompanyById($id) {
$company = $this->getDoctrine()
->getRepository('AppBundle:Company')
->find($id);
if (!$company) {
throw $this->createNotFoundException(sprintf(
'No company found with id "%d"',
$id
));
}
return $company;
}
Company.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Entity\User;
use Symfony\Component\HttpFoundation\File\File;
use SimpleMediaBundle\Entity\Media;
/**
* Company
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Repository\CompanyRepository")
*/
class Company
{
/**
* #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="website", type="string", length=255, nullable=true)
*/
private $website;
/**
* #var string
* #ORM\Column(name="address_1", type="string", length=255, nullable=true)
*/
private $address1;
/**
* #var string
* #ORM\Column(name="address_2", type="string", length=255, nullable=true)
*/
private $address2;
/**
* #var string
* #ORM\Column(name="city", type="string", length=255, nullable=true)
*/
private $city;
/**
* #var string
* #ORM\Column(name="state", type="string", length=255, nullable=true)
*/
private $state;
/**
* #var string
* #ORM\Column(name="zipcode", type="string", length=50, nullable=true)
*/
private $zipcode;
/**
* #var string
* #ORM\Column(name="country", type="string", length=255, nullable=true)
*/
private $country;
/**
* #var string
* #ORM\Column(name="phone", type="string", length=50, nullable=true)
*/
private $phone;
/**
* #var string
* #ORM\Column(name="fax", type="string", length=50, nullable=true)
*/
private $fax;
/**
* #var integer
* #ORM\Column(name="years_in_business", type="integer", length=50, nullable=true)
*/
private $yearsInBusiness;
/**
* #var string
* #ORM\Column(name="number_of_employees", type="integer", length=50, nullable=true)
*/
private $numberOfEmployees;
/**
* #var integer
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\User")
*/
private $owners;
/**
* #ORM\OneToOne(targetEntity="SimpleMediaBundle\Entity\Media", cascade={"persist", "remove"}, orphanRemoval=true)
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="logo_file", referencedColumnName="id", nullable=true)
* })
*/
protected $logoFile;
/**
* #ORM\Column(type="datetime", name="updated", nullable=true)
* #var \DateTime $updated
*/
protected $updated;
public function __construct()
{
$this->owners = new ArrayCollection();
$this->logoFile = new ArrayCollection();
}
/**
* Get id
* #return integer
*/
public function getId()
{
return $this->id;
}
// Basic Getter and Setters
/**
* Set logo file id.
* #param Media $media
* #return $this
*/
public function setLogoFile(Media $media)
{
$this->logoFile = $media;
return $this;
}
/**
* Get Log File.
* #return File
*/
public function getLogoFile()
{
return $this->logoFile;
}
}
Media.php
namespace SimpleMediaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use SimpleMediaBundle\Entity\MediaInterface;
/**
* #ORM\Table()
* #ORM\Entity(repositoryClass="SimpleMediaBundle\Repository\MediaRepository")
*/
class Media implements MediaInterface
{
/**
* #var integer $id
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $name
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string $path
* #ORM\Column(name="path", type="string", length=500)
*/
private $path;
/**
* #var string $size
* #ORM\Column(name="size", type="integer", nullable=true)
*/
private $size;
/**
* #var string $createdAt
* #ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* #var string $createdAt
* #ORM\Column(name="updated_at", type="datetime")
*/
private $updatedAt;
/**
* #var string $contentType
* #ORM\Column(name="content_type", type="string", length=50, nullable=true)
*/
private $contentType;
/**
* #var string $type
* #ORM\Column(name="type", type="string", length=255)
*/
private $type;
public function __construct()
{
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
/**
* Get id
* #return integer
*/
public function getId()
{
return $this->id;
}
// Basic Getter and Setters
/**
* Transform to string
* #return string
*/
public function __toString()
{
return (string) $this->getId();
}
}
Changing the fetch mode to EAGER fixed the issue for me.
/**
* #var Media
*
* #ORM\OneToOne(targetEntity="SimpleMediaBundle\Entity\Media", cascade={"persist", "remove"}, orphanRemoval=true, fetch="EAGER")
* #ORM\JoinColumn(name="logo_file", referencedColumnName="id", nullable=true)
*/
private $logoFile;
I think you have define the relationship wrong. I think should be:
/**
*
* #ORM\OneToOne(targetEntity="SimpleMediaBundle\Entity\Media", cascade={"persist", "remove"}, orphanRemoval=true)
* #ORM\JoinColumn(name="logo_file", referencedColumnName="id", nullable=true)
*/
protected $logoFile;
On the other hand, in the company constructor you dont need initialiser the $logoFile variable because is not a array it is just one value.
I'm a begginer in Symfony2...
I have a table "TUTORIEL" and an other table 'Groupe_de_categories' linked by 'id_groupe_categorie'
TABLE Tutoriels :
TABLE Groupe_de_categories
I need to count for each "titre_categorie_nv1" (here in my example 'Maison', 'Art&Loisir', 'Enseignement'...) how many "TUTORIEL" I have where "tutoriel_controle" = 'no'
Before to use Synfony2, I hade this code in PHP (works nice):
<?php
$query_nb_cat = "SELECT CATEGORIE, COUNT(*)
FROM Tutoriels
INNER JOIN Groupe_de_categories
ON TUTORIEL.id_groupe_categorie = Groupe_de_categories.id_groupe_categorie
WHERE tutoriel_controle='no'
GROUP BY CATEGORIE_TITLE";
$nb_cat = mysqli_query($BDD_connect, $query_nb_cat)or die(log_mysql($query_nb_cat));
$row_nb_cat = mysqli_fetch_assoc($nb_cat);
do {
$tableau_nb_cat[]=array(
'titre_cat_nv1'=>$row_nb_cat['titre_categorie_nv1'],
'compte_cat_nv1'=>$row_nb_cat['COUNT(*)'],
);
} while ($row_nb_cat = mysqli_fetch_assoc($nb_cat));
$val_cat=array(
'valeur_retour'=>$tableau_nb_cat
);
mysqli_free_result($nb_cat);
mysqli_close($BDD_connect);
?>
The result was something like that :
Maison => 3
Art&Loisir => 9
Enseignement => 14
...
How can I do this with Symfony2 ?
Here is my ORM :
Tutoriel 'Video2LearnBddBundle:Tutoriels':
Tutoriels
{
/**
* #var integer
*
* #ORM\Column(name="id_tutoriel", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idTutoriel;
/**
* #var string
*
* #ORM\Column(name="titre", type="string", length=70, nullable=false)
*/
private $titre;
/**
* #var string
*
* #ORM\Column(name="description", type="text", nullable=false)
*/
private $description;
/**
* #var \DateTime
*
* #ORM\Column(name="date_creation", type="datetime", nullable=false)
*/
private $dateCreation = 'CURRENT_TIMESTAMP';
/**
* #var string
*
* #ORM\Column(name="nombre_de_vues", type="string", length=45, nullable=true)
*/
private $nombreDeVues;
/**
* #var string
*
* #ORM\Column(name="FAQs", type="string", length=45, nullable=true)
*/
private $faqs;
/**
* #var string
*
* #ORM\Column(name="exclusivite", type="string", nullable=false)
*/
private $exclusivite;
/**
* #var string
*
* #ORM\Column(name="eligible_promotion", type="string", nullable=false)
*/
private $eligiblePromotion;
/**
* #var string
*
* #ORM\Column(name="info_moderateur", type="string", length=300, nullable=true)
*/
private $infoModerateur;
/**
* #var string
*
* #ORM\Column(name="tutoriel_controle", type="string", nullable=false)
*/
private $tutorielControle = 'non';
/**
* #var string
*
* #ORM\Column(name="niveau_choix_categorie_autre", type="string", nullable=true)
*/
private $niveauChoixCategorieAutre;
/**
* #var string
*
* #ORM\Column(name="categorie_autre", type="string", length=45, nullable=true)
*/
private $categorieAutre;
/**
* #var \GroupeDeCategories
*
* #ORM\ManyToOne(targetEntity="GroupeDeCategories")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_groupe_categorie", referencedColumnName="id_groupe_categorie")
* })
*/
private $idGroupeCategorie;
/**
* #var \Membres
*
* #ORM\ManyToOne(targetEntity="Membres")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_membre", referencedColumnName="id_membre")
* })
*/
private $idMembre;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="MotsCles", inversedBy="idTutoriel")
* #ORM\JoinTable(name="mots_cles_et_tutoriels",
* joinColumns={
* #ORM\JoinColumn(name="id_tutoriel", referencedColumnName="id_tutoriel")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="mots_cles", referencedColumnName="mots_cles")
* }
* )
*/
private $motsCles;
Groupe_de_categorie GroupeDeCategories:
<?php
namespace Video2Learn\BddBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* GroupeDeCategories
*
* #ORM\Table(name="Groupe_de_categories", indexes={
* #ORM\Index(name="fk_Groupe_de_categories_Categories_nv11_idx", columns={"titre_categorie_nv1"}),
* #ORM\Index(name="fk_Groupe_de_categories_Categories_nv21_idx", columns={"titre_categorie_nv2"}),
* #ORM\Index(name="fk_Groupe_de_categories_Categories_nv31_idx", columns={"titre_categorie_nv3"}),
* #ORM\Index(name="fk_Groupe_de_categories_Categories_nv41_idx", columns={"titre_categorie_nv4"}),
* #ORM\Index(name="fk_Groupe_de_categories_Categories_nv51_idx", columns={"titre_categorie_nv5"})
* })
* #ORM\Entity
*/
class GroupeDeCategories
{
/**
* #var integer
*
* #ORM\Column(name="id_groupe_categorie", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idGroupeCategorie;
/**
* #var \CategoriesNv1
*
* #ORM\ManyToOne(targetEntity="Video2Learn\BddBundle\Entity\CategoriesNv1", inversedBy="GroupeDeCategories")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="titre_categorie_nv1", referencedColumnName="titre_categorie_nv1")
* })
*/
private $titreCategorieNv1;
/**
* #var \CategoriesNv2
*
* #ORM\ManyToOne(targetEntity="CategoriesNv2")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="titre_categorie_nv2", referencedColumnName="titre_categorie_nv2")
* })
*/
private $titreCategorieNv2;
/**
* #var \CategoriesNv3
*
* #ORM\ManyToOne(targetEntity="CategoriesNv3")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="titre_categorie_nv3", referencedColumnName="titre_categorie_nv3")
* })
*/
private $titreCategorieNv3;
/**
* #var \CategoriesNv4
*
* #ORM\ManyToOne(targetEntity="CategoriesNv4")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="titre_categorie_nv4", referencedColumnName="titre_categorie_nv4")
* })
*/
private $titreCategorieNv4;
/**
* #var \CategoriesNv5
*
* #ORM\ManyToOne(targetEntity="CategoriesNv5")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="titre_categorie_nv5", referencedColumnName="titre_categorie_nv5")
* })
*/
private $titreCategorieNv5;
Thanks !
You can do it by using createQueryBuilder in your repository, or you can use your current query as NativeQuery
Here is a sample (I assume the name of your entities are Tutoriels and GroupeDeCategories)
$em = $this->getDoctrine()->getManager();
$query = $em->getRepository('YourBundle:Tutoriels')
->createQueryBuilder('T')
->join('T.idGroupeCategorie', 'GC')
-->select('COUNT(GC.titreCategorieNv1) AS CT1')
->where("T.tutorielControle = 'no'")
->groupBy('GC.titreCategorieNv1')
->getQuery();
$result = $query->getScalarResult(); //This will return an array of the counts
In your entity I could not find CATEGORIE_TITLE to use in GroupBy and CATEGORIE to add in the selection; so I grouped them by titreCategorieNv1. You can add more fields for your select.
Maybe this link helps you more Symfony CreateQueryBuilder
I did that :
public function updateAction($type_update)
{
$request = Request::createFromGlobals();
$result = $request->isXmlHttpRequest();
if ($result === true) {
switch ($type_update) {
case "compteur_menu":
$em = $this->getDoctrine()->getManager();
$result = $em->createQuery("
SELECT TC.titreCategorieNv1, COUNT(TC.titreCategorieNv1) AS num
FROM Video2LearnBddBundle:Tutoriels T
INNER JOIN T.idGroupeCategorie GC
INNER JOIN GC.titreCategorieNv1 TC
WHERE T.tutorielControle='non'
GROUP BY TC.titreCategorieNv1"
)
->getResult();
$response = new Response();
$response->setStatusCode(Response::HTTP_OK);
$response->setContent(json_encode($result));
$response->headers->set('Content-Type', 'application/json');
return $response;
default:
break;
}
}
}
You can do a repository (more infos here : http://symfony.com/fr/doc/current/book/doctrine.html )
And you could use Doctrine Query Language.
But I think your struture is wrong and it wold be better to have a category, this category has a parent category, ...
So you'll have for example a rootcatégory who has childs named "Bricolage", "Sport", ...
"Bricolage" would have, for exemple a category "Peinture" as child, and so on ;)