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.
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();
}
}
I'm working on Symfony 2.7. I have to create a mail client to retrieve and send mails from the mail server (IMAP protocol); to do this, I used the php-imap classes, with a bundle (included with composer). But I'm not sure about the way I should use them : Do I extend the classes to represent my Mail and Mailbox objects, or should I create new classes from scratch ?
I don't want to manipulate IMAP straight from my controllers, I think it would be too long to process. Is that right?
Is it a good idea to create a "watcher" (periodic command executed by cron) to parse new mails every 2 minutes or so, create new mails entity from them, and send the waiting ones?
Could I do that while extending the php-imap classes? This way I would use one class only? But wouldn't that be too heavy to store for the database ?
What's the correct way to fetch only new mails ? Do I have to use a specific function, like imap_check, or do I do that by a search criteria (like date from the last check) ? I tried with criteria "NEW", but that was unsuccessful.
Also, the mailboxes I have to parse are quite heavy. I tried to make a search in one of them with "ALL" criteria, but it's really long to process ! Am I doing it right ? Do I just have to be patient ?
Here's what I did for the "watcher" function :
use PhpImap\Mailbox as ImapMailbox;
class GetNewMailsCommand extends ContainerAwareCommand
{
$em = $this->getContainer()->get('doctrine')->getEntityManager();
$mailboxes = $em->getRepository('MIPMailBundle:MailBox')->findAllActive();
foreach ($mailboxes as $mailbox){
$imapBox = new ImapMailbox('{'.$mailbox->getServer().':143/notls/norsh/novalidate-cert}INBOX', $mailbox->getAdress(), $mailbox->getPassword());
if ($mailbox->getMails() == null || empty($mailbox->getMails())){
$mailsIds = $imapBox->searchMailbox('ALL');
if(!$mailsIds) {
$output->writeln($mailbox->getAdress() . " is empty");
}
} else {
$mailsIds = $imapBox->searchMailbox('NEW');
if(!$mailsIds) {
$output->writeln("No new mail for " . $mailbox->getAdress());
}
}
foreach ($mailsIds as $mailId){
$imapMail = $imapBox->getMail($mailId);
$mail = new Mail($mailbox, false);
$mail->setSubject($imapMail->subject);
$mail->setSender($imapMail->fromAddress);
$mail->setCc($imapMail->cc);
$mail->setBcc($imapMail->bcc);
$mail->setToString($imapMail->toString);
$mail->setContent($imapMail->textPlain);
$mail->setDate(new \DateTime($imapMail->date));
foreach ($imapMail->to as $toAddress){
$mail->addRecipient($toAddress);
}
$em->persist($mail);
}
}
$em->flush();
And here's my entities :
class MailBox
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var
*
* #ORM\OneToOne(targetEntity="\MIP\CRMBundle\Entity\Agency", inversedBy="mailBox", cascade={"persist"})
* #ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
*/
private $user;
/**
* #var Agency
*
* #ORM\ManyToMany(targetEntity="\MIP\CRMBundle\Entity\Agency", inversedBy="mailBoxShared")
* #ORM\JoinTable(name="mailbox_shared")
*/
private $sharedTo;
/**
* #var
*
* #ORM\OneToMany(targetEntity="Mail", mappedBy="mailBox")
*/
private $mails;
/**
* #var boolean
*
* #ORM\Column(name="active", type="boolean")
*/
private $active;
/**
* #var string
*
* #ORM\Column(name="adress", type="string", length=255)
*/
private $adress;
/**
* #var string
*
* #ORM\Column(name="server", type="string", length=255)
*/
private $server;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* #var integer
*
* #ORM\Column(name="port", type="integer")
*/
private $port;
/**
* MailBox constructor.
*/
public function __construct()
{
$this->sharedTo = new ArrayCollection();
$this->mails = new ArrayCollection();
}
class Mail
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="sender", type="string", length=255)
*/
private $sender;
/**
* #var array
*
* #ORM\Column(name="recipients", type="json_array", nullable=true)
*/
private $recipients;
/**
* #var string
*
* #ORM\Column(name="toString", type="string", nullable=true)
*/
private $toString;
/**
* #var array
*
* #ORM\Column(name="cc", type="json_array", nullable=true)
*/
private $cc;
/**
* #var array
*
* #ORM\Column(name="bcc", type="json_array", nullable=true)
*/
private $bcc;
/**
* #var string
*
* #ORM\Column(name="subject", type="string", length=255, nullable=true)
*/
private $subject;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* #var string
*
* #ORM\Column(name="content", type="text", nullable=true)
*/
private $content;
/**
* #var
*
* #ORM\OneToMany(targetEntity="MIP\CRMBundle\Entity\File", mappedBy="mail", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="file_id", referencedColumnName="id", nullable=true)
*/
protected $files;
/**
* #var ArrayCollection
*/
private $attached;
/**
* #var MailBox
* #ORM\ManyToOne(targetEntity="MailBox", inversedBy="mails")
* #ORM\JoinColumn(name="mailBox_id", referencedColumnName="id")
*/
private $mailBox;
/**
* #var LabelSticker
*
* #ORM\ManyToMany(targetEntity="\MIP\MailBundle\Entity\LabelSticker", mappedBy="mails")
*/
private $labels;
/**
* #var boolean
*/
private $readed;
/**
* #var boolean
*/
private $sent;
/**
* Constructor
* #param MailBox $mailbox
* #param boolean $readed
*/
public function __construct($mailbox, $readed)
{
$this->files = new ArrayCollection();
$this->date = new \DateTime('now');
$this->mailBox = $mailbox;
$this->readed = $readed;
}
Thanks for your help !
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 have a easy form with a data transformer, it works correctly (update, persist and delete) but I get a error on twig.
Impossible to invoke a method ("trans") on a NULL variable ("") in SonataDoctrineORMAdminBundle:CRUD:edit_orm_one_to_many.html.twig at line 30
error is in this line:
{{ nested_field.vars['sonata_admin'].admin.trans(nested_field.vars.label) }}
All fields have value in nested_field.vars['sonata_admin'] less my custom field
My code is this:
$formMapper
->add(
$formMapper->create('articleAmount', 'text')
->addModelTransformer($articleAmountToStringTransformer)
)
...
Entities
/**
* AppShopHasArticles
*
* #ORM\Table(name="app_shop_has_articles")
* #ExclusionPolicy("all")
* #ORM\Entity(repositoryClass="Nvia\ShopAppBundle\Entity\Repository\AppShopHasArticlesRepository")
* #ORM\HasLifecycleCallbacks()
*/
class AppShopHasArticles
{
/**
* #var \Nvia\CommonBundle\Entity\Article
*
* #ORM\ManyToOne(targetEntity="Nvia\ShopAppBundle\Entity\Article", inversedBy="appShopHasArticles")
* #ORM\JoinColumn(name="article_id", referencedColumnName="id", nullable=false)
* #ORM\Id
* #Expose
*/
private $article;
/**
* #var \Nvia\CommonBundle\Entity\Country
*
* #ORM\ManyToOne(targetEntity="Nvia\CommonBundle\Entity\Country")
* #ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=false)
* #ORM\Id
* #Expose
*/
private $country;
/**
* #var \Nvia\ShopAppBundle\Entity\AppShop
*
* #ORM\ManyToOne(targetEntity="Nvia\ShopAppBundle\Entity\ArticleAmount")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="country_id", referencedColumnName="country_id", nullable=false),
* #ORM\JoinColumn(name="article_id", referencedColumnName="article_id", nullable=false)
* })
* #Expose
*/
private $articleAmount;
...
}
/**
* ArticleAmount
*
* #ORM\Table(name="article_amount")
*/
class ArticleAmount
{
/**
* #var \Nvia\CommonBundle\Entity\Article
*
* #ORM\ManyToOne(targetEntity="Nvia\ShopAppBundle\Entity\Article", inversedBy="articleAmounts")
* #ORM\JoinColumn(name="article_id", referencedColumnName="id", nullable=false)
* #ORM\Id
*/
private $article;
/**
* #var \Nvia\CommonBundle\Entity\Country
*
* #ORM\ManyToOne(targetEntity="Nvia\CommonBundle\Entity\Country")
* #ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=false)
* #ORM\Id
*/
private $country;
/**
* #var float
*
* #ORM\Column(name="amount", type="float", precision=10, scale=0, nullable=false)
* #Expose
*/
private $amount;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt;
}
What am I doing wrong :/ ?
When using the create method of the formMapper sonata will not insert it's own logic.
Instead try something like this
$formMapper
->add('articleAmount', 'text')
->get('articleAmount')
->addModelTransformer($articleAmountToStringTransformer)
Tested on Sonata 2.4