Related
I'm a Symfony beginner and been stuck on this issue for a while. I have two entities (Mairie and Ville) with a many-to-one relation. Ville is an imported table, pre-filled with info. In a form, the user creates an instance of Mairie and with the form data, it will find a Ville to bind to.
Problem is : doctrine does find a Ville object when the Mairie is created, but when I want to set it to the Mairie object, it is nulled. Here's my code, in the Controller :
if($formMairie->isSubmitted() && $formMairie->isValid())
{
$repoMairie = $this->getDoctrine()->getRepository(Mairie::class);
$repoVilles = $this->getDoctrine()->getRepository(Villes::class);
$inseeInput = $mairie->getInsee();
$ville = $repoVilles->findOneBy(array("ville_code_commune" => $inseeInput));
dump($ville);
This dump works : it finds the right instance of Ville in the database and returns an object filled with the right properties.
$mairie->setVilles($ville);
This doesn't work, the Ville property of Mairie is set to null and I get no errors when I flush.
$mairieVille = $mairie->getVilles();
dump($mairieVille);
Returns null.
Why isn't the object registered ? I thought this could be an SQL issue since I've imported the Ville table but I can't find anything. I have used this method to bind other entities together and I've had no issue until now... Here are my two entities for more info (getters and setters not included) :
Ville:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Villes
*
* #ORM\Table(name="villes")
* #ORM\Entity
*/
class Villes
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var int
*
* #ORM\Column(name="ville_id", type="integer", nullable=false)
*/
private $ville_id;
/**
* #var string|null
*
* #ORM\Column(name="ville_departement", type="string", length=3, nullable=true)
*/
private $ville_departement;
/**
* #var string|null
*
* #ORM\Column(name="ville_slug", type="string", length=255, nullable=true)
*/
private $ville_slug;
/**
* #var string|null
*
* #ORM\Column(name="ville_nom", type="string", length=45, nullable=true)
*/
private $ville_nom;
/**
* #var string|null
*
* #ORM\Column(name="ville_nom_simple", type="string", length=45, nullable=true)
*/
private $ville_nom_simple;
/**
* #var string|null
*
* #ORM\Column(name="ville_nom_reel", type="string", length=45, nullable=true)
*/
private $ville_nom_reel;
/**
* #var string|null
*
* #ORM\Column(name="ville_nom_soundex", type="string", length=20, nullable=true)
*/
private $ville_nom_soundex;
/**
* #var string|null
*
* #ORM\Column(name="ville_nom_methaphone", type="string", length=22, nullable=true)
*/
private $ville_nom_methaphone;
/**
* #var string|null
*
* #ORM\Column(name="ville_code_postal", type="string", length=255, nullable=true)
*/
private $ville_code_postal;
/**
* #var string|null
*
* #ORM\Column(name="ville_commune", type="string", length=3, nullable=true)
*/
private $ville_commune;
/**
* #var string
*
* #ORM\Column(name="ville_code_commune", type="string", length=5, nullable=false)
*/
private $ville_code_commune;
/**
* #var int|null
*
* #ORM\Column(name="ville_arrondissement", type="integer", nullable=true)
*/
private $ville_arrondissement;
/**
* #var string|null
*
* #ORM\Column(name="ville_canton", type="string", length=4, nullable=true)
*/
private $ville_canton;
/**
* #var int|null
*
* #ORM\Column(name="ville_amdi", type="integer", nullable=true)
*/
private $ville_amdi;
/**
* #var int|null
*
* #ORM\Column(name="ville_population_2010", type="integer", nullable=true)
*/
private $ville_population_2010;
/**
* #var int|null
*
* #ORM\Column(name="ville_population_1999", type="integer", nullable=true)
*/
private $ville_population_1999;
/**
* #var int|null
*
* #ORM\Column(name="ville_population_2012", type="integer", nullable=true)
*/
private $ville_population_2012;
/**
* #var int|null
*
* #ORM\Column(name="ville_densite_2010", type="integer", nullable=true)
*/
private $ville_densite_2010;
/**
* #var float|null
*
* #ORM\Column(name="ville_surface", type="float", precision=10, scale=0, nullable=true)
*/
private $ville_surface;
/**
* #var float|null
*
* #ORM\Column(name="ville_longitude_deg", type="float", precision=10, scale=0, nullable=true)
*/
private $ville_longitude_deg;
/**
* #var float|null
*
* #ORM\Column(name="ville_latitude_deg", type="float", precision=10, scale=0, nullable=true)
*/
private $ville_latitude_deg;
/**
* #var string|null
*
* #ORM\Column(name="ville_longitude_grd", type="string", length=9, nullable=true)
*/
private $ville_longitude_grd;
/**
* #var string|null
*
* #ORM\Column(name="ville_latitude_grd", type="string", length=8, nullable=true)
*/
private $ville_latitude_grd;
/**
* #var string|null
*
* #ORM\Column(name="ville_longitude_dms", type="string", length=9, nullable=true)
*/
private $ville_longitude_dms;
/**
* #var string|null
*
* #ORM\Column(name="ville_latitude_dms", type="string", length=8, nullable=true)
*/
private $ville_latitude_dms;
/**
* #var int|null
*
* #ORM\Column(name="ville_zmin", type="integer", nullable=true)
*/
private $ville_zmin;
/**
* #var int|null
*
* #ORM\Column(name="ville_zmax", type="integer", nullable=true)
*/
private $ville_zmax;
Mairie
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Mairie
*
* #ORM\Table(name="mairie", uniqueConstraints={#ORM\UniqueConstraint(name="UNIQ_3946A254A73F0036", columns={"ville_id"})}, indexes={#ORM\Index(name="IDX_3946A254CF94313", columns={"office_tourisme_id"})})
* #ORM\Entity
*/
class Mairie
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string|null
*
* #ORM\Column(name="mairie_nom_touristique", type="string", length=255, nullable=true)
*/
private $mairieNomTouristique;
/**
* #var string|null
*
* #ORM\Column(name="mairie_descriptif_1", type="string", length=255, nullable=true)
*/
private $mairieDescriptif1;
/**
* #var string|null
*
* #ORM\Column(name="mairie_descriptif_2", type="string", length=255, nullable=true)
*/
private $mairieDescriptif2;
/**
* #var string|null
*
* #ORM\Column(name="mairie_epci_rattachement", type="string", length=255, nullable=true)
*/
private $mairieEpciRattachement;
/**
* #var string
*
* #ORM\Column(name="mairie_maire_nom", type="string", length=255, nullable=false)
*/
private $mairieMaireNom;
/**
* #var string
*
* #ORM\Column(name="mairie_maire_prenom", type="string", length=255, nullable=false)
*/
private $mairieMairePrenom;
/**
* #var string|null
*
* #ORM\Column(name="mairie_adjoint_nom", type="string", length=255, nullable=true)
*/
private $mairieAdjointNom;
/**
* #var string|null
*
* #ORM\Column(name="mairie_adjoint_prenom", type="string", length=255, nullable=true)
*/
private $mairieAdjointPrenom;
/**
* #var string|null
*
* #ORM\Column(name="mairie_contact_nom", type="string", length=255, nullable=true)
*/
private $mairieContactNom;
/**
* #var string|null
*
* #ORM\Column(name="mairie_contact_prenom", type="string", length=255, nullable=true)
*/
private $mairieContactPrenom;
/**
* #var int
*
* #ORM\Column(name="mairie_telephone_contact", type="integer", nullable=false)
*/
private $mairieTelephoneContact;
/**
* #var string
*
* #ORM\Column(name="mairie_email_contact", type="string", length=255, nullable=false)
*/
private $mairieEmailContact;
/**
* #var string
*
* #ORM\Column(name="mairie_latitude", type="string", length=255, nullable=false)
*/
private $mairieLatitude;
/**
* #var string
*
* #ORM\Column(name="mairie_longitude", type="string", length=255, nullable=false)
*/
private $mairieLongitude;
/**
* #var string|null
*
* #ORM\Column(name="mairie_photo_1", type="string", length=255, nullable=true)
*/
private $mairiePhoto1;
/**
* #var string|null
*
* #ORM\Column(name="mairie_photo_2", type="string", length=255, nullable=true)
*/
private $mairiePhoto2;
/**
* #var string|null
*
* #ORM\Column(name="mairie_photo_3", type="string", length=255, nullable=true)
*/
private $mairiePhoto3;
/**
* #var string|null
*
* #ORM\Column(name="mairie_photo_4", type="string", length=255, nullable=true)
*/
private $mairiePhoto4;
/**
* #var string|null
*
* #ORM\Column(name="mairie_taxe_sejour_gestionnaire", type="string", length=255, nullable=true)
*/
private $mairieTaxeSejourGestionnaire;
/**
* #var string|null
*
* #ORM\Column(name="mairie_taxe_sejour_bareme", type="string", length=255, nullable=true)
*/
private $mairieTaxeSejourBareme;
/**
* #var string|null
*
* #ORM\Column(name="mairie_sejour_lien", type="string", length=255, nullable=true)
*/
private $mairieSejourLien;
/**
* #var string|null
*
* #ORM\Column(name="mairie_contact_nom_prenom", type="string", length=255, nullable=true)
*/
private $mairieContactNomPrenom;
/**
* #var string|null
*
* #ORM\Column(name="mairie_de_telephone", type="text", length=255, nullable=true)
*/
private $mairieDeTelephone;
/**
* #var string|null
*
* #ORM\Column(name="mairie_sejour_email", type="string", length=255, nullable=true)
*/
private $mairieSejourEmail;
/**
* #var string|null
*
* #ORM\Column(name="mairie_rappel_texte", type="string", length=255, nullable=true)
*/
private $mairieRappelTexte;
/**
* #var string|null
*
* #ORM\Column(name="mairie_rappel_lien", type="string", length=255, nullable=true)
*/
private $mairieRappelLien;
/**
* #var string|null
*
* #ORM\Column(name="mairie_logo", type="string", length=255, nullable=true)
*/
private $mairieLogo;
/**
* #var string|null
*
* #ORM\Column(name="mairie_logo_2", type="string", length=255, nullable=true)
*/
private $mairieLogo2;
/**
* #var \DateTime
*
* #ORM\Column(name="mairie_date_inscription", type="datetime", nullable=false)
*/
private $mairieDateInscription;
/**
* #var string|null
*
* #ORM\Column(name="mairie_tampon", type="string", length=255, nullable=true)
*/
private $mairieTampon;
/**
* #var string|null
*
* #ORM\Column(name="mairie_maire_signature", type="string", length=255, nullable=true)
*/
private $mairieMaireSignature;
/**
* #var string
*
* #ORM\Column(name="mairie_slug", type="string", length=255, nullable=false)
*/
private $mairieSlug;
/**
* #var string
*
* #ORM\Column(name="insee", type="string", length=255, nullable=false)
*/
private $insee;
/**
* #var string
*
* #ORM\Column(name="mairie_adresse", type="string", length=255, nullable=false)
*/
private $mairieAdresse;
/**
* #var string|null
*
* #ORM\Column(name="mairie_complement_adresse", type="string", length=255, nullable=true)
*/
private $mairieComplementAdresse;
/**
* #var string
*
* #ORM\Column(name="mairie_postal_code", type="string", length=10, nullable=false)
*/
private $mairiePostalCode;
/**
* #var string
*
* #ORM\Column(name="mairie_commune", type="string", length=255, nullable=false)
*/
private $mairieCommune;
/**
* #var \Villes
*
* #ORM\ManyToOne(targetEntity="Villes")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="ville_id", referencedColumnName="id")
* })
*/
private $ville;
/**
* #var \OfficeTourisme
*
* #ORM\ManyToOne(targetEntity="OfficeTourisme")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="office_tourisme_id", referencedColumnName="id")
* })
*/
private $officeTourisme;
public function __construct()
{
$this->user_id_heb = new ArrayCollection();
$this->hebergements = new ArrayCollection();
$this->mairie_id_user = new ArrayCollection();
$this->user = new ArrayCollection();
$this->hebergement = new ArrayCollection();
}
public function getVilles(): ?Villes
{
return $this->ville;
}
public function setVilles(?Villes $villes): self
{
$this->villes = $villes;
return $this;
}
In Mairie :
The function setVilles
public function setVilles(?Villes $villes): self
{
$this->villes = $villes;
return $this;
}
Should'nt it be :
$this->ville = $ville;
Because the property "Villes" with an S doesn't seem to exist.
So the function should be :
public function setVille(?Ville $ville): self
{
$this->ville = $ville;
return $this;
}
Your setVilles() function is seting $this->villes rather than $this->ville
I'm facing a big problem with Doctrine when i'm requesting a table with a composite primary key referencing foreign keys.
I have a 3 entities:
Library(idLibrary,adress), Book(idBook,title,pageCount), Container(idLibrary,idBook, quantity).
Everything is generated without any errors but when i'm doing findAll() request on my Container repository my browser freezes and nothing is showed like it was stuck in a loop (there 3 rows in my Container table).
class Bibliotheque
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="name", type="string", length=60)
*/
private $name;
/**
* #ORM\Column(name="adress", type="string", length=60)
*/
private $adress;
}
class Conteneur
{
/**
* Many Containers have One library.
* #ORM\Id
* #ORM\OneToOne(targetEntity="Bibliotheque")
* #ORM\JoinColumn(name="libraryId", referencedColumnName="id")
*/
private $library;
/**
* One Container has One book.
* #ORM\Id
* #ORM\OneToOne(targetEntity="Livre")
* #ORM\JoinColumn(name="bookId", referencedColumnName="id")
*/
private $book;
/**
* #ORM\Column(name="quantity", type="integer")
*/
private $quantity;
}
class Livre
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #ORM\Column(name="pageCount", type="integer")
*/
private $pageCount;
/**
* Un livre a plusieurs auteurs
* #ORM\ManyToMany(targetEntity="Auteur")
* #ORM\JoinTable(name="books_authors",
* joinColumns={#ORM\JoinColumn(name="book_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="author_id", referencedColumnName="id")}
* )
*/
private $authors;
}
class Auteur
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="name", type="string", length=60)
*/
private $name;
/**
* #ORM\Column(name="surname", type="string", length=60)
*/
private $prenom;
/**
* #ORM\Column(name="birthDate", type="date")
*/
private $birthDate;
}
firstly, please use only French or English for class name and variable.
Add id inside class "Container" and remove annotation #ORM\id for attributes "library" and "book"
Define attribute "authors" with ArrayCollection and add method getAuthors(), addAuthor(Author $author), removeAuthor(Author $author)
And rename the tables name => "name_id" not "nameId"
look documention http://symfony.com/doc/current/doctrine.html
class Library
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $name
*
* #ORM\Column(name="name", type="string", length=60)
*/
private $name;
/**
* #var string $address
*
* #ORM\Column(name="address", type="string", length=60)
*/
private $address;
}
class Container
{
/**
* #var integer $id
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Many Containers have One library.
*
* #ORM\OneToOne(targetEntity="Library")
* #ORM\JoinColumn(name="library_id", referencedColumnName="id")
*/
private $library;
/**
* One Container has One book.
*
* #ORM\OneToOne(targetEntity="Book")
* #ORM\JoinColumn(name="book_id", referencedColumnName="id")
*/
private $book;
/**
* #ORM\Column(name="quantity", type="integer")
*/
private $quantity;
}
class Book
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $title
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string $pageCount
*
* #ORM\Column(name="page_count", type="integer")
*/
private $pageCount;
/**
* A book have many authors
*
* #var ArrayCollection $authors
*
* #ORM\ManyToMany(targetEntity="Auteur")
* #ORM\JoinTable(name="books_authors",
* joinColumns={#ORM\JoinColumn(name="book_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="author_id", referencedColumnName="id")}
* )
*/
private $authors;
/**
* #return ArrayCollection
*/
public function getAuthors()
{
return $this->authors;
}
/**
* #param Author $author
*
* #return $this
*/
public function addAuthor(Author $author)
{
$this->authors->add($author);
return $this;
}
/**
* #param Author $author
*
* #return $this
*/
public function removeAuthor(Author $author)
{
$this->authors->removeElement($author);
return $this;
}
}
class Author
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $firstname
*
* #ORM\Column(name="firstname", type="string", length=60)
*/
private $firstname;
/**
* #var string $lastname
*
* #ORM\Column(name="lastname", type="string", length=60)
*/
private $lastname;
/**
* #var DateTime $birthDate
*
* #ORM\Column(name="birthDate", type="date")
*/
private $birthDate;
}
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();
Does anyone have a clue why in the world I would be getting 20 minute page load times in dev in Symfony2? It just randomly happens to me. One day I will get fast load times, the next day I am twiddling my thumbs waiting for a page to load. What can I check/disable/enable/etc? Thanks!
Here is my latest page load:
Time: 298068 ms
Here is the Entity for PurchaseOrder
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="archived_po_number", type="string", length=50, nullable=true)
* #Common\Versioned
*/
private $archived_po_number;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=50, nullable=true)
* #Common\Versioned
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="show_locations", type="string", length=1, nullable=true)
* #Common\Versioned
*/
private $show_locations;
/**
* #var integer
*
* #ORM\Column(name="freight", type="decimal", precision=10, scale=2, nullable=true)
* #Common\Versioned
*/
private $freight;
/**
* #var integer
*
* #ORM\Column(name="pallets", type="integer", nullable=true)
* #Common\Versioned
*/
private $pallets;
/**
* #var integer
*
* #ORM\Column(name="boxes", type="integer", nullable=true)
* #Common\Versioned
*/
private $boxes;
/**
* #var text
* #ORM\Column(name="internal_notes", type="text", nullable=true)
* #Common\Versioned
*/
private $internal_notes;
/**
* #var text
* #ORM\Column(name="sales_rep_notes", type="text", nullable=true)
* #Common\Versioned
*/
private $sales_rep_notes;
/**
* #var string
*
* #ORM\Column(name="custom_purchase_order_number", type="string", length=255, nullable=true)
* #Common\Versioned
*/
private $custom_purchase_order_number;
/**
* #ORM\ManyToOne(targetEntity="WIC\CommonBundle\Entity\CustomOptions", inversedBy="purchaseOrders")
* #ORM\JoinColumn(name="purchase_order_class", referencedColumnName="id", nullable=true)
* #Common\Versioned
*/
private $purchase_order_class;
/**
* #ORM\ManyToOne(targetEntity="WIC\WarehouseBundle\Entity\Warehouse", inversedBy="purchaseOrders")
* #ORM\JoinColumn(name="generated_by_location", referencedColumnName="id", nullable=true)
* #Common\Versioned
*/
private $generated_by_location;
/**
* #ORM\ManyToOne(targetEntity="WIC\InventoryLocationBundle\Entity\InventoryLocation", inversedBy="purchaseOrders")
* #ORM\JoinColumn(name="receive_location", referencedColumnName="id", nullable=true)
* #Common\Versioned
*/
private $receive_location;
/**
* #ORM\ManyToOne(targetEntity="WIC\WarehouseBundle\Entity\Warehouse", inversedBy="purchaseOrders")
* #ORM\JoinColumn(name="company_name", referencedColumnName="id", nullable=true)
* #Common\Versioned
*/
private $company_name;
/**
* #ORM\ManyToOne(targetEntity="WIC\WarehouseBundle\Entity\Warehouse", inversedBy="purchaseOrders")
* #ORM\JoinColumn(name="ship_to", referencedColumnName="id", nullable=true)
* #Common\Versioned
*/
private $ship_to;
/**
* #ORM\ManyToOne(targetEntity="WIC\WarehouseBundle\Entity\Warehouse", inversedBy="purchaseOrders")
* #ORM\JoinColumn(name="bill_to", referencedColumnName="id", nullable=true)
* #Common\Versioned
*/
private $bill_to;
/**
* #ORM\ManyToOne(targetEntity="WIC\CommonBundle\Entity\CustomOptions", inversedBy="purchaseOrders")
* #ORM\JoinColumn(name="payment_terms", referencedColumnName="id", nullable=true)
* #Common\Versioned
*/
private $payment_terms;
/**
* #ORM\ManyToOne(targetEntity="WIC\CommonBundle\Entity\CustomOptions", inversedBy="purchaseOrders")
* #ORM\JoinColumn(name="fob", referencedColumnName="id", nullable=true)
* #Common\Versioned
*/
private $fob;
/**
* #ORM\ManyToOne(targetEntity="WIC\CommonBundle\Entity\CustomOptions", inversedBy="purchaseOrders")
* #ORM\JoinColumn(name="ship_via", referencedColumnName="id", nullable=true)
* #Common\Versioned
*/
private $ship_via;
/**
* #var datetime
*
* #ORM\Column(name="ship_by", type="datetime", nullable=true)
* #Common\Versioned
*/
private $ship_by;
/**
* #var datetime
*
* #ORM\Column(name="cancel_by", type="datetime", nullable=true)
* #Common\Versioned
*/
private $cancel_by;
/**
* #var datetime
*
* #ORM\Column(name="due_by", type="datetime", nullable=true)
* #Common\Versioned
*/
private $due_by;
/**
* #var string
*
* #ORM\Column(name="nbt", type="string", length=2, nullable=true)
* #Common\Versioned
*/
private $nbt;
/**
* #ORM\OneToMany(targetEntity="WIC\PurchaseOrderLineItemBundle\Entity\PurchaseOrderLineItem", mappedBy="purchaseOrder", cascade={"remove"}, fetch="EAGER")
*/
protected $purchaseOrderLineItem;
/**
* #ORM\OneToMany(targetEntity="WIC\PurchaseOrderBundle\Entity\PurchaseOrderCharge", mappedBy="purchaseOrder", cascade={"remove"}, fetch="EAGER")
*/
protected $purchaseOrderCharge;
/**
* #ORM\OneToMany(targetEntity="WIC\PurchaseOrderBundle\Entity\PurchaseOrderPayment", mappedBy="purchaseOrder", cascade={"remove"}, fetch="EAGER")
*/
protected $purchaseOrderPayment;
/**
* #ORM\ManyToOne(targetEntity="WIC\SupplierBundle\Entity\Supplier", inversedBy="purchaseOrders")
* #ORM\JoinColumn(name="supplier_id", referencedColumnName="id", nullable=true)
* #Common\Versioned
*/
protected $supplier;
/**
* #ORM\ManyToOne(targetEntity="WIC\CommonBundle\Entity\CustomOptions", inversedBy="purchaseOrders")
* #ORM\JoinColumn(name="status_id", referencedColumnName="id", nullable=true)
* #Common\Versioned
*/
private $status;
/**
* #ORM\ManyToOne(targetEntity="WIC\UserBundle\Entity\User")
* #ORM\JoinColumn(name="created_by", referencedColumnName="id")
* #Common\Blameable(on="create")
*/
private $createdBy;
/**
* #ORM\ManyToOne(targetEntity="WIC\UserBundle\Entity\User")
* #ORM\JoinColumn(name="updated_by", referencedColumnName="id")
* #Common\Blameable(on="update")
*/
private $updatedBy;
/**
* #ORM\ManyToOne(targetEntity="WIC\AccountBundle\Entity\Account", inversedBy="purchaseOrders")
* #ORM\JoinColumn(name="account_id", referencedColumnName="id", nullable=false)
* #Common\Versioned
* #Common\Blameable(on="create")
*/
protected $account;
/**
* #var datetime $created
*
* #Common\Timestampable(on="create")
* #ORM\Column(type="datetime")
*/
private $created;
/**
* #var datetime $updated
*
* #Common\Timestampable(on="update")
* #ORM\Column(type="datetime", nullable=true)
*/
private $updated;
/**
* #ORM\Column(name="deletedAt", type="datetime", nullable=true)
*/
private $deletedAt;
Here is my controller method listAction()
// verify access
if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) {
$classIdentity = new ObjectIdentity('class', 'WIC\\PurchaseOrderBundle\\Entity\\PurchaseOrder');
if (false === $this->get('security.context')->isGranted('VIEW', $classIdentity)) {
throw new AccessDeniedException('Only an admin user has access to this section...');
}
}
// get user's account
$account = $this->getUser()->getAccount();
$search_form = $this->createForm(new PurchaseOrderSearchType());
$em = $this->getDoctrine()->getManager();
if ($request->isMethod('POST')) {
$search_form->bind($request);
// if ($search_form->isValid()) {
$data = $search_form->getData();
$purchaseOrders = $em->getRepository('WICPurchaseOrderBundle:PurchaseOrder')->getListBy($data);
// }
} else {
if($this->get('request')->query->get('letter')){
}else{
}
// Set the up the pagination statement...
$em = $this->getDoctrine()->getManager();
$dql = "SELECT p FROM WIC\PurchaseOrderBundle\Entity\PurchaseOrder p WHERE p.account=:account_id ORDER BY p.created desc";
$query = $em->createQuery($dql);
$query->setParameters(array(
'account_id' => $account->getId(),
));
$paginator = $this->get('knp_paginator');
$paginatorObject = $paginator->paginate(
$query,
$this->get('request')->query->get('page', 1),25
);
// $purchaseOrders = $em->getRepository('WICPurchaseOrderBundle:PurchaseOrder')->findByAccount($account->getId(),array('id' => 'DESC'));
}
return array(
'heading' => 'Purchase Order',
'sidebarLeftTitle'=>'Purchase Order Menu',
'purchaseOrders' => $paginatorObject,
'search_form' => $search_form->createView(),
);
By default in app_dev.php you have the profiler bar, click on the timer and you will have a good start to debug (timeline with main/sub request, etc)
The issue was in the OneToMany relationship code where it says fetch="EAGER". This was pulling in all sorts of unnecessary associations and db queries. When I removed it, the db queries went down to 4 per page instead of 4000.
Are you using x-debug or any PHP level profiling? You've either got some form of recursion happening or one of the steps in the security\http\firewall process is taking a very long time to complete.
My suggestion would be
Look at your stack-trace and find anywhere there are highly repeated events around the security\http\firewall code and investigate if they exist
Quickly step through your code using x-debug, and find where the delay is occurring. With a few page reloads you should be able to step into the section which is causing the long page load times.
If you are using profiling, make sure your created files aren't ridiculously verbose. I've seen this long page load issue happen before due to profiling, where each page load would create a 20Gb+ profiling file.
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