I have Order documents that are embedded in my Table document and I cannot figure out how to update them. For example how can I change the value of "pizza" to "pollo"
My MongoDB is like this:
Tables Document
<?php
namespace Acme\StoreBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/** #MongoDB\Document */
class Tables
{
/**
* #MongoDB\Id
*/
protected $id;
/**
* #MongoDB\Int
*/
protected $number;
/**
* #MongoDB\Int
*/
protected $numordini;
/**
* #MongoDB\String
*/
protected $state;
/**
* #MongoDB\String
*/
protected $left;
/**
* #MongoDB\String
*/
protected $top;
/**
* #MongoDB\String
*/
protected $qrcode;
/**
* #MongoDB\String
*/
protected $idristorante;
/**
* #MongoDB\String
*/
protected $idadmin;
/**
* #MongoDB\EmbedMany(targetDocument="Order")
*/
private $order;
public function __construct()
{
$this->order = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return id $id
*/
public function getId()
{
return $this->id;
}
/**
* Set number
*
* #param int $number
* #return self
*/
public function setNumber($number)
{
$this->number = $number;
return $this;
}
/**
* Get number
*
* #return int $number
*/
public function getNumber()
{
return $this->number;
}
/**
* Set state
*
* #param string $state
* #return self
*/
public function setState($state)
{
$this->state = $state;
return $this;
}
/**
* Get state
*
* #return string $state
*/
public function getState()
{
return $this->state;
}
/**
* Set qrcode
*
* #param string $qrcode
* #return self
*/
public function setQrcode($qrcode)
{
$this->qrcode = $qrcode;
return $this;
}
/**
* Get qrcode
*
* #return string $qrcode
*/
public function getQrcode()
{
return $this->qrcode;
}
/**
* Set idristorante
*
* #param string $idristorante
* #return self
*/
public function setIdristorante($idristorante)
{
$this->idristorante = $idristorante;
return $this;
}
/**
* Get idristorante
*
* #return string $idristorante
*/
public function getIdristorante()
{
return $this->idristorante;
}
/**
* Set idadmin
*
* #param string $idadmin
* #return self
*/
public function setIdadmin($idadmin)
{
$this->idadmin = $idadmin;
return $this;
}
/**
* Get idadmin
*
* #return string $idadmin
*/
public function getIdadmin()
{
return $this->idadmin;
}
/**
* Add order
*
* #param Acme\StoreBundle\Document\Order $order
*/
public function addOrder(\Acme\StoreBundle\Document\Order $order)
{
$this->order[] = $order;
}
/**
* Remove order
*
* #param Acme\StoreBundle\Document\Order $order
*/
public function removeOrder(\Acme\StoreBundle\Document\Order $order)
{
$this->order->removeElement($order);
}
/**
* Get order
*
* #return Doctrine\Common\Collections\Collection $order
*/
public function getOrder()
{
return $this->order;
}
/**
* Set left
*
* #param string $left
* #return self
*/
public function setLeft($left)
{
$this->left = $left;
return $this;
}
/**
* Get left
*
* #return string $left
*/
public function getLeft()
{
return $this->left;
}
/**
* Set top
*
* #param string $top
* #return self
*/
public function setTop($top)
{
$this->top = $top;
return $this;
}
/**
* Get top
*
* #return string $top
*/
public function getTop()
{
return $this->top;
}
/**
* Set numordini
*
* #param int $numordini
* #return self
*/
public function setNumordini($numordini)
{
$this->numordini = $numordini;
return $this;
}
/**
* Get numordini
*
* #return int $numordini
*/
public function getNumordini()
{
return $this->numordini;
}
}
Order Document
<?php
namespace Acme\StoreBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/** #MongoDB\EmbeddedDocument */
class Order
{
/**
* #MongoDB\Id
*/
protected $id;
/**
* #MongoDB\String
*/
protected $nomepiatto;
/**
* #MongoDB\String
*/
protected $variazione;
/**
* #MongoDB\String
*/
protected $prezzo;
/**
* #MongoDB\String
*/
protected $riferimento;
/**
* #MongoDB\String
*/
protected $status;
/**
* Get id
*
* #return id $id
*/
public function getId()
{
return $this->id;
}
/**
* Set nomepiatto
*
* #param string $nomepiatto
* #return self
*/
public function setNomepiatto($nomepiatto)
{
$this->nomepiatto = $nomepiatto;
return $this;
}
/**
* Get nomepiatto
*
* #return string $nomepiatto
*/
public function getNomepiatto()
{
return $this->nomepiatto;
}
/**
* Set variazione
*
* #param string $variazione
* #return self
*/
public function setVariazione($variazione)
{
$this->variazione = $variazione;
return $this;
}
/**
* Get variazione
*
* #return string $variazione
*/
public function getVariazione()
{
return $this->variazione;
}
/**
* Set prezzo
*
* #param string $prezzo
* #return self
*/
public function setPrezzo($prezzo)
{
$this->prezzo = $prezzo;
return $this;
}
/**
* Get prezzo
*
* #return string $prezzo
*/
public function getPrezzo()
{
return $this->prezzo;
}
/**
* Set riferimento
*
* #param string $riferimento
* #return self
*/
public function setRiferimento($riferimento)
{
$this->riferimento = $riferimento;
return $this;
}
/**
* Get riferimento
*
* #return string $riferimento
*/
public function getRiferimento()
{
return $this->riferimento;
}
/**
* Set status
*
* #param string $status
* #return self
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return string $status
*/
public function getStatus()
{
return $this->status;
}
}
databases tables output:
{
"_id" : ObjectId("54ad1e08622ef2b6541fc775"),
"number" : NumberLong(1),
"state" : "ciao",
"idristorante" : "54a8834f622ef2e8688b4572",
"idadmin" : "luca",
"order" : [
{
"_id" : ObjectId("54f71508622ef2df618b4567"),
"nomepiatto" : "opopop",
"prezzo" : "12"
},
{
"_id" : ObjectId("54f71ffb622ef2f47f8b4567"),
"nomepiatto" : "pizza",
"prezzo" : "12"
}
],
"left" : "476px",
"top" : "420px"
}
This will set all Table.order.nomepiatta fields with the value pizza to pollo. Not tested but it should work.
$repo = $odm->getRepository("Table");
$repo->createQueryBuilder()->findAndUpdate()->field('order.nomepiatto')
->set('pollo')->field("order.nomepiatto")->equals('pizza')->getQuery()->execute();
Related
I'm having a similiar issue to Symfony2 Doctrine Unrecognized field:
But mine doesn't have the same solution
I have an entity "category" I'm trying to find all the categories with the attribute "activo" set to true but it gives me the error in the title
I've done php app/console doctrine:generate:entities [path] --no-backup and php app/console doctrine:schema:update --force
My table in my database is updated just as my entity.
<?php
namespace ROSHKA\BBVA\SitioExperience\FrontendBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Expcategorias
*/
class Expcategorias
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $nombre;
/**
* #var string
*/
private $descripcion;
/**
* #var \ROSHKA\BBVA\SitioUniversos\ImagenesBundle\Entity\Imagenes
*/
private $imagen;
/**
* #var string
*/
private $titulo;
/**
* #var boolean
*/
private $activo;
/**
* #var string
*/
private $colorfondo;
/**
* #var \ROSHKA\BBVA\SitioUniversos\ImagenesBundle\Entity\Imagenes
*/
private $imagenfondo;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nombre
*
* #param string $nombre
* #return Expcategorias
*/
public function setNombre($nombre)
{
$this->nombre = $nombre;
return $this;
}
/**
* Get nombre
*
* #return string
*/
public function getNombre()
{
return $this->nombre;
}
/**
* Set descripcion
*
* #param string $descripcion
* #return Expcategorias
*/
public function setDescripcion($descripcion)
{
$this->descripcion = $descripcion;
return $this;
}
/**
* Get descripcion
*
* #return string
*/
public function getDescripcion()
{
return $this->descripcion;
}
/**
* Set imagen
*
* #param \ROSHKA\BBVA\SitioUniversos\ImagenesBundle\Entity\Imagenes $imagen
* #return Expcategorias
*/
public function setImagen(\ROSHKA\BBVA\SitioUniversos\ImagenesBundle\Entity\Imagenes $imagen = null)
{
$this->imagen = $imagen;
return $this;
}
/**
* Get imagen
*
* #return \ROSHKA\BBVA\SitioUniversos\ImagenesBundle\Entity\Imagenes
*/
public function getImagen()
{
return $this->imagen;
}
public function __toString()
{
return $this->getNombre();
}
/**
* Set titulo
*
* #param string $titulo
* #return Expcategorias
*/
public function setTitulo($titulo)
{
$this->titulo = $titulo;
return $this;
}
/**
* Get titulo
*
* #return string
*/
public function getTitulo()
{
return $this->titulo;
}
/**
* Set activo
*
* #param boolean $activo
* #return Expcategorias
*/
public function setActivo($activo)
{
$this->activo = $activo;
return $this;
}
/**
* Get activo
*
* #return boolean
*/
public function getActivo()
{
return $this->activo;
}
/**
* Set colorfondo
*
* #param string $colorfondo
* #return Expcategorias
*/
public function setColorfondo($colorfondo)
{
$this->colorfondo = $colorfondo;
return $this;
}
/**
* Get colorfondo
*
* #return string
*/
public function getColorfondo()
{
return $this->colorfondo;
}
/**
* Set imagenfondo
*
* #param \ROSHKA\BBVA\SitioUniversos\ImagenesBundle\Entity\Imagenes $imagenfondo
* #return Expcategorias
*/
public function setImagenfondo(\ROSHKA\BBVA\SitioUniversos\ImagenesBundle\Entity\Imagenes $imagenfondo = null)
{
$this->imagenfondo = $imagenfondo;
return $this;
}
/**
* Get imagenfondo
*
* #return \ROSHKA\BBVA\SitioUniversos\ImagenesBundle\Entity\Imagenes
*/
public function getImagenfondo()
{
return $this->imagenfondo;
}
}
This is what I use to call it in my controller. When I debug and set a breakpoint here i can see in the expressions sections that my category only has three columns instead of the 9 columns that should have. I don't know what else to do.
$repo = $this->getDoctrine()->getRepository('ROSHKABBVASitioExperienceFrontendBundle:Expcategorias');
$categorias = $repo->findBy(array('activo'=>true));
After thinking for a while I remembered I've installed memcached, then all I did was restart memcached in the task manager, then my project took all the new values.
I'm working in symfony 3.3:
I need to add the #Groups annotation to the slug field (created using ORMBehaviors\Sluggable\Sluggable) so I can pass this field through other entities that I will specify in the Groups.
How can I do this?
this is the entity 'foo':
/**
* #var int
*
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*
* #Groups({"advertisingSpace"})
*/
private $id;
/**
* #var string
*
* #ORM\Column(type="string", length=4)
*
* #Groups({"advertisingSpace"})
*/
private $code;
/**
* #var string
*
* #ORM\Column(type="string", length=255)
*
* #Groups({"advertisingSpace"})
*/
private $description;
/**
* #Vich\UploadableField(mapping="advertisingspacecategory_icon", fileNameProperty="icon")
*
* #var File
*/
private $iconFile;
/**
* #var string
*
* #ORM\Column(type="string", length=255)
*
* #Groups({"advertisingSpace"})
*/
private $icon;
/**
* #var array
*
* #ORM\OneToMany(
* targetEntity="AppBundle\Entity\AdvertisingSpace",
* mappedBy="category"
* )
*/
private $advertisingSpaces;
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #return string
*/
public function getIcon()
{
return $this->icon;
}
/**
* #param string $icon
*
* #return AdvertisingSpaceCategory
*/
public function setIcon($icon)
{
$this->icon = $icon;
return $this;
}
/**
* #param File|\Symfony\Component\HttpFoundation\File\UploadedFile $iconFile
*
* #return AdvertisingSpaceCategory
*/
public function setIconFile(File $iconFile = null)
{
$this->iconFile = $iconFile;
if ($iconFile) {
$this->updatedAt = new \DateTimeImmutable();
}
return $this;
}
/**
* #return File|null
*/
public function getIconFile()
{
return $this->iconFile;
}
/**
* #param int $id
*
* #return AdvertisingSpaceCategory
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* #return string
*/
public function getCode()
{
return $this->code;
}
/**
* #param string $code
*
* #return AdvertisingSpaceCategory
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* #return array
*/
public function getAdvertisingSpaces()
{
return $this->advertisingSpaces;
}
/**
* #param array $advertisingSpaces
*
* #return AdvertisingSpaceCategory
*/
public function setAdvertisingSpaces($advertisingSpaces)
{
$this->advertisingSpaces = $advertisingSpaces;
return $this;
}
/**
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* #param string $description
*
* #return $this
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* #return array
*/
public function getSluggableFields()
{
return [ 'slug' ];
}
/**
* #param string $values
*
* #return string
*/
public function generateSlugValue($values)
{
return implode('-', $values);
}
not having the private slug declaration as I can attribute the #groups annotation to?
You should install the JMS Serializer Bundle and use the #VirtualProperty annotation on your own getSlug() method like the following
/**
* #VirtualProperty
* #Groups({"advertisingSpace"})
*/
public function getSlug()
{
return parent::getSlug();
}
I solved it anyway by creating a new string variable (Eg "SlugName"):
/**
* #var string
*
* #Groups({"advertisingSpace"})
*/
private $slugName;
where I inserted the Groups annotation and which returns the original 'slug' field from its getter:
/**
* # return string
*/
public function getSlugName(): string
{
return $this->slug;
}
i'm trying to upload a file in my database with symfony, I modified my entity, and my form. But when i click on upload a document, i have no error but nothing append (I have no new doc in C:\wamp64\www\myproject\web\upload, and my database field, here support, is null). Here is the code of my entities :
<?php
namespace Main\ConfigBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Invoice
*/
class InvoiceCoach
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $reference_invoice;
/**
* #var string
*/
private $status;
/**
* #var string
*/
private $total = 0;
/**
* #var string
*/
private $tva;
/**
* #var string
*/
private $link;
/**
* #var string
*/
private $paiement_method;
/**
* #var \DateTime
*/
private $date_billing;
/**
* #var \DateTime
*/
private $date_submission;
/**
* #var \DateTime
*/
private $date_creation;
/**
* #var \DateTime
*/
private $date_updated;
/**
* #var string
*/
private $commentaire;
/**
* #var boolean
*/
private $valid_paul = 0;
/**
* #var boolean
*/
private $valid_corinne = 0;
/**
* #var boolean
*/
private $valid_lise = 0;
/**
* #var \Doctrine\Common\Collections\Collection
*/
private $invoice;
/**
* #var \Doctrine\Common\Collections\Collection
*/
private $element;
/**
* #var \Main\ConfigBundle\Entity\Coach
*/
private $coachs;
/**
* #var File
*/
private $file;
/**
* Constructor
*/
public function __construct()
{
$this->invoice = new \Doctrine\Common\Collections\ArrayCollection();
$this->element = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set referenceInvoice
*
* #param string $referenceInvoice
*
* #return InvoiceCoach
*/
public function setReferenceInvoice($referenceInvoice)
{
$this->reference_invoice = $referenceInvoice;
return $this;
}
/**
* Get referenceInvoice
*
* #return string
*/
public function getReferenceInvoice()
{
return $this->reference_invoice;
}
/**
* Set status
*
* #param string $status
*
* #return InvoiceCoach
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return string
*/
public function getStatus()
{
return $this->status;
}
/**
* Set total
*
* #param string $total
*
* #return InvoiceCoach
*/
public function setTotal($total)
{
$this->total = $total;
return $this;
}
/**
* Get total
*
* #return string
*/
public function getTotal()
{
return $this->total;
}
/**
* Set tva
*
* #param string $tva
*
* #return InvoiceCoach
*/
public function setTva($tva)
{
$this->tva = $tva;
return $this;
}
/**
* Get tva
*
* #return string
*/
public function getTva()
{
return $this->tva;
}
/**
* Set link
*
* #param string $link
*
* #return InvoiceCoach
*/
public function setLink($link)
{
$this->link = $link;
return $this;
}
/**
* Get link
*
* #return string
*/
public function getLink()
{
return $this->link;
}
/**
* Set paiementMethod
*
* #param string $paiementMethod
*
* #return InvoiceCoach
*/
public function setPaiementMethod($paiementMethod)
{
$this->paiement_method = $paiementMethod;
return $this;
}
/**
* Get paiementMethod
*
* #return string
*/
public function getPaiementMethod()
{
return $this->paiement_method;
}
/**
* Set dateBilling
*
* #param \DateTime $dateBilling
*
* #return InvoiceCoach
*/
public function setDateBilling($dateBilling)
{
$this->date_billing = $dateBilling;
return $this;
}
/**
* Get dateBilling
*
* #return \DateTime
*/
public function getDateBilling()
{
return $this->date_billing;
}
/**
* Set dateSubmission
*
* #param \DateTime $dateSubmission
*
* #return InvoiceCoach
*/
public function setDateSubmission($dateSubmission)
{
$this->date_submission = $dateSubmission;
return $this;
}
/**
* Get dateSubmission
*
* #return \DateTime
*/
public function getDateSubmission()
{
return $this->date_submission;
}
/**
* Set dateCreation
*
* #param \DateTime $dateCreation
*
* #return InvoiceCoach
*/
public function setDateCreation($dateCreation)
{
$this->date_creation = $dateCreation;
return $this;
}
/**
* Get dateCreation
*
* #return \DateTime
*/
public function getDateCreation()
{
return $this->date_creation;
}
/**
* Set dateUpdated
*
* #param \DateTime $dateUpdated
*
* #return InvoiceCoach
*/
public function setDateUpdated($dateUpdated)
{
$this->date_updated = $dateUpdated;
return $this;
}
/**
* Get dateUpdated
*
* #return \DateTime
*/
public function getDateUpdated()
{
return $this->date_updated;
}
/**
* Set commentaire
*
* #param string $commentaire
*
* #return InvoiceCoach
*/
public function setCommentaire($commentaire)
{
$this->commentaire = $commentaire;
return $this;
}
/**
* Get commentaire
*
* #return string
*/
public function getCommentaire()
{
return $this->commentaire;
}
/**
* Set validPaul
*
* #param boolean $validPaul
*
* #return InvoiceCoach
*/
public function setValidPaul($validPaul)
{
$this->valid_paul = $validPaul;
return $this;
}
/**
* Get validPaul
*
* #return boolean
*/
public function getValidPaul()
{
return $this->valid_paul;
}
/**
* Set validCorinne
*
* #param boolean $validCorinne
*
* #return InvoiceCoach
*/
public function setValidCorinne($validCorinne)
{
$this->valid_corinne = $validCorinne;
return $this;
}
/**
* Get validCorinne
*
* #return boolean
*/
public function getValidCorinne()
{
return $this->valid_corinne;
}
/**
* Set validLise
*
* #param boolean $validLise
*
* #return InvoiceCoach
*/
public function setValidLise($validLise)
{
$this->valid_lise = $validLise;
return $this;
}
/**
* Get validLise
*
* #return boolean
*/
public function getValidLise()
{
return $this->valid_lise;
}
/**
* Add invoice
*
* #param \Main\ConfigBundle\Entity\InvoiceBox $invoice
*
* #return InvoiceCoach
*/
public function addInvoice(\Main\ConfigBundle\Entity\InvoiceBox $invoice)
{
$this->invoice[] = $invoice;
return $this;
}
/**
* Remove invoice
*
* #param \Main\ConfigBundle\Entity\InvoiceBox $invoice
*/
public function removeInvoice(\Main\ConfigBundle\Entity\InvoiceBox $invoice)
{
$this->invoice->removeElement($invoice);
}
/**
* Get invoice
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getInvoice()
{
return $this->invoice;
}
/**
* Add element
*
* #param \Main\ConfigBundle\Entity\InvoiceElement $element
*
* #return InvoiceCoach
*/
public function addElement(\Main\ConfigBundle\Entity\InvoiceElement $element)
{
$this->element[] = $element;
return $this;
}
/**
* Remove element
*
* #param \Main\ConfigBundle\Entity\InvoiceElement $element
*/
public function removeElement(\Main\ConfigBundle\Entity\InvoiceElement $element)
{
$this->element->removeElement($element);
}
/**
* Get element
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getElement()
{
return $this->element;
}
/**
* Set coachs
*
* #param \Main\ConfigBundle\Entity\Coach $coachs
*
* #return InvoiceCoach
*/
public function setCoachs(\Main\ConfigBundle\Entity\Coach $coachs = null)
{
$this->coachs = $coachs;
return $this;
}
/**
* Get coachs
*
* #return \Main\ConfigBundle\Entity\Coach
*/
public function getCoachs()
{
return $this->coachs;
}
/**
* #var string
*/
private $support;
/**
* Set support
*
* #param string $support
*
* #return InvoiceCoach
*/
public function setSupport($support)
{
$this->support = $support;
return $this;
}
/**
* Get support
*
* #return string
*/
public function getSupport()
{
return $this->support;
}
/**
* Set file
*
* #param File $file
* #return InvoiceCoach
*/
public function setFile($file)
{
$this->file = $file;
return $this;
}
/**
* Get file
*
* #return File
*/
public function getFile()
{
return $this->file;
}
// File Upload Method
public function getAbsolutePath()
{
return null === $this->support ? null : $this->getUploadRootDir().'/'.$this->support;
}
public function getPath()
{
return null === $this->support ? null : $this->getUploadDir().'/'.$this->support;
}
protected function getUploadRootDir()
{
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
return 'upload';
}
// Doctrine Specific Method
public function __persist()
{
if($this->file !== null)
$this->support = sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
}
public function __postPersist()
{
if($this->file === null)
return;
$this->file->move($this->getUploadRootDir(), $this->support);
unset($this->file);
$this->file = null;
}
public function __flush()
{
if($this->file === null)
return;
$this->__remove();
$this->__persist();
$this->__postPersist();
}
public function __remove()
{
if($file = $this->getAbsolutePath())
if(file_exists($file))
unlink($file);
}
}
THX a lot
edit : here is the code of my controller (when the form is created) and where I call it on twig :
$em = $this->getDoctrine()->getManager();
$invoice = $em->getRepository("MainConfigBundle:InvoiceCoach")->find($id);
$formUpload = $this->createForm('invoicesupport', $invoice)
->add('save', 'submit', array('label' => 'Enregistrer') );
if($request->getMethod() == "POST"){
$formUpload->handleRequest($request);
if($formUpload->isValid()){
$em->persist($invoice);
$em->flush();
}
return $this->redirect($this->generateUrl('coach_paid_edit', array('id' => $id) ));
}
twig :
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Upload facture</h5>
</div>
<div class="modal-body">
{{ form_start(form, {'attr': {'novalidate': 'novalidate', 'data-parsley-validate':'data-parsley-validate'}}) }}
{{ form_row(formUpload.file) }}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Fermer</button>
{{ form_row(formUpload.save) }}
{{ form_end(form) }}
</div>
</div>
First and most important - did you test that "Doctrine Specific Method" are firing up ?. My guess is that they not firing up (honestly i see first time approach like this - and i don't know why should it work)
Even if - i think that approach is bad , this code should be moved to event listeners/subscribed . It shouldn't be in entity file.
Look at http://symfony.com/doc/current/doctrine/event_listeners_subscribers.html
Here you have link to my old upload solution with Trait connected to entity
https://gist.github.com/poznet/e3955a48fea01d8640dafbd966c53a83
I have below query in MySQL:
SELECT * FROM (orders)
INNER JOIN links ON orders.ref_id = links.id
INNER JOIN users ON links.user_id = users.id
WHERE links.user_id=2
And three entities in Symfony: order, link, user.
How can I write this in Doctrine Query Builder?
$repo = $em->getRepository('OrderBundle:Order');
$queryBuilder=$repo->createQueryBuilder('o');
$query = $queryBuilder
->select('o, l, u')
->innerJoin('c.ref_id', 'l')
->innerJoin('l.user_id', 'u')
->where('u.id=1')
->getQuery();
This doesn't work. I got below error:
[Semantical Error] line 0, col 84 near 'u WHERE u.id': Error: Class OrderBundle\Entity\Order has no association named user_id
I trying for two hours and this make me angry... Thanks in advance.
My order entity:
<?php
// src/OrderBundle/Entity/Order.php
namespace OrderBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints\DateTime;
/**
* #ORM\Entity
* #ORM\Table(name="orders")
*/
class Order
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="OrderBundle\Entity\Order")
* #ORM\JoinColumn(name="ref_id", referencedColumnName="id")
*/
protected $ref_id;
/**
* #ORM\Column(type="datetime")
*/
protected $date_created;
/**
* #ORM\Column(type="string", length=60)
*/
protected $email;
/**
* #ORM\Column(type="string", length=60)
*/
protected $email2;
/**
* #ORM\Column(type="integer")
*/
protected $service;
/**
* #ORM\Column(type="string", length=20)
*/
protected $fromlang;
/**
* #ORM\Column(type="string", length=20)
*/
protected $tolang;
/**
* #ORM\Column(type="boolean")
*/
protected $academic;
/**
* #ORM\Column(type="string", length=40)
*/
protected $discipline;
/**
* #ORM\Column(type="text")
*/
protected $notes;
/**
* #ORM\Column(type="string")
*/
protected $file;
/**
* #ORM\Column(type="integer")
*/
protected $status;
/**
* #ORM\Column(type="integer")
*/
protected $cost;
/**
* #ORM\Column(type="string", length=2)
*/
protected $site;
public function __construct()
{
$this->date_created = new DateTime();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set dateCreated
*
* #param \DateTime $dateCreated
*
* #return Order
*/
public function setDateCreated($dateCreated)
{
$this->date_created = $dateCreated;
return $this;
}
/**
* Get dateCreated
*
* #return \DateTime
*/
public function getDateCreated()
{
return $this->date_created;
}
/**
* Set email
*
* #param string $email
*
* #return Order
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set email2
*
* #param string $email2
*
* #return Order
*/
public function setEmail2($email2)
{
$this->email2 = $email2;
return $this;
}
/**
* Get email2
*
* #return string
*/
public function getEmail2()
{
return $this->email2;
}
/**
* Set refId
*
* #param integer $refId
*
* #return Order
*/
public function setRefId($refId)
{
$this->ref_id = $refId;
return $this;
}
/**
* Get refId
*
* #return integer
*/
public function getRefId()
{
return $this->ref_id;
}
/**
* Set service
*
* #param integer $service
*
* #return Order
*/
public function setService($service)
{
$this->service = $service;
return $this;
}
/**
* Get service
*
* #return integer
*/
public function getService()
{
return $this->service;
}
/**
* Set fromlang
*
* #param string $fromlang
*
* #return Order
*/
public function setFromlang($fromlang)
{
$this->fromlang = $fromlang;
return $this;
}
/**
* Get fromlang
*
* #return string
*/
public function getFromlang()
{
return $this->fromlang;
}
/**
* Set tolang
*
* #param string $tolang
*
* #return Order
*/
public function setTolang($tolang)
{
$this->tolang = $tolang;
return $this;
}
/**
* Get tolang
*
* #return string
*/
public function getTolang()
{
return $this->tolang;
}
/**
* Set academic
*
* #param boolean $academic
*
* #return Order
*/
public function setAcademic($academic)
{
$this->academic = $academic;
return $this;
}
/**
* Get academic
*
* #return boolean
*/
public function getAcademic()
{
return $this->academic;
}
/**
* Set discipline
*
* #param string $discipline
*
* #return Order
*/
public function setDiscipline($discipline)
{
$this->discipline = $discipline;
return $this;
}
/**
* Get discipline
*
* #return string
*/
public function getDiscipline()
{
return $this->discipline;
}
/**
* Set notes
*
* #param string $notes
*
* #return Order
*/
public function setNotes($notes)
{
$this->notes = $notes;
return $this;
}
/**
* Get notes
*
* #return string
*/
public function getNotes()
{
return $this->notes;
}
/**
* Set file
*
* #param string $file
*
* #return Order
*/
public function setFile($file)
{
$this->file = $file;
return $this;
}
/**
* Get file
*
* #return string
*/
public function getFile()
{
return $this->file;
}
/**
* Set status
*
* #param integer $status
*
* #return Order
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return integer
*/
public function getStatus()
{
return $this->status;
}
/**
* Set cost
*
* #param integer $cost
*
* #return Order
*/
public function setCost($cost)
{
$this->cost = $cost;
return $this;
}
/**
* Get cost
*
* #return \cost
*/
public function getCost()
{
return $this->cost;
}
/**
* Set site
*
* #param string $site
*
* #return Order
*/
public function setSite($site)
{
$this->site = $site;
return $this;
}
/**
* Get site
*
* #return string
*/
public function getSite()
{
return $this->site;
}
}
In editAction method of controller I tried to edit existed entities in class table inheritance.
But after form submit I get error:
Neither the property "id" nor one of the methods
"addId()"/"removeId()", "setId()", "id()", "__set()" or "__call()"
exist and have public access in class "AdBundle\Entity\AdFlat".
Why AdFlat entity not extended of AdBase entity?
Base entity:
<?php
namespace AdBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* AdBase
*
* #ORM\Table(name="ad_base")
* #ORM\Entity(repositoryClass="AdBundle\Repository\AdBaseRepository")
* #ORM\HasLifecycleCallbacks()
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="discr", type="string")
* #ORM\DiscriminatorMap({
"flat" = "AdFlat"
* })
*/
abstract class AdBase
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="author", type="string", length=255)
*/
private $author;
/**
* #ORM\ManyToOne(targetEntity="AdBundle\Entity\AdCategory")
*/
private $category;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank(message="Field Location should not be blank")
*/
private $location;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
* #Assert\NotBlank(message="Not specified Title")
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="description", type="text", nullable=true)
* #Assert\NotBlank(message="Not specified Description")
*/
private $description;
/**
* #ORM\OneToMany(targetEntity="AdBundle\Entity\AdPhoto", mappedBy="ad")
*/
private $photos;
/**
* #ORM\Column(type="float")
* #Assert\NotBlank()
*/
private $price;
/**
* #ORM\ManyToOne(targetEntity="AdBundle\Entity\AdPriceType")
*/
private $priceType;
/**
* #var \DateTime
*
* #ORM\Column(name="createdAt", type="datetime")
*/
private $createdAt;
/**
* #var string
*
* #ORM\Column(name="updatedAt", type="datetime")
*/
private $updatedAt;
/**
* #var bool
*
* #ORM\Column(name="visible", type="boolean")
*/
private $visible = false;
/**
* #ORM\Column(type="boolean")
*/
private $active = true;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set author
*
* #param string $author
*
* #return AdBase
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* #return mixed
*/
public function getCategory()
{
return $this->category;
}
/**
* #param mixed $category
*/
public function setCategory($category)
{
$this->category = $category;
}
/**
* #return mixed
*/
public function getLocation()
{
return $this->location;
}
/**
* #param mixed $location
*/
public function setLocation($location)
{
$this->location = $location;
}
/**
* Set title
*
* #param string $title
*
* #return AdBase
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* #param string $description
*
* #return AdBase
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set photos
*
* #param string $photos
*
* #return AdBase
*/
public function setPhotos($photos)
{
$this->photos = $photos;
return $this;
}
/**
* Get photos
*
* #return string
*/
public function getPhotos()
{
return $this->photos;
}
/**
* #return mixed
*/
public function getPrice()
{
return $this->price;
}
/**
* #param mixed $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* #return mixed
*/
public function getPriceType()
{
return $this->priceType;
}
/**
* #param mixed $priceType
*/
public function setPriceType($priceType)
{
$this->priceType = $priceType;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
*
* #return AdBase
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* #param string $updatedAt
*
* #return AdBase
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return string
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set visible
*
* #param boolean $visible
*
* #return AdBase
*/
public function setVisible($visible)
{
$this->visible = $visible;
return $this;
}
/**
* Get visible
*
* #return bool
*/
public function getVisible()
{
return $this->visible;
}
/**
* #return mixed
*/
public function getActive()
{
return $this->active;
}
/**
* #param mixed $active
*/
public function setActive($active)
{
$this->active = $active;
}
/**
* #ORM\PrePersist()
*/
public function prePersist()
{
$this->author = 'voodoo';
$this->createdAt = new \DateTime('now', new \DateTimeZone('UTC'));
$this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
$this->location = 'Donetsk';
}
/**
* #ORM\PreUpdate()
*/
public function preUpdate()
{
$this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
}
}
Extended entity:
<?php
namespace AdBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* AdFlat
*
* #ORM\Table(name="ad_flat")
* #ORM\Entity(repositoryClass="AdBundle\Repository\AdFlatRepository")
*/
class AdFlat extends AdBase
{
/**
* #var integer
*
* #ORM\Column(type="integer")
* #Assert\NotBlank(message="ad_flat.rooms.not_blank")
*/
private $rooms;
/**
* #var float
*
* #ORM\Column(name="square", type="float", nullable=true)
*/
private $square;
/**
* #var float
*
* #ORM\Column(name="liveSquare", type="float", nullable=true)
*/
private $liveSquare;
/**
* #var float
*
* #ORM\Column(type="float", nullable=true)
*/
private $kitchenSquare;
/**
* #var int
*
* #ORM\Column(name="floor", type="integer")
*/
private $floor;
/**
* #var int
*
* #ORM\Column(name="floors", type="integer")
*/
private $floors;
/**
* #var string
*
* #ORM\ManyToOne(targetEntity="AdBundle\Entity\AdWallType")
*/
private $wallType;
/**
* #ORM\ManyToOne(targetEntity="AdBundle\Entity\AdWCType")
*/
private $wcType;
/**
* #return mixed
*/
public function getRooms()
{
return $this->rooms;
}
/**
* #param mixed $rooms
*/
public function setRooms($rooms)
{
$this->rooms = $rooms;
}
/**
* Set square
*
* #param float $square
*
* #return AdFlat
*/
public function setSquare($square)
{
$this->square = $square;
return $this;
}
/**
* Get square
*
* #return float
*/
public function getSquare()
{
return $this->square;
}
/**
* Set liveSquare
*
* #param float $liveSquare
*
* #return AdFlat
*/
public function setLiveSquare($liveSquare)
{
$this->liveSquare = $liveSquare;
return $this;
}
/**
* Get liveSquare
*
* #return float
*/
public function getLiveSquare()
{
return $this->liveSquare;
}
/**
* #return float
*/
public function getKitchenSquare()
{
return $this->kitchenSquare;
}
/**
* #param float $kitchenSquare
*/
public function setKitchenSquare($kitchenSquare)
{
$this->kitchenSquare = $kitchenSquare;
}
/**
* Set floor
*
* #param integer $floor
*
* #return AdFlat
*/
public function setFloor($floor)
{
$this->floor = $floor;
return $this;
}
/**
* Get floor
*
* #return int
*/
public function getFloor()
{
return $this->floor;
}
/**
* Set floors
*
* #param integer $floors
*
* #return AdFlat
*/
public function setFloors($floors)
{
$this->floors = $floors;
return $this;
}
/**
* Get floors
*
* #return int
*/
public function getFloors()
{
return $this->floors;
}
/**
* Set wallType
*
* #param string $wallType
*
* #return AdFlat
*/
public function setWallType($wallType)
{
$this->wallType = $wallType;
return $this;
}
/**
* Get wallType
*
* #return string
*/
public function getWallType()
{
return $this->wallType;
}
/**
* Set wcType
*
* #param string $wcType
*
* #return AdFlat
*/
public function setWcType($wcType)
{
$this->wcType = $wcType;
return $this;
}
/**
* Get wcType
*
* #return string
*/
public function getWcType()
{
return $this->wcType;
}
}
In controller:
public function editAction(Request $request)
{
$adId = $request->get('id');
if (!$adId) {
return $this->renderError('Unknown Ad');
}
$adEntity = $this->getDoctrine()->getRepository('AdBundle:AdBase')->find($adId);
if (null === $adEntity) {
return $this->renderError('Unknown Ad');
}
$reflection = new \ReflectionClass($adEntity);
$adForm = $this->getForm($reflection->getShortName());
$form = $this->createForm($adForm, $adEntity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($adEntity);
$em->flush();
return $this->redirectToRoute('ad_bundle_ad_view', array(
'id' => $adEntity->getId()
));
}
return $this->render('AdBundle:Ad:edit-ad-flat.html.twig', array(
'form' => $form->createView()
));
}
private function renderError($message = '')
{
return $this->render('::error.html.twig', array(
'error' => $message
));
}
private function getEntity($className)
{
$entityName = 'AdBundle\Entity\\' . $className;
return new $entityName();
}
private function getForm($className)
{
$formName = 'AdBundle\Form\Type\\' . $className . 'Type';
return new $formName();
}
The error message simply states that you are missing a way to set the entity's $id property. It doesn't tell you anything about the class not extending the base class. However, your base class only defines a getter method for the $id property but no setter method.
Doctrine Entities work as POPOs (Plain Old PHP Objects). To achieve extending correctly you need to work with the MappedSuperClass here is an example