Related
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
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
I created a Twig extension, registered it in services, but im getting an error:
This is the extension:
<?php
// src/AppBundle/Twig/AppExtension.php
namespace Mp\ShopBundle\twig;
class AppExtension extends \Twig_Extension
{
public function getFunctions()
{
return array(
'getTotalPrice' => new \Twig_Function_Method($this, 'getTotalPrice'));
}
public function getTotalPrice(Items $items)
{
$total = 0;
foreach($items as $item){
$total += $item->getPrice();
}
return $total;
}
public function getName()
{
return 'app_extension';
}
}
Services:
services:
app.twig_extension:
class: Mp\ShopBundle\twig\AppExtension
public: false
tags:
- { name: twig.extension }
Now i want to count the sum of products with my extension like this:
{% for item in product %}
<td> ${{getTotalPrice(item)}}.00</td>
{% endfor %}
But i get this error:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Argument 1 passed to Mp\ShopBundle\twig\AppExtension::getTotalPrice() must be an instance of Mp\ShopBundle\twig\Items, instance of Mp\ShopBundle\Entity\Product given, called in C:\wamp\www\Digidis\tree\app\cache\dev\twig\b4\5d\b2cbf04f86aeef591812f9721d41a678d3fc5dbbd3aae638883d71c26af0.php on line 175 and defined") in MpShopBundle:Frontend:product_summary.html.twig at line 92.
Product:
<?php
namespace Mp\ShopBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Sonata\ClassificationBundle\Model\TagInterface;
use Sonata\ClassificationBundle\Model\Tag;
/**
* Product
*
* #ORM\Table(name="product", indexes={#ORM\Index(name="product_type_id", columns={"product_type_id"})})
* #ORM\Entity
*/
class Product
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="model", type="string", length=255, nullable=true)
*/
private $model;
/**
* #var \Mp\ShopBundle\Entity\ProductType
*
* #ORM\ManyToOne(targetEntity="Mp\ShopBundle\Entity\ProductType")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="product_type_id", referencedColumnName="id")
* })
*/
private $productType;
/**
* #var \Mp\ShopBundle\Entity\ProductLanguageData
*
* #ORM\OneToMany(targetEntity="ProductLanguageData", mappedBy="product", cascade={"persist"}, orphanRemoval=true)
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id", referencedColumnName="product_id")
* })
*/
private $translations;
/**
* #var string
*
* #ORM\Column(name="admin_title", type="string", length=255, nullable=true)
*/
private $admin_title;
protected $tags;
/**
* #var \Application\Sonata\ClassificationBundle\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Application\Sonata\ClassificationBundle\Entity\Category")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
* })
*/
private $category;
/**
* #var \Application\Sonata\ClassificationBundle\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Application\Sonata\ClassificationBundle\Entity\Category")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="subcategory_id", referencedColumnName="id")
* })
*/
private $subcategory;
/**
* #var \Application\Sonata\ClassificationBundle\Entity\Collection
*
* #ORM\ManyToOne(targetEntity="Application\Sonata\ClassificationBundle\Entity\Collection")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="manufacturer_id", referencedColumnName="id")
* })
*/
private $manufacturer;
/**
* #var boolean
*
* #ORM\Column(name="status", type="boolean")
*/
private $status;
/**
* #ORM\Column(name="created_at", type="datetime")
*/
private $created_at;
/**
* #ORM\Column(name="updated_at", type="datetime")
*/
private $updated_at;
/**
* #ORM\Column(name="pc", type="decimal", precision=10, scale=2, nullable=true)
*/
private $pc;
/**
* #ORM\Column(name="value", type="decimal", precision=10, scale=2, nullable=true)
*/
private $value;
/**
* #ORM\Column(name="discount", type="decimal", precision=10, scale=2, nullable=true)
*/
private $discount;
/**
* #ORM\Column(name="base", type="decimal", precision=10, scale=2, nullable=true)
*/
private $base;
/**
* #ORM\Column(name="price", type="decimal", precision=10, scale=2, nullable=true)
*/
private $price;
/**
* #ORM\Column(name="stock", type="integer", nullable=true)
*/
private $stock;
/**
* #ORM\Column(name="map", type="string", length=255, nullable=true)
*/
private $map;
/**
* #ORM\Column(name="feature1", type="string", length=255, nullable=true)
*/
private $feature1;
/**
* #ORM\Column(name="feature2", type="string", length=255, nullable=true)
*/
private $feature2;
/**
* #ORM\Column(name="feature3", type="string", length=255, nullable=true)
*/
private $feature3;
/**
* #ORM\Column(name="feature4", type="string", length=255, nullable=true)
*/
private $feature4;
/**
* #ORM\Column(name="feature5", type="string", length=255, nullable=true)
*/
private $feature5;
/**
* #ORM\Column(name="feature6", type="string", length=255, nullable=true)
*/
private $feature6;
/**
* #ORM\Column(name="feature7", type="string", length=255, nullable=true)
*/
private $feature7;
/**
* #ORM\Column(name="feature8", type="string", length=255, nullable=true)
*/
private $feature8;
/**
* #var boolean
*
* #ORM\Column(name="published", type="boolean", nullable=true)
*/
private $published;
/**
* #ORM\Column(name="url_marketing", type="string", length=255, nullable=true)
*/
private $url_marketing;
/**
* #ORM\Column(name="cesion_tienda", type="string", length=255, nullable=true)
*/
private $cesion_tienda;
/**
* #ORM\Column(name="google", type="string", length=255, nullable=true)
*/
private $google;
/**
* #ORM\Column(name="provider_reference", type="string", length=255, nullable=true)
*/
private $provider_reference;
private $gallery;
/**
* #ORM\Column(name="vat", type="string", length=255, nullable=true)
*/
private $vat;
private $provider;
/**
* #var string
*
* #ORM\Column(name="video1", type="text", length=65535, nullable=true)
*/
private $video1;
/**
* #var string
*
* #ORM\Column(name="video2", type="text", length=65535, nullable=true)
*/
private $video2;
/**
* #var string
*
* #ORM\Column(name="friendly_url", type="string", length=255, nullable=true)
*/
private $friendly_url;
/**
* #var string
*
* #ORM\Column(name="shop_assignment", type="text", length=65535, nullable=true)
*/
private $shop_assignment;
/**
* #var string
*
* #ORM\Column(name="custom_product_type", type="string", length=255, nullable=true)
*/
private $custom_product_type;
/**
* Set model
*
* #param string $model
* #return Product
*/
public function setModel($model)
{
$this->model = $model;
return $this;
}
/**
* Get model
*
* #return string
*/
public function getModel()
{
return $this->model;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set productType
*
* #param \Mp\ShopBundle\Entity\ProductType $productType
* #return Product
*/
public function setProductType(\Mp\ShopBundle\Entity\ProductType $productType = null)
{
$this->productType = $productType;
return $this;
}
/**
* Get productType
*
* #return \Mp\ShopBundle\Entity\ProductType
*/
public function getProductType()
{
return $this->productType;
}
/**
* Get translations
*
* #return \Mp\ShopBundle\Entity\ProductLanguageData
*/
public function getTranslations()
{
return $this->translations;
}
/**
* Set translations
*
* #param \Doctrine\ORM\PersistentCollection $translations
* #return Product
*/
public function setTranslations(\Doctrine\ORM\PersistentCollection $translations)
{
$this->translations = new ArrayCollection();
foreach ($translations as $s) {
$this->addTranslation($s);
}
return $this;
}
/**
* Add translation
*
* #param \Mp\ShopBundle\Entity\ProductLanguageData $translation
*/
public function addTranslation(\Mp\ShopBundle\Entity\ProductLanguageData $translation)
{
$translation->setProduct($this);
$this->translations[] = $translation;
}
/**
* Remove translation
*
* #param \Mp\ShopBundle\Entity\ProductLanguageData $translation
*/
public function removeTranslation(\Mp\ShopBundle\Entity\ProductLanguageData $translation)
{
foreach ($this->translations as $k => $s) {
if ($s->getId() == $translation->getId()) {
unset($this->translations[$k]);
}
}
}
/**
* Get string value
*
* #return string
*/
public function __toString()
{
return ($this->admin_title == "") ? "Product" : $this->admin_title;
}
/**
* Constructor
*/
public function __construct()
{
//$this->translations = new \Doctrine\Common\Collections\ArrayCollection();
$this->tags = new ArrayCollection();
$this->status = false;
}
/**
* Set admin_title
*
* #param string $adminTitle
* #return Product
*/
public function setAdminTitle($adminTitle)
{
$this->admin_title = $adminTitle;
return $this;
}
/**
* Get admin_title
*
* #return string
*/
public function getAdminTitle()
{
return $this->admin_title;
}
/**
* Add tags
*
* #param \Sonata\ClassificationBundle\Model\TagInterface $tags
*/
public function addTags(TagInterface $tags)
{
$this->tags[] = $tags;
}
/**
* Get tags
*
* #return array $tags
*/
public function getTags()
{
return $this->tags;
}
/**
* #param $tags
*
* #return mixed
*/
public function setTags($tags)
{
$this->tags = $tags;
}
/**
* Set category
*
* #param \Application\Sonata\ClassificationBundle\Entity\Category $category
* #return Product
*/
public function setCategory(\Application\Sonata\ClassificationBundle\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return \Application\Sonata\ClassificationBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
/**
* Add tags
*
* #param \Application\Sonata\ClassificationBundle\Entity\Tag $tags
* #return Product
*/
public function addTag(\Application\Sonata\ClassificationBundle\Entity\Tag $tags)
{
$this->tags[] = $tags;
return $this;
}
/**
* Remove tags
*
* #param \Application\Sonata\ClassificationBundle\Entity\Tag $tags
*/
public function removeTag(\Application\Sonata\ClassificationBundle\Entity\Tag $tags)
{
$this->tags->removeElement($tags);
}
/**
* Set subcategory
*
* #param \Application\Sonata\ClassificationBundle\Entity\Category $subcategory
* #return Product
*/
public function setSubcategory(\Application\Sonata\ClassificationBundle\Entity\Category $subcategory = null)
{
$this->subcategory = $subcategory;
return $this;
}
/**
* Get subcategory
*
* #return \Application\Sonata\ClassificationBundle\Entity\Category
*/
public function getSubcategory()
{
return $this->subcategory;
}
/**
* Set manufacturer
*
* #param \Application\Sonata\ClassificationBundle\Entity\Collection $manufacturer
* #return Product
*/
public function setManufacturer(\Application\Sonata\ClassificationBundle\Entity\Collection $manufacturer = null)
{
$this->manufacturer = $manufacturer;
return $this;
}
/**
* Get manufacturer
*
* #return \Application\Sonata\ClassificationBundle\Entity\Collection
*/
public function getManufacturer()
{
return $this->manufacturer;
}
/**
* Set status
*
* #param boolean $status
* #return Product
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return boolean
*/
public function getStatus()
{
return $this->status;
}
/**
* Set created_at
*
* #param \DateTime $createdAt
* #return Product
*/
public function setCreatedAt($createdAt)
{
$this->created_at = $createdAt;
return $this;
}
/**
* Get created_at
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->created_at;
}
/**
* Set updated_at
*
* #param \DateTime $updatedAt
* #return Product
*/
public function setUpdatedAt($updatedAt)
{
$this->updated_at = $updatedAt;
return $this;
}
/**
* Get updated_at
*
* #return \DateTime
*/
public function getUpdatedAt()
{
return $this->updated_at;
}
/**
* Set pc
*
* #param string $pc
* #return Product
*/
public function setPc($pc)
{
$this->pc = $pc;
return $this;
}
/**
* Get pc
*
* #return string
*/
public function getPc()
{
return $this->pc;
}
/**
* Set value
*
* #param string $value
* #return Product
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value
*
* #return string
*/
public function getValue()
{
return $this->value;
}
/**
* Set discount
*
* #param string $discount
* #return Product
*/
public function setDiscount($discount)
{
$this->discount = $discount;
return $this;
}
/**
* Get discount
*
* #return string
*/
public function getDiscount()
{
return $this->discount;
}
/**
* Set base
*
* #param string $base
* #return Product
*/
public function setBase($base)
{
$this->base = $base;
return $this;
}
/**
* Get base
*
* #return string
*/
public function getBase()
{
return $this->base;
}
/**
* Set price
*
* #param string $price
* #return Product
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* #return string
*/
public function getPrice()
{
return $this->price;
}
/**
* Set stock
*
* #param string $stock
* #return Product
*/
public function setStock($stock)
{
$this->stock = $stock;
return $this;
}
/**
* Get stock
*
* #return string
*/
public function getStock()
{
return $this->stock;
}
/**
* Set map
*
* #param string $map
* #return Product
*/
public function setMap($map)
{
$this->map = $map;
return $this;
}
/**
* Get map
*
* #return string
*/
public function getMap()
{
return $this->map;
}
/**
* Set feature1
*
* #param string $feature1
* #return Product
*/
public function setFeature1($feature1)
{
$this->feature1 = $feature1;
return $this;
}
/**
* Get feature1
*
* #return string
*/
public function getFeature1()
{
return $this->feature1;
}
/**
* Set feature2
*
* #param string $feature2
* #return Product
*/
public function setFeature2($feature2)
{
$this->feature2 = $feature2;
return $this;
}
/**
* Get feature2
*
* #return string
*/
public function getFeature2()
{
return $this->feature2;
}
/**
* Set feature3
*
* #param string $feature3
* #return Product
*/
public function setFeature3($feature3)
{
$this->feature3 = $feature3;
return $this;
}
/**
* Get feature3
*
* #return string
*/
public function getFeature3()
{
return $this->feature3;
}
/**
* Set feature4
*
* #param string $feature4
* #return Product
*/
public function setFeature4($feature4)
{
$this->feature4 = $feature4;
return $this;
}
/**
* Get feature4
*
* #return string
*/
public function getFeature4()
{
return $this->feature4;
}
/**
* Set feature5
*
* #param string $feature5
* #return Product
*/
public function setFeature5($feature5)
{
$this->feature5 = $feature5;
return $this;
}
/**
* Get feature5
*
* #return string
*/
public function getFeature5()
{
return $this->feature5;
}
/**
* Set feature6
*
* #param string $feature6
* #return Product
*/
public function setFeature6($feature6)
{
$this->feature6 = $feature6;
return $this;
}
/**
* Get feature6
*
* #return string
*/
public function getFeature6()
{
return $this->feature6;
}
/**
* Set feature7
*
* #param string $feature7
* #return Product
*/
public function setFeature7($feature7)
{
$this->feature7 = $feature7;
return $this;
}
/**
* Get feature7
*
* #return string
*/
public function getFeature7()
{
return $this->feature7;
}
/**
* Set feature8
*
* #param string $feature8
* #return Product
*/
public function setFeature8($feature8)
{
$this->feature8 = $feature8;
return $this;
}
/**
* Get feature8
*
* #return string
*/
public function getFeature8()
{
return $this->feature8;
}
/**
* Set published
*
* #param boolean $published
* #return Product
*/
public function setPublished($published)
{
$this->published = $published;
return $this;
}
/**
* Get published
*
* #return boolean
*/
public function getPublished()
{
return $this->published;
}
/**
* Set url_marketing
*
* #param string $urlMarketing
* #return Product
*/
public function setUrlMarketing($urlMarketing)
{
$this->url_marketing = $urlMarketing;
return $this;
}
/**
* Get url_marketing
*
* #return string
*/
public function getUrlMarketing()
{
return $this->url_marketing;
}
/**
* Set cesion_tienda
*
* #param string $cesionTienda
* #return Product
*/
public function setCesionTienda($cesionTienda)
{
$this->cesion_tienda = $cesionTienda;
return $this;
}
/**
* Get cesion_tienda
*
* #return string
*/
public function getCesionTienda()
{
return $this->cesion_tienda;
}
/**
* Set google
*
* #param string $google
* #return Product
*/
public function setGoogle($google)
{
$this->google = $google;
return $this;
}
/**
* Get google
*
* #return string
*/
public function getGoogle()
{
return $this->google;
}
/**
* Set provider_reference
*
* #param string $providerReference
* #return Product
*/
public function setProviderReference($providerReference)
{
$this->provider_reference = $providerReference;
return $this;
}
/**
* Get provider_reference
*
* #return string
*/
public function getProviderReference()
{
return $this->provider_reference;
}
/**
* Set gallery
*
* #param \Application\Sonata\MediaBundle\Entity\Gallery $gallery
* #return Product
*/
public function setGallery(\Application\Sonata\MediaBundle\Entity\Gallery $gallery = null)
{
$this->gallery = $gallery;
return $this;
}
/**
* Get gallery
*
* #return \Application\Sonata\MediaBundle\Entity\Gallery
*/
public function getGallery()
{
return $this->gallery;
}
/**
* Set vat
*
* #param string $vat
* #return Product
*/
public function setVat($vat)
{
$this->vat = $vat;
return $this;
}
/**
* Get vat
*
* #return string
*/
public function getVat()
{
return $this->vat;
}
/**
* Set provider
*
* #param \Application\Sonata\UserBundle\Entity\User $provider
* #return Product
*/
public function setProvider(\Application\Sonata\UserBundle\Entity\User $provider = null)
{
$this->provider = $provider;
return $this;
}
/**
* Get provider
*
* #return \Application\Sonata\UserBundle\Entity\User
*/
public function getProvider()
{
return $this->provider;
}
/**
* Set video1
*
* #param string $video1
* #return Product
*/
public function setVideo1($video1)
{
$this->video1 = $video1;
return $this;
}
/**
* Get custom_product_type
*
* #return string
*/
public function getCustomProductType()
{
return $this->custom_product_type;
}
}
So for some reason it is passing the wrong class? How can this be fixed?
Yes, you are passing the wrong class in your twig template.
In this part of code:
{% for item in product %}
<td>${{getTotalPrice(item)}}.00</td>
{% endfor %}
You are passing the item parameter which is the instance of Product class.
You should rewrite your Twig extension to accept Product instanses:
public function getTotalPrice($items)
{
$total = 0;
foreach($items as $item){
$total += $item->getPrice();
}
return $total;
}
And then pass the whole array, not only one Product:
<td>${{getTotalPrice(product)}}.00</td>
Hope this helps!
If you want pass a reference of you Product object (entity), you have to inform it to your method:
use Mp\ShopBundle\Entity\Product; - src/AppBundle/Twig/AppExtension.php
And then change your Items to Product object:
...
public function getTotalPrice(Product $items) {
....
I have an api in symfony2, in which adding and deleting items is not issue, however, updating doesn't throw me any errors however the relevent row in my database does not get updated!
My Controller method:
/*
* #Route("/complete/uid/{storeUid}",
* name = "media_complete"
* )
*
* #Method({"GET"})
*
* #param String $storeUid - the uid for the store that has completed downloading
*
* #return Response
*/
public function downloadCompleteAction($storeUid)
{
$response = $this->getNewJsonResponse();
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository("SimplySmartUHDProBundle:Machine");
try {
//get the machine from the db
$machine = $repo->findOneBy(array(
"uid" => $storeUid
));
//set it as download completed
$machine->setStatus(Machine::STATUS_DOWNLOAD_COMPLETE);
$em->persist($machine);
$em->flush();
$response->setStatusCode(200);
} catch (\Exception $e) {
//if there is an error, catch the exception set the status to 500 and return json with the exception error
$error = array("message" => $e->getMessage());
$response->setContent(json_encode($error));
$response->setStatusCode(500);
}
return $response;
}
My Entity:
namespace SimplySmart\UHDProBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use SimplySmart\UHDProBundle\Entity\Store;
/**
* Machine
*
* #ORM\Table(name="Machines", uniqueConstraints={#ORM\UniqueConstraint(name="UID", columns={"UID"})})
* #ORM\Entity
*/
class Machine
{
const STATUS_NO_FEED = 0;
const STATUS_REQUIRES_DOWNLOAD = 1;
const STATUS_DOWNLOAD_IN_PROGRESS = 2;
const STATUS_DOWNLOAD_COMPLETE = 3;
/**
* Array of available statuses
*
* #var array
*/
static public $statuses = array(
self::STATUS_NO_FEED,
self::STATUS_REQUIRES_DOWNLOAD,
self::STATUS_DOWNLOAD_IN_PROGRESS,
self::STATUS_DOWNLOAD_COMPLETE
);
/**
* #var string
*
* #ORM\Column(name="UID", type="string", length=50, nullable=false)
*/
private $uid;
/**
* #var string
*
* #ORM\Column(name="StoreCode", type="string", length=10, nullable=false)
*/
private $storecode;
/**
* #var string
*
* #ORM\Column(name="Description", type="string", length=100, nullable=true)
*/
private $description;
/**
* #var boolean
*
* #ORM\Column(name="Status", type="boolean", nullable=false)
*/
private $status;
/**
* #var boolean
*
* #ORM\Column(name="OnlineStatus", type="boolean", nullable=false)
*/
private $onlinestatus;
/**
* #var string
*
* #ORM\Column(name="Version", type="string", length=10, nullable=false)
*/
private $version;
/**
* #var \DateTime
*
* #ORM\Column(name="Timestamp", type="datetime", nullable=false)
*/
private $timestamp;
/**
* #ORM\ManyToOne(targetEntity="SimplySmart\UHDProBundle\Entity\Store", inversedBy="machines")
* #ORM\JoinColumn(name="StoreCode", referencedColumnName="Code")
*/
protected $store;
/**
* #ORM\ManyToMany(targetEntity="SimplySmart\UHDProBundle\Entity\Feed", inversedBy="machines")
* #ORM\JoinTable(joinColumns={#ORM\JoinColumn(name="machine_id", referencedColumnName="MachID")},
* inverseJoinColumns={#ORM\JoinColumn(name="feed_id", referencedColumnName="FeedID")}
* )
*
*/
protected $feeds;
/**
* #var integer
*
* #ORM\Column(name="MachID", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $machid;
/**
*
*/
public function __construct()
{
$this->feeds = new ArrayCollection();
}
/**
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* #param string $description
*
* #return $this
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* #return string
*/
public function getFeedtype()
{
return $this->feedtype;
}
/**
* #param string $feedtype
*
* #return $this
*/
public function setFeedtype($feedtype)
{
$this->feedtype = $feedtype;
return $this;
}
/**
* #return int
*/
public function getMachid()
{
return $this->machid;
}
/**
* #param int $machid
*
* #return $this
*/
public function setMachid($machid)
{
$this->machid = $machid;
return $this;
}
/**
* #return boolean
*/
public function isOnlinestatus()
{
return $this->onlinestatus;
}
/**
* #param boolean $onlinestatus
*
* #return $this
*/
public function setOnlinestatus($onlinestatus)
{
$this->onlinestatus = $onlinestatus;
return $this;
}
/**
* #return boolean
*/
public function isStatus()
{
return $this->status;
}
/**
* #param boolean $status
*
* #throws \Exception if invalid status is given
*
* #return $this
*/
public function setStatus($status)
{
if (in_array($status, self::$statuses)) {
$this->status = $status;
} else {
throw new \Exception("invalid status given");
}
return $this;
}
/**
* #return string
*/
public function getStorecode()
{
return $this->storecode;
}
/**
* #param string $storecode
*
* #return $this
*/
public function setStorecode($storecode)
{
$this->storecode = $storecode;
return $this;
}
/**
* #return \DateTime
*/
public function getTimestamp()
{
return $this->timestamp;
}
/**
* #param \DateTime $timestamp
*
* #return $this
*/
public function setTimestamp($timestamp)
{
$this->timestamp = $timestamp;
return $this;
}
/**
* #return string
*/
public function getUid()
{
return $this->uid;
}
/**
* #param string $uid
*
* #return $this
*/
public function setUid($uid)
{
$this->uid = $uid;
return $this;
}
/**
* #return string
*/
public function getVersion()
{
return $this->version;
}
/**
* #param string $version
*
* #return $this
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* Get status
*
* #return boolean
*/
public function getStatus()
{
return $this->status;
}
/**
* Get onlinestatus
*
* #return boolean
*/
public function getOnlinestatus()
{
return $this->onlinestatus;
}
/**
* Set store
*
* #param \SimplySmart\UHDProBundle\Entity\Store $store
* #return Machine
*/
public function setStore(\SimplySmart\UHDProBundle\Entity\Store $store = null)
{
$this->store = $store;
return $this;
}
/**
* Get store
*
* #return \SimplySmart\UHDProBundle\Entity\Store
*/
public function getStore()
{
return $this->store;
}
/**
* Method to generate and set a new UID
*
* #return $this
*/
public function generateNewUid()
{
$date = new \DateTime("UTC");
$uid = "U";
$uid .= $date->format("U");
$uid .= 'R'.rand(0,100);
$this->setUid($uid);
return $this;
}
/**
* Add feeds
*
* #param \SimplySmart\UHDProBundle\Entity\Feed $feeds
* #return Machine
*/
public function addFeed(\SimplySmart\UHDProBundle\Entity\Feed $feeds)
{
$this->feeds[] = $feeds;
return $this;
}
/**
* Remove feeds
*
* #param \SimplySmart\UHDProBundle\Entity\Feed $feeds
*/
public function removeFeed(\SimplySmart\UHDProBundle\Entity\Feed $feeds)
{
$this->feeds->removeElement($feeds);
}
/**
* Get feeds
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getFeeds()
{
return $this->feeds;
}
}
I am fully aware that when updating an entity I shouldn't need to use $em->persist($machine), however removing this doesn't seem to make any difference.
The part that is really grinding my gears, is that when I go on the profiler, it appears as it is ran all the queries as expected!
Just realised that I've been a complete idiot and my issue is that status is set as a boolean field in my entity, so it was updating, but always setting it as 1!
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???