Symfony elastica mapping with relationsip causing OutOfMemoryException while populating the index - php

I wanted to index some data from a associated entity of the main entity which I already have an index for. My mapping rules are as below and I have given the 2 entities below too. My problem is I am getting OutOfMemoryException while trying to populate the index. bin/console fos:elastica:populate --index=sales_rule
Index configuration
sales_rule:
client: default
use_alias: true
types:
sales_rule:
mappings:
id: {type: integer}
name:
description:
salesCoupons:
type: nested
properties:
code:
persistence:
driver: orm
model: MyProject\MyBundle\Entity\SalesRule
provider:
batch_size: 100
listener:
Entities
/**
* #ORM\Table(name="sales_rule")
* #ORM\Entity(repositoryClass="MyProject\MyBundle\Repository\SalesRuleRepository")
*/
class SalesRule
{
/**
* #var int
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
private $id;
/**
* #var string
*
* #ORM\Column(type="string")
*/
private $name;
/**
* #var string
*
* #ORM\Column(type="text")
*/
private $description;
/**
* #var int
*
* #ORM\Column(type="integer", nullable=true)
*/
private $usageLimit;
/**
* #var SalesCoupon[]|ArrayCollection
*
* #ORM\OneToMany(targetEntity="SalesCoupon", mappedBy="salesRule")
*/
private $salesCoupons;
public function __construct()
{
$this->salesCoupons = new ArrayCollection();
}
/**
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* #return int
*/
public function getUsageLimit()
{
return $this->usageLimit;
}
/**
* #return int
*/
public function getUsagePerCustomer()
{
return $this->usagePerCustomer;
}
/**
* #param string $description
* #return SalesRule
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* #param string $name
* #return SalesRule
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* #param int $usageLimit
* #return SalesRule
*/
public function setUsageLimit($usageLimit)
{
$this->usageLimit = $usageLimit;
return $this;
}
/**
* #return SalesCoupon[]|ArrayCollection
*/
public function getSalesCoupons()
{
return $this->salesCoupons;
}
/**
* #param ArrayCollection $salesCoupons
*/
public function setSalesCoupons(ArrayCollection $salesCoupons)
{
$this->salesCoupons = $salesCoupons;
}
}
/**
* #ORM\Table(name="sales_coupon")
* #ORM\Entity(repositoryClass="MyProject\MyBundle\Repository\SalesCouponRepository")
*/
class SalesCoupon
{
/**
* #var int
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
private $id;
/**
* #var SalesRule
*
* #Serializer\Exclude
*
* #ORM\ManyToOne(targetEntity="SalesRule", inversedBy="salesCoupons")
* #ORM\JoinColumn(nullable=false)
*/
private $salesRule;
/**
* #var \DateTime
*
* #ORM\Column(type="datetime", nullable=true)
*/
private $fromDate;
/**
* #var \DateTime
*
* #ORM\Column(type="datetime", nullable=true)
*/
private $toDate;
/**
* #var string
*
* #ORM\Column(nullable=true)
*/
private $code;
/**
* #var int
*
* #Assert\Range(min=0)
*
* #ORM\Column(type="integer", nullable=true)
*/
private $usageLimit;
public function __construct()
{
$this->salesCouponCustomers = new ArrayCollection();
}
/**
* #return string
*/
public function getCode()
{
return $this->code;
}
/**
* #return \DateTime
*/
public function getFromDate()
{
return $this->fromDate;
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #return \Unity\CmsBundle\Entity\SalesRule
*/
public function getSalesRule()
{
return $this->salesRule;
}
/**
* #return \DateTime
*/
public function getToDate()
{
return $this->toDate;
}
/**
* #param string $code
* #return SalesCoupon
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* #param \DateTime $fromDate
* #return SalesCoupon
*/
public function setFromDate($fromDate)
{
$this->fromDate = $fromDate;
return $this;
}
/**
* #param \Unity\CmsBundle\Entity\SalesRule $salesRule
* #return SalesCoupon
*/
public function setSalesRule(SalesRule $salesRule)
{
$this->salesRule = $salesRule;
return $this;
}
/**
* #param \DateTime $toDate
* #return SalesCoupon
*/
public function setToDate($toDate)
{
$this->toDate = $toDate;
return $this;
}
}

Can you try set limit memory unlimit.
php -d memory_limit=-1 bin/console fos:elastica:populate --index=sales_rule

Related

How annotation #groups for the sluggable column

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;
}

getClientOriginalName() undefined method

Im working in a symfony3 project where i have a onetomany relation to upload multiple files with VICH UPLOADER, and save each file as separated entity.
First entity is "Message", and has a onetomany relation with "MessageAttachment".
Message entity
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var CourseEdition
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\CourseEdition")
* #ORM\JoinColumn(name="edition_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private $edition;
/**
* #var Message
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Message", inversedBy="childs", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
private $parent;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\User", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private $user;
/**
* #var string
*
* #ORM\Column(name="subject", type="string", length=255)
*/
private $subject;
/**
* #var string
*
* #ORM\Column(name="body", type="text")
*/
private $body;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime")
* #Gedmo\Timestampable(on="create")
*/
private $createdAt;
/**
* #var ArrayCollection
*
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Message", mappedBy="parent", orphanRemoval=true)
*/
private $childs;
/**
* #var ArrayCollection
*
* #ORM\OneToMany(targetEntity="AppBundle\Entity\MessageAttachment", mappedBy="message", orphanRemoval=true, cascade={"persist", "remove"})
* #Assert\Valid()
*/
private $attachments;
/**
* #var ArrayCollection
*
* #ORM\OneToMany(targetEntity="AppBundle\Entity\MessageUser", mappedBy="message", orphanRemoval=true, cascade={"persist"})
*/
private $recipients;
public function __construct()
{
$this->childs = new ArrayCollection();
$this->attachments = new ArrayCollection();
$this->recipients = new ArrayCollection();
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #param CourseEdition $edition
*
* #return Message
*/
public function setEdition($edition)
{
$this->edition = $edition;
return $this;
}
/**
* #return CourseEdition
*/
public function getEdition()
{
return $this->edition;
}
/**
* #param Message $parent
*
* #return Message
*/
public function setParent($parent)
{
$this->parent = $parent;
return $this;
}
/**
* #return Message
*/
public function getParent()
{
return $this->parent;
}
/**
* #param User $user
*
* #return Message
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* #return User
*/
public function getUser()
{
return $this->user;
}
/**
* #param string $subject
*
* #return Message
*/
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* #return string
*/
public function getSubject()
{
return $this->subject;
}
/**
* #param string $body
*
* #return Message
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* #return string
*/
public function getBody()
{
return $this->body;
}
/**
* #param \DateTime $createdAt
*
* #return Message
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* #param MessageAttachment $attachment
*
* #return Message
*/
public function addAttachment($attachment)
{
$this->attachments->add($attachment);
$attachment->setMessage($this);
return $this;
}
/**
* #param MessageAttachment $attachment
*
* #return Message
*/
public function removeAttachment($attachment)
{
$this->attachments->removeElement($attachment);
$attachment->setMessage(null);
return $this;
}
/**
* #return \Doctrine\Common\Collections\Collection
*/
public function getAttachments()
{
return $this->attachments;
}
/**
* #param Message $message
*
* #return Message
*/
public function addChild($message)
{
$this->childs->add($message);
$message->setParent($this);
return $this;
}
/**
* #param Message $message
*
* #return Message
*/
public function removeChild($message)
{
$this->childs->removeElement($message);
$message->setParent(null);
return $this;
}
/**
* #return ArrayCollection
*/
public function getChilds()
{
return $this->childs;
}
/**
* #param NoticeUser $recipient
*
* #return Message
*/
public function addRecipient($recipient)
{
$this->recipients->add($recipient);
return $this;
}
/**
* #param NoticeUser $recipient
*
* #return Message
*/
public function removeRecipient($recipient)
{
$this->recipients->removeElement($recipient);
return $this;
}
/**
* #return ArrayCollection
*/
public function getRecipients()
{
return $this->recipients;
}
MessageAttachment Entity:
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var Message
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Message", inversedBy="attachments", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="message_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
private $message;
/**
* #var Filesys
*
* #Vich\UploadableField(mapping="doc_message_attach",fileNameProperty="name")
*/
private $file;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="realname", type="string", length=255)
*/
private $realname;
/**
* #var int
*
* #ORM\Column(name="size", type="integer")
*/
private $size;
/**
* #var string
*
* #ORM\Column(name="mime_type", type="string", length=100)
*/
private $mimeType;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime")
* #Gedmo\Timestampable(on="create")
*/
private $createdAt;
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #param Message $message
*
* #return MessageAttachment
*/
public function setMessage($message)
{
$this->message = $message;
return $this;
}
/**
* #return Message
*/
public function getMessage()
{
return $this->message;
}
/**
* #param Filesys|\Symfony\Component\HttpFoundation\File\UploadedFile $file
*
* #return MessageAttachment
*/
public function setFile($file = null)
{
$this->file = $file;
if ($file) {
$this->size = $file->getSize();
$this->mimeType = $file->getMimeType();
$this->realname = $file->getClientOriginalName();
}
return $this;
}
/**
* #return Filesys|null
*/
public function getFile()
{
return $this->file;
}
/**
* #param string $name
*
* #return MessageAttachment
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* #param integer $size
*
* #return MessageAttachment
*/
public function setSize($size)
{
$this->size = $size;
return $this;
}
/**
* #return int
*/
public function getSize()
{
return $this->size;
}
/**
* #param string $mimeType
*
* #return MessageAttachment
*/
public function setMimeType($mimeType)
{
$this->mimeType = $mimeType;
return $this;
}
/**
* #return string
*/
public function getMimeType()
{
return $this->mimeType;
}
/**
* #param \DateTime $createdAt
*
* #return MessageAttachment
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set realname
*
* #param string $realname
*
* #return MessageAttachment
*/
public function setRealname($realname)
{
$this->realname = $realname;
return $this;
}
/**
* Get realname
*
* #return string
*/
public function getRealname()
{
return $this->realname;
}
**As you can see, in setFile() in MessageAttachments, im trying to retrieve original file name, and save it. Vich uploader can do it with annotations, but im not working in php 7 !
Everything is working ok, filesize and mimetypes are being saved correctly, except saving original name.**
This is the error symfony shows me:
[Tue Oct 17 11:58:19.031167 2017] [:error] [pid 9076:tid 1816] [client 127.0.0.1:53731] PHP Fatal error: Call to undefined method Symfony\Component\HttpFoundation\File\File::getClientOriginalName() in C:\xampp\htdocs\mindway2\src\AppBundle\Entity\MessageAttachment.php on line 121, referer: http://mindway.dev/messages/new
Any idea? i cant find why!
The real filename needs to be shown, and i need it to rename files for downloading.
Finally, i have decided to do it in the way i dont like, in controller, before persist entities:
foreach($message->getAttachments() as $attachfile)
{
$attachfile->setRealname($attachfile->getFile()->getClientOriginalName());
}
It says it's looking in Symfony\Component\HttpFoundation\File\ File and it doesn't exist in that class --that's true. The method getClientOriginalName is in Symfony\Component\HttpFoundation\File\ UploadedFile.
Does your use statement say File or UploadedFile? If it's not UploadedFile, change it and see what happens.

Why my symfony entity is not managed?

I have a Portfolio entity which is working well to create or update but I can't delete it. Symfony throws this error:
Entity matthieu-appriou is not managed. An entity is managed if its
fetched from the database or registered as new through
EntityManager#persist
Here is my entity:
<?php
namespace CreasensoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use SensoBundle\Entity\Talent;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;
use JMS\Serializer\Annotation\MaxDepth;
use JMS\Serializer\Annotation\Exclude;
/**
* Portfolio
*
* #ORM\Entity
* #ORM\Table(name="portfolio")
* #ORM\Entity(repositoryClass="CreasensoBundle\Repository\PortfolioRepository")
*/
class Portfolio
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var bool
*
* #ORM\Column(name="visible", type="boolean", nullable=true)
*/
private $visible;
/**
* #Exclude
* #ORM\OneToOne(targetEntity="SensoBundle\Entity\Talent", cascade={"persist", "remove"}, inversedBy="portfolio")
* #ORM\JoinColumn(nullable=true, onDelete="SET NULL")
*/
private $talent;
/**
* #Exclude
* #ORM\OneToOne(targetEntity="SensoBundle\Entity\Image", cascade={"persist", "remove"})
* #ORM\JoinColumn(nullable=true, onDelete="SET NULL")
*/
private $image;
/**
* #var string
*
* #ORM\Column(name="folio_label", type="string", length=400, nullable=true)
* #Gedmo\Translatable
*/
private $folioLabel;
/**
* #var string
*
* #ORM\Column(name="main_customers", type="string", length=400, nullable=true)
*/
private $mainCustomers;
/**
* #var string
*
* #ORM\Column(name="main_agencies", type="string", length=400, nullable=true)
*/
private $mainAgencies;
/**
* #var string
*
* #ORM\Column(name="publications", type="string", length=700, nullable=true)
*/
private $publications;
/**
* #var string
*
* #ORM\Column(name="description_title", type="string", length=400, nullable=true)
* #Gedmo\Translatable
*/
private $descriptionTitle;
/**
* #var string
*
* #ORM\Column(name="description_content", type="text", nullable=true)
* #Gedmo\Translatable
*/
private $descriptionText;
/**
* #MaxDepth(2)
* #ORM\ManyToMany(targetEntity="SensoBundle\Entity\Expertise", cascade={"persist"})
*/
private $expertises;
/**
* #var \DateTime $created
*
* #Gedmo\Timestampable(on="create")
* #ORM\Column(type="datetime")
*/
private $created;
/**
* #var \DateTime $updated
*
* #Gedmo\Timestampable(on="update")
* #ORM\Column(type="datetime")
*/
private $updated;
/**
* #var \DateTime $updated
*
* #ORM\Column(type="datetime", nullable=true)
*/
private $mostRecentProjectDate;
/**
* #var \DateTime $featuredTime
* #ORM\Column(type="datetime", nullable=true)
*/
private $featuredTime;
/**
* #var \DateTime $submissionTime
* #ORM\Column(type="datetime", nullable=true)
*/
private $submissionTime;
/**
* #Gedmo\Locale
*/
protected $locale;
public function __construct()
{
$this->setVisible(false);
$this->expertises = new ArrayCollection();
}
public function __toString() {
return $this->getSlug();
}
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
public function getSlug()
{
return $this->getTalent()->getSlug();
}
/**
* Set visible
*
* #param boolean $visible
*
* #return Portfolio
*/
public function setVisible($visible)
{
$this->visible = $visible;
return $this;
}
/**
* Get visible
*
* #return bool
*/
public function getVisible()
{
return $this->visible;
}
/**
* #return mixed
*/
public function getTalent()
{
return $this->talent;
}
/**
* #param mixed $talent
*/
public function setTalent($talent)
{
$this->talent = $talent;
$talent->setPortfolio($this);
}
/**
* #return mixed
*/
public function getImage()
{
return $this->image;
}
/**
* #param mixed $image
*/
public function setImage($image)
{
if ($image) {
$image->setParentType('portfolio');
}
$this->image = $image;
}
/**
* #return string
*/
public function getMainCustomers()
{
return $this->mainCustomers;
}
/**
* #param string $mainCustomers
*/
public function setMainCustomers($mainCustomers)
{
$this->mainCustomers = $mainCustomers;
}
/**
* #return string
*/
public function getMainAgencies()
{
return $this->mainAgencies;
}
/**
* #param string $mainAgencies
*/
public function setMainAgencies($mainAgencies)
{
$this->mainAgencies = $mainAgencies;
}
/**
* #return string
*/
public function getDescriptionTitle()
{
return $this->descriptionTitle;
}
/**
* #param string $descriptionTitle
*/
public function setDescriptionTitle($descriptionTitle)
{
$this->descriptionTitle = $descriptionTitle;
}
/**
* #return string
*/
public function getDescriptionText()
{
return $this->descriptionText;
}
/**
* #param string $descriptionText
*/
public function setDescriptionText($descriptionText)
{
$this->descriptionText = $descriptionText;
}
public function addExpertise($expertise)
{
$this->expertises[] = $expertise;
return $this;
}
public function removeExpertise($expertise)
{
$this->expertises->removeElement($expertise);
}
public function getExpertises()
{
return $this->expertises;
}
public function setExpertises($expertises)
{
$this->expertises = $expertises;
}
/**
* #return mixed
*/
public function getCreated()
{
return $this->created;
}
/**
* #param mixed $created
*/
public function setCreated($created)
{
$this->created = $created;
}
/**
* #return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* #param \DateTime $updated
*/
public function setUpdated($updated)
{
$this->updated = $updated;
}
/**
* #return \DateTime
*/
public function getFeaturedTime()
{
return $this->featuredTime;
}
/**
* #param \DateTime $featuredTime
*/
public function setFeaturedTime($featuredTime)
{
$this->featuredTime = $featuredTime;
}
/**
* #return string
*/
public function getPublications()
{
return $this->publications;
}
/**
* #param string $publications
*/
public function setPublications($publications)
{
$this->publications = $publications;
}
/**
* #return mixed
*/
public function getSubmissionTime()
{
return $this->submissionTime;
}
/**
* #param mixed $submissionTime
*/
public function setSubmissionTime($submissionTime)
{
$this->submissionTime = $submissionTime;
}
/**
* #return string
*/
public function getFolioLabel()
{
return $this->folioLabel;
}
/**
* #param string $folioLabel
*/
public function setFolioLabel($folioLabel)
{
$this->folioLabel = $folioLabel;
}
public function getType()
{
return 'portfolio';
}
/**
* #return \DateTime
*/
public function getMostRecentProjectDate()
{
return $this->mostRecentProjectDate;
}
/**
* #param \DateTime $mostRecentProjectDate
*/
public function setMostRecentProjectDate($mostRecentProjectDate)
{
$this->mostRecentProjectDate = $mostRecentProjectDate;
}
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
}
And here is the code sample I use to reproduce this error:
<?php
// namespace and use ...
/**
* Tool controller.
*
* #Route("/admin/test")
*/
class TestController extends Controller
{
/**
*
* #Route("/testTwo", name="testTwo")
* #Method({"GET", "POST"})
*/
public function indexTwoAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$pr = $this->get('creasenso.portfolio_repo');
$p = $pr->find(32);
$em->remove($p);
$em->flush();
return new Response("<html><head></head><body><hr />Done</body></html>");
}
}
Here is the repository linked to this entity:
<?php
namespace CreasensoBundle\Repository;
use Doctrine\ORM\EntityRepository;
/**
* PortfolioRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class PortfolioRepository extends EntityRepository
{
protected $qb;
public function init()
{
$this->qb = $this->createQueryBuilder('p');
}
public function isVisible()
{
$this->qb
->andWhere('p.visible = :visible')
->setParameter(':visible', 1);
}
public function getAllPublicPortfolioCount()
{
$this->init();
$this->isVisible();
$this->qb->select('COUNT(p)');
return $this->qb->getQuery()->getSingleScalarResult();
}
public function getQueryBuilder()
{
return $this->qb;
}
}
Do you have any clue about this behavior? Thank you very much.
You are getting your repository directly from the service container when you should be getting it from your EntityManager through getRepository().
You are not loading your entity from the entityManager that you flush so it is not managed

Symfony2 After form submit entity not extended on base entity

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

Doctrine 2 perform insert, update on entity locally but fails without error on staging

I have an entity called DoerTrip
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Doer Trip
*
* #ORM\Table(name="doer_trip")
* #ORM\Entity
*/
class DoerTrip extends AbstractEntity
{
const STATUS_PUBLISHED = 1;
const STATUS_UNPUBLISHED = 0;
/**
* #var integer
*
* #ORM\Column(name="`id`", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #var \Application\Entity\Doer
*
* #ORM\ManyToOne(targetEntity="Doer")
* #ORM\JoinColumn(name="`doer_id`", referencedColumnName="id")
*/
protected $doer; // inversedBy="trips"
/**
* #var \Application\Entity\Trip
*
* #ORM\ManyToOne(targetEntity="Trip")
* #ORM\JoinColumn(name="`trip_id`", referencedColumnName="id")
*/
protected $trip; //inversedBy="doers"
/**
* #var bool
*
* #ORM\Column(name="`published`", type="boolean")
*/
protected $published;
/**
* #var string
*
* #ORM\Column(name="`comment`", type="text")
*/
protected $comment;
/**
* #var integer
*
* #ORM\Column(name="`target_sum`", type="integer")
*/
protected $targetSum;
public function __construct()
{
$this->published = false;
}
/**
* #param string $comment
* #return $this
*/
public function setComment($comment)
{
$this->comment = $comment;
return $this;
}
/**
* #return string
*/
public function getComment()
{
return $this->comment;
}
/**
* #param \Application\Entity\Doer $doer
* #return $this
*/
public function setDoer(Doer $doer)
{
$this->doer = $doer;
return $this;
}
/**
* #return \Application\Entity\Doer
*/
public function getDoer()
{
return $this->doer;
}
/**
* #param int $id
* #return $this
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #param boolean $published
* #return $this
*/
public function setPublished($published)
{
$this->published = $published;
return $this;
}
/**
* #return boolean
*/
public function getPublished()
{
return $this->published;
}
/**
* #param \Application\Entity\Trip $trip
* #return $this
*/
public function setTrip(Trip $trip)
{
$this->trip = $trip;
return $this;
}
/**
* #return \Application\Entity\Trip
*/
public function getTrip()
{
return $this->trip;
}
/**
* #param int $targetSum
* #return $this
*/
public function setTargetSum($targetSum)
{
$this->targetSum = $targetSum;
return $this;
}
/**
* #return int
*/
public function getTargetSum()
{
return (null !== $this->targetSum) ? $this->targetSum : $this->getTrip()->getTargetAmount();
}
}
here Trip Entity:
namespace Application\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Zend\Validator\IsInstanceOf;
/**
* Trip
*
* #ORM\Table(name="trip")
* #ORM\Entity
*/
class Trip extends AbstractEntity
{
const STATUS_ACTIVE = 'active';
const STATUS_INACTIVE = 'inactive';
const STATUS_BANNED = 'banned';
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
protected $name;
/**
* #var Collection
*
* #ORM\ManyToMany(targetEntity="Media", cascade={"remove"}, orphanRemoval=true)
* #ORM\JoinTable(name="trip_media",
* joinColumns={#ORM\JoinColumn(name="trip_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="file_id", referencedColumnName="id", unique=true)}
* )
*/
protected $media;
/**
* #var \Application\Entity\Media
*
* #ORM\OneToOne(targetEntity="Media", mappedBy="trip", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="main_media_id", referencedColumnName="id")
*/
protected $mainMedia;
/**
* #var string
*
* #ORM\Column(name="api_key", type="string", length=255)
*/
protected $apiKey;
/**
* #var string
*
* #ORM\Column(name="country", type="string", length=255, nullable=true)
*/
protected $country;
/**
* #var \DateTime
*
* #ORM\Column(name="departure_date", type="date", nullable=true)
*/
protected $departureDate;
/**
* #var \DateTime
*
* #ORM\Column(name="due_date", type="date", nullable=true)
*/
protected $dueDate;
/**
* #var \DateTime
*
* #ORM\Column(name="return_date", type="date", nullable=true)
*/
protected $returnDate;
/**
* #var string
*
* #ORM\Column(name="description", type="text", nullable=true)
*/
protected $description;
/**
* #var string
*
* #ORM\Column(name="target_amount", type="integer")
*/
protected $targetAmount;
/**
* #var Collection
*
* #ORM\OneToMany(targetEntity="DoerTrip", mappedBy="trip", cascade={"persist","remove"}, orphanRemoval=true)
*/
protected $doers;
/**
* #var Organization
*
* #ORM\ManyToOne(targetEntity="Organization", inversedBy="trips")
* #ORM\JoinColumn(name="organization_id", referencedColumnName="id")
*/
protected $organization;
/**
* #var Cause
*
* #ORM\OneToOne(targetEntity="Cause", inversedBy="trips")
* #ORM\JoinColumn(name="cause_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $cause;
/**
* #var integer
*
* #ORM\Column(name="status", type="string" ,columnDefinition="ENUM('active', 'inactive', 'banned')")
*/
protected $status;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #var Transaction
*
* #ORM\OneToMany(targetEntity="Transaction", mappedBy="trip", cascade={"remove"}, orphanRemoval=true)
*/
protected $transactions;
/**
* #param Organization $organization
* #return $this
*/
public function setOrganization(Organization $organization)
{
$this->organization = $organization;
return $this;
}
/**
* #return Organization
*/
public function getOrganization()
{
return $this->organization;
}
public function __construct()
{
$this->doers = new ArrayCollection();
$this->media = new ArrayCollection();
$this->status = self::STATUS_ACTIVE;
$this->transactions = new ArrayCollection();
$this->targetAmount = 0;
$this->description = '';
}
/**
* #param string $apiKey
* #return $this
*/
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
return $this;
}
/**
* #return string
*/
public function getApiKey()
{
return $this->apiKey;
}
/**
* #param string $country
* #return $this
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* #return string
*/
public function getCountry()
{
return $this->country;
}
/**
* #param string $description
* #return $this
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* #param \DateTime $dueDate
* #return $this
*/
public function setDueDate(\DateTime $dueDate)
{
$this->dueDate = $dueDate;
return $this;
}
/**
* #return \DateTime
*/
public function getDueDate()
{
return $this->dueDate;
}
/**
* #param int $id
* #return $this
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #param \DateTime $departureDate
* #return $this
*/
public function setDepartureDate(\DateTime $departureDate)
{
$this->departureDate = $departureDate;
return $this;
}
/**
* #return \DateTime
*/
public function getDepartureDate()
{
return $this->departureDate;
}
/**
* #param string $targetAmount
* #return $this
*/
public function setTargetAmount($targetAmount)
{
$this->targetAmount = $targetAmount;
return $this;
}
/**
* #return string
*/
public function getTargetAmount()
{
return $this->targetAmount;
}
/**
* #param string $name
* #return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* #param int $status
* #return $this
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* #return int
*/
public function getStatus()
{
return $this->status;
}
/**
* #param \Application\Entity\Cause $cause
* #return $this
*/
public function setCause($cause)
{
$this->cause = $cause;
return $this;
}
/**
* #return \Application\Entity\Cause
*/
public function getCause()
{
return $this->cause;
}
/**
* #param \Doctrine\Common\Collections\Collection $media
* #return $this
*/
public function setMedia($media)
{
$this->media = $media;
return $this;
}
/**
* #return \Doctrine\Common\Collections\Collection
*/
public function getMedia()
{
return $this->media;
}
/**
* #param \Application\Entity\DoerTrip $doer
* #return $this
*/
public function addDoer(DoerTrip $doer)
{
$this->doers->add($doer);
$doer->setTrip($this);
return $this;
}
/**
* #param Collection $doers
* #return $this
*/
public function setDoers(Collection $doers)
{
$this->doers = $doers;
return $this;
}
/**
* #return Collection
*/
public function getDoers()
{
return $this->doers;
}
/**
* #param \Application\Entity\DoerTrip $doer
* #return $this
*/
public function removeDoer(DoerTrip $doer)
{
$this->doers->removeElement($doer);
return $this;
}
/**
* #param \Doctrine\Common\Collections\Collection $transactions
* #return $this
*/
public function setTransactions($transactions)
{
$this->transactions = $transactions;
return $this;
}
/**
* #return \Doctrine\Common\Collections\Collection
*/
public function getTransactions()
{
return $this->transactions;
}
/**
* #param array $data
* #return $this
*/
public function populate(array $data)
{
if (!empty($data['departureDate']) && !$data['departureDate'] instanceof \DateTime) {
$data['departureDate'] = new \DateTime($data['departureDate']);
}
if (!empty($data['dueDate']) && !$data['dueDate'] instanceof \DateTime) {
$data['dueDate'] = new \DateTime($data['dueDate']);
}
if (!empty($data['returnDate']) && !$data['returnDate'] instanceof \DateTime) {
$data['returnDate'] = new \DateTime($data['returnDate']);
}
parent::populate($data);
return $this;
}
/**
* #param \DateTime $returnDate
* #return $this
*/
public function setReturnDate(\DateTime $returnDate)
{
$this->returnDate = $returnDate;
return $this;
}
/**
* #return \DateTime
*/
public function getReturnDate()
{
return $this->returnDate;
}
/**
* #param \Application\Entity\Media $mainMedia
* #return $this
*/
public function setMainMedia($mainMedia)
{
$this->mainMedia = $mainMedia;
return $this;
}
/**
* #return \Application\Entity\Media
*/
public function getMainMedia()
{
return $this->mainMedia;
}
/**
* #param Media $media
* #return bool
*/
public function hasMedia(Media $media)
{
return $this->getMedia()->contains($media);
}
/**
* #param \Application\Entity\Media $media
* #return $this
*/
public function addMedia(Media $media)
{
$this->media->add($media);
return $this;
}
/**
* #param \Application\Entity\Media $media
* #return $this
*/
public function removeMedia(Media $media)
{
$this->media->removeElement($media);
return $this;
}
}
Here is Doer Entity
namespace Application\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Zend\Validator\IsInstanceOf;
/**
* Trip
*
* #ORM\Table(name="trip")
* #ORM\Entity
*/
class Trip extends AbstractEntity
{
const STATUS_ACTIVE = 'active';
const STATUS_INACTIVE = 'inactive';
const STATUS_BANNED = 'banned';
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
protected $name;
/**
* #var Collection
*
* #ORM\ManyToMany(targetEntity="Media", cascade={"remove"}, orphanRemoval=true)
* #ORM\JoinTable(name="trip_media",
* joinColumns={#ORM\JoinColumn(name="trip_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="file_id", referencedColumnName="id", unique=true)}
* )
*/
protected $media;
/**
* #var \Application\Entity\Media
*
* #ORM\OneToOne(targetEntity="Media", mappedBy="trip", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="main_media_id", referencedColumnName="id")
*/
protected $mainMedia;
/**
* #var string
*
* #ORM\Column(name="api_key", type="string", length=255)
*/
protected $apiKey;
/**
* #var string
*
* #ORM\Column(name="country", type="string", length=255, nullable=true)
*/
protected $country;
/**
* #var \DateTime
*
* #ORM\Column(name="departure_date", type="date", nullable=true)
*/
protected $departureDate;
/**
* #var \DateTime
*
* #ORM\Column(name="due_date", type="date", nullable=true)
*/
protected $dueDate;
/**
* #var \DateTime
*
* #ORM\Column(name="return_date", type="date", nullable=true)
*/
protected $returnDate;
/**
* #var string
*
* #ORM\Column(name="description", type="text", nullable=true)
*/
protected $description;
/**
* #var string
*
* #ORM\Column(name="target_amount", type="integer")
*/
protected $targetAmount;
/**
* #var Collection
*
* #ORM\OneToMany(targetEntity="DoerTrip", mappedBy="trip", cascade={"persist","remove"}, orphanRemoval=true)
*/
protected $doers;
/**
* #var Organization
*
* #ORM\ManyToOne(targetEntity="Organization", inversedBy="trips")
* #ORM\JoinColumn(name="organization_id", referencedColumnName="id")
*/
protected $organization;
/**
* #var Cause
*
* #ORM\OneToOne(targetEntity="Cause", inversedBy="trips")
* #ORM\JoinColumn(name="cause_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $cause;
/**
* #var integer
*
* #ORM\Column(name="status", type="string" ,columnDefinition="ENUM('active', 'inactive', 'banned')")
*/
protected $status;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #var Transaction
*
* #ORM\OneToMany(targetEntity="Transaction", mappedBy="trip", cascade={"remove"}, orphanRemoval=true)
*/
protected $transactions;
/**
* #param Organization $organization
* #return $this
*/
public function setOrganization(Organization $organization)
{
$this->organization = $organization;
return $this;
}
/**
* #return Organization
*/
public function getOrganization()
{
return $this->organization;
}
public function __construct()
{
$this->doers = new ArrayCollection();
$this->media = new ArrayCollection();
$this->status = self::STATUS_ACTIVE;
$this->transactions = new ArrayCollection();
$this->targetAmount = 0;
$this->description = '';
}
/**
* #param string $apiKey
* #return $this
*/
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
return $this;
}
/**
* #return string
*/
public function getApiKey()
{
return $this->apiKey;
}
/**
* #param string $country
* #return $this
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* #return string
*/
public function getCountry()
{
return $this->country;
}
/**
* #param string $description
* #return $this
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* #param \DateTime $dueDate
* #return $this
*/
public function setDueDate(\DateTime $dueDate)
{
$this->dueDate = $dueDate;
return $this;
}
/**
* #return \DateTime
*/
public function getDueDate()
{
return $this->dueDate;
}
/**
* #param int $id
* #return $this
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #param \DateTime $departureDate
* #return $this
*/
public function setDepartureDate(\DateTime $departureDate)
{
$this->departureDate = $departureDate;
return $this;
}
/**
* #return \DateTime
*/
public function getDepartureDate()
{
return $this->departureDate;
}
/**
* #param string $targetAmount
* #return $this
*/
public function setTargetAmount($targetAmount)
{
$this->targetAmount = $targetAmount;
return $this;
}
/**
* #return string
*/
public function getTargetAmount()
{
return $this->targetAmount;
}
/**
* #param string $name
* #return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* #param int $status
* #return $this
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* #return int
*/
public function getStatus()
{
return $this->status;
}
/**
* #param \Application\Entity\Cause $cause
* #return $this
*/
public function setCause($cause)
{
$this->cause = $cause;
return $this;
}
/**
* #return \Application\Entity\Cause
*/
public function getCause()
{
return $this->cause;
}
/**
* #param \Doctrine\Common\Collections\Collection $media
* #return $this
*/
public function setMedia($media)
{
$this->media = $media;
return $this;
}
/**
* #return \Doctrine\Common\Collections\Collection
*/
public function getMedia()
{
return $this->media;
}
/**
* #param \Application\Entity\DoerTrip $doer
* #return $this
*/
public function addDoer(DoerTrip $doer)
{
$this->doers->add($doer);
$doer->setTrip($this);
return $this;
}
/**
* #param Collection $doers
* #return $this
*/
public function setDoers(Collection $doers)
{
$this->doers = $doers;
return $this;
}
/**
* #return Collection
*/
public function getDoers()
{
return $this->doers;
}
/**
* #param \Application\Entity\DoerTrip $doer
* #return $this
*/
public function removeDoer(DoerTrip $doer)
{
$this->doers->removeElement($doer);
return $this;
}
/**
* #param \Doctrine\Common\Collections\Collection $transactions
* #return $this
*/
public function setTransactions($transactions)
{
$this->transactions = $transactions;
return $this;
}
/**
* #return \Doctrine\Common\Collections\Collection
*/
public function getTransactions()
{
return $this->transactions;
}
/**
* #param array $data
* #return $this
*/
public function populate(array $data)
{
if (!empty($data['departureDate']) && !$data['departureDate'] instanceof \DateTime) {
$data['departureDate'] = new \DateTime($data['departureDate']);
}
if (!empty($data['dueDate']) && !$data['dueDate'] instanceof \DateTime) {
$data['dueDate'] = new \DateTime($data['dueDate']);
}
if (!empty($data['returnDate']) && !$data['returnDate'] instanceof \DateTime) {
$data['returnDate'] = new \DateTime($data['returnDate']);
}
parent::populate($data);
return $this;
}
/**
* #param \DateTime $returnDate
* #return $this
*/
public function setReturnDate(\DateTime $returnDate)
{
$this->returnDate = $returnDate;
return $this;
}
/**
* #return \DateTime
*/
public function getReturnDate()
{
return $this->returnDate;
}
/**
* #param \Application\Entity\Media $mainMedia
* #return $this
*/
public function setMainMedia($mainMedia)
{
$this->mainMedia = $mainMedia;
return $this;
}
/**
* #return \Application\Entity\Media
*/
public function getMainMedia()
{
return $this->mainMedia;
}
/**
* #param Media $media
* #return bool
*/
public function hasMedia(Media $media)
{
return $this->getMedia()->contains($media);
}
/**
* #param \Application\Entity\Media $media
* #return $this
*/
public function addMedia(Media $media)
{
$this->media->add($media);
return $this;
}
/**
* #param \Application\Entity\Media $media
* #return $this
*/
public function removeMedia(Media $media)
{
$this->media->removeElement($media);
return $this;
}
}
I perform this sample of code:
$doerTrip = new DoerTrip();
$doerTrip->setDoer($doer)->setTrip($trip);
$em->persist($doerTrip);
$em->flush();
locally it works (on Windows). But on staging (Ubuntu) new record is not created and I don't get any errors. Update of the entity DoerTrip on staging doesn't work too.
If I perform insert with the help of connection something like this
$stmt = $conn->prepare('INSERT INTO doer_trip (...) VALUES (?,?,?,?,?)');
$stmt->bindParam(1, $param);
...
everything works fine.
When using ORM in SQL Logger I can see
START TRANSACTION;
INSERT INTO doer_trip (...) VALUES (?,?,?,?,?)
COMMIT;
but new record is not created on staging.
locally everything works fine.
I don't know maybe it depends on MySQL configuration.
I have no idea.
Here my doer_trip table
CREATE TABLE `doer_trip` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`doer_id` INT(11) NOT NULL,
`trip_id` INT(11) UNSIGNED NOT NULL,
`published` INT(1) UNSIGNED NOT NULL DEFAULT '0',
`comment` TEXT NULL,
`target_sum` INT(10) UNSIGNED NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `doer_id` (`doer_id`),
INDEX `trip_id` (`trip_id`),
UNIQUE INDEX `doer_iduniq` (`doer_id`, `trip_id`),
CONSTRAINT `FK_doer_trip_trip` FOREIGN KEY (`trip_id`) REFERENCES `trip` (`id`),
CONSTRAINT `FK_doer_trip_doer` FOREIGN KEY (`doer_id`) REFERENCES `doer` (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
ROW_FORMAT=DEFAULT
AUTO_INCREMENT=41
The problem was in
/**
* #var bool
*
* #ORM\Column(name="`published`", type="boolean")
*/
protected $published;
If I set type to integer like this
/**
* #var integer
*
* #ORM\Column(name="`published`", type="integer")
*/
protected $published;
Everything work fine. But why it works fine on my local environment???

Categories