Symfony2 wrong instance passed with twig extension - php

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) {
....

Related

Mapping Exception in Symfony / Doctrine: The target-entity App\DocumentBundle\File cannot be found in 'App\DocumentBundle\Entity\Document#file'

I'm trying to upgrade an application running on Symfony 3.4 to version 4.4. I already did multiple changes and adaptions according to the official documentation for upgrading Symfony. Neverthelesse, I'm stuck now with the following error that comes up when I try to run a command in the terminal or access the application via browser.
Mapping Exception
Doctrine\ORM\Mapping\MappingException:
The target-entity App\DocumentBundle\File cannot be found in 'App\DocumentBundle\Entity\Document#file'.
at /srv/http/sp7/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php:772
at Doctrine\ORM\Mapping\MappingException::invalidTargetEntityClass('App\\DocumentBundle\\File', 'App\\DocumentBundle\\Entity\\Document', 'file')
(/srv/http/sp7/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php:1032)
at Doctrine\ORM\Mapping\ClassMetadataInfo->validateAssociations()
(/srv/http/sp7/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:266)
at Doctrine\ORM\Mapping\ClassMetadataFactory->validateRuntimeMetadata(object(ClassMetadata), object(ClassMetadata))
(/srv/http/sp7/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:245)
at Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata(object(ClassMetadata), object(ClassMetadata), false, array())
(/srv/http/sp7/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:306)
at Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata('App\\DocumentBundle\\Entity\\Document')
(/srv/http/sp7/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:78)
at Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata('App\\DocumentBundle\\Entity\\Document')
(/srv/http/sp7/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:185)
at Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('App\\DocumentBundle\\Entity\\Document')
(/srv/http/sp7/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:91)
at Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getAllMetadata()
(/srv/http/sp7/vendor/mgilet/notification-bundle/NotifiableDiscovery.php:76)
at Mgilet\NotificationBundle\NotifiableDiscovery->discoverNotifiables()
(/srv/http/sp7/vendor/mgilet/notification-bundle/NotifiableDiscovery.php:40)
at Mgilet\NotificationBundle\NotifiableDiscovery->__construct(object(EntityManager), object(CachedReader))
(/srv/http/sp7/var/cache/dev/ContainerSAZB8iG/srcApp_KernelDevDebugContainer.php:6135)
at ContainerSAZB8iG\srcApp_KernelDevDebugContainer->getMgilet_NotificationService()
(/srv/http/sp7/var/cache/dev/ContainerSAZB8iG/srcApp_KernelDevDebugContainer.php:7481)
at ContainerSAZB8iG\srcApp_KernelDevDebugContainer->getTwigService()
(/srv/http/sp7/var/cache/dev/ContainerSAZB8iG/srcApp_KernelDevDebugContainer.php:13406)
at ContainerSAZB8iG\srcApp_KernelDevDebugContainer->getSensioFrameworkExtra_View_ListenerService()
(/srv/http/sp7/var/cache/dev/ContainerSAZB8iG/srcApp_KernelDevDebugContainer.php:4501)
at ContainerSAZB8iG\srcApp_KernelDevDebugContainer->ContainerSAZB8iG\{closure}()
(/srv/http/sp7/vendor/symfony/event-dispatcher/EventDispatcher.php:301)
at Symfony\Component\EventDispatcher\EventDispatcher::Symfony\Component\EventDispatcher\{closure}(object(ControllerEvent), 'kernel.controller', object(EventDispatcher))
(/srv/http/sp7/vendor/symfony/event-dispatcher/EventDispatcher.php:264)
at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(object(Closure), object(Closure), object(Closure), object(Closure), object(Closure), object(Closure)), 'kernel.controller', object(ControllerEvent))
(/srv/http/sp7/vendor/symfony/event-dispatcher/EventDispatcher.php:239)
at Symfony\Component\EventDispatcher\EventDispatcher->callListeners(array(object(Closure), object(Closure), object(Closure), object(Closure), object(Closure), object(Closure)), 'kernel.controller', object(ControllerEvent))
(/srv/http/sp7/vendor/symfony/event-dispatcher/EventDispatcher.php:73)
at Symfony\Component\EventDispatcher\EventDispatcher->dispatch(object(ControllerEvent), 'kernel.controller')
(/srv/http/sp7/vendor/symfony/http-kernel/HttpKernel.php:134)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(/srv/http/sp7/vendor/symfony/http-kernel/HttpKernel.php:68)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(/srv/http/sp7/vendor/symfony/http-kernel/Kernel.php:201)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(/srv/http/sp7/public/index.php:25)
The code itself should store a document and offer the possibility to attach files to the documents. Therefore, I used the Vich/Uploader-Bundle and I suspect that the error somehow is linked to Vich.
Document.php
<?php
namespace App\DocumentBundle\Entity;
use App\Entity\FileAwareEntity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ExecutionContextInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Security;
use App\UserBundle\Entity\User as User;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* #ORM\HasLifecycleCallbacks()
* #ORM\Table(name="document_document", options={"engine"="InnoDB"})
* #ORM\Entity(repositoryClass="App\DocumentBundle\Entity\DocumentRepository")
*/
class Document extends FileAwareEntity {
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #Assert\NotBlank()
* #var string
* #ORM\Column(type="string")
* #Groups({"elastica"})
*/
private $name;
/**
*
* #var string
* #ORM\Column(type="text", nullable=true)
* #Groups({"elastica"})
*/
private $description;
/**
*
* #var string
* #ORM\Column(type="string", nullable=true)
* #Groups({"elastica"})
*/
private $headline;
/**
* #ORM\ManyToOne(targetEntity="App\DocumentBundle\Entity\DocumentType")
* #ORM\JoinColumn(name="type", referencedColumnName="id")
* #ORM\OrderBy({"translationKey" = "ASC"})
* #var \App\DocumentBundle\Entity\DocumentType
* #Groups({"elastica"})
**/
private $type;
/**
* #var string
* #ORM\Column(type="string", nullable=true)
* #Groups({"elastica"})
*/
private $vkaNumber;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $linkedDocument;
/**
* #var string
* #ORM\Column(type="string", nullable=true)
*/
private $linkedDocumentExpires;
/**
* #var boolean
* #ORM\Column(type="boolean", nullable=true)
* #Groups({"elastica"})
*/
private $active;
/**
* #var boolean
* #ORM\Column(type="boolean", nullable=true)
*/
private $status_stealth;
/**
* #var boolean
* #ORM\Column(type="boolean", nullable=true)
*/
private $locked;
/**
* $var DateTime
* #ORM\Column(type="datetime", nullable=true)
* #Groups({"elastica"})
*/
private $created;
// * #Groups({"elastica"})
/**
* #ORM\ManyToOne(targetEntity="App\UserBundle\Entity\User", inversedBy="documentsCreated")
* #ORM\JoinColumn(name="createdBy", referencedColumnName="id")
* #var \App\UserBundle\Entity\User
**/
private $createdBy;
/**
* $var DateTime
* #ORM\Column(type="datetime", nullable=true)
* #Assert\Expression(
* "false == this.getStealthException()",
* message="The effective date must be in the future!")
* #Groups({"elastica"})
*/
private $effective;
/**
* $var DateTime
* #ORM\Column(type="datetime", nullable=true)
* #Assert\Expression(
* "this.getEffective() < this.getExpires()",
* message="The expiration date can't be before the effective date")
*/
private $expires;
/**
* $var DateTime
* #ORM\Column(type="datetime", nullable=true)
*/
private $modified;
/**
* #ORM\ManyToOne(targetEntity="App\UserBundle\Entity\User")
* #ORM\JoinColumn(name="modifiedBy", referencedColumnName="id")
* #var \App\UserBundle\Entity\User
**/
private $modifiedBy;
// * #Groups({"elastica"})
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Agency", inversedBy="documentagency", cascade={"persist"})
* #ORM\JoinTable(name="document_document_agency",
* joinColumns={#ORM\JoinColumn(name="document_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="iata8", referencedColumnName="iata8")})
* #var \App\Entity\Agency
**/
private $agency;
/**
* $var boolean
* #ORM\Column(type="boolean", nullable=true)
*/
private $signature;
/**
* $var boolean
* #ORM\Column(type="boolean", nullable=true)
*/
private $signed;
/**
* #ORM\ManyToOne(targetEntity="App\UserBundle\Entity\User", inversedBy="documentSigned")
* #ORM\JoinColumn(name="signedBy", referencedColumnName="id")
* #var \App\UserBundle\Entity\User
**/
private $signedBy;
/**
* #ORM\OneToMany(targetEntity="App\DocumentBundle\Entity\Comment", mappedBy="document",cascade={"remove"})
* #var \Doctrine\Common\Collections\Collection
**/
protected $comment;
// * #Groups({"elastica"})
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Market", inversedBy="document", cascade={"persist"})
* #ORM\JoinTable(name="document_document_market",
* joinColumns={#ORM\JoinColumn(name="document_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="market_id", referencedColumnName="id")})
* #Assert\Count(
* min = "0"
* )
**/
private $market;
// * #Groups({"elastica"})
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Airline", inversedBy="document", cascade={"persist"})
* #ORM\JoinTable(name="document_document_airline",
* joinColumns={#ORM\JoinColumn(name="document_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="airline_id", referencedColumnName="id")})
* #Assert\Count(
* min = "0"
* )
* #var \App\Entity\Airline
**/
private $airline;
// * #Groups({"elastica"})
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Product", inversedBy="document", cascade={"persist"})
* #ORM\JoinTable(name="document_document_product",
* joinColumns={#ORM\JoinColumn(name="document_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="product_id", referencedColumnName="id")})
* #Assert\Count(
* min = "0"
* )
* #var \App\Entity\Product
**/
private $product;
// * #Groups({"elastica"})
/**
* #ORM\ManyToMany(targetEntity="App\ReferentialBundle\Entity\Channel1", inversedBy="document", cascade={"persist"})
* #ORM\JoinTable(name="document_document_channel",
* joinColumns={#ORM\JoinColumn(name="document_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="channel_id", referencedColumnName="id")})
* #Assert\Count(
* min = "0"
* )
* #var \App\ReferentialBundle\Entity\Channel1
**/
private $channel;
/**
* $var boolean
* #ORM\Column(type="boolean", nullable=true)
*/
private $internalCom;
/**
* #ORM\OneToMany(targetEntity="App\DocumentBundle\File", mappedBy="entity", cascade={"persist", "merge", "remove"}, orphanRemoval=true)
* #Assert\Valid()
* #var \Doctrine\Common\Collections\Collection
**/
protected $file;
/**
* #ORM\ManyToOne(targetEntity="App\DocumentBundle\Entity\Status")
* #ORM\JoinColumn(name="status", referencedColumnName="id", nullable=false)
* #var \App\DocumentBundle\Entity\Status
* #Groups({"elastica"})
*/
private $status;
/**
* #ORM\ManyToMany(targetEntity="App\DocumentBundle\Entity\UploadProfile", inversedBy="documents", cascade={"persist"})
* #ORM\JoinTable(name="document_document_uploadprofile",
* joinColumns={#ORM\JoinColumn(name="document_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="uploadprofile_id", referencedColumnName="id")})
* #var \App\DocumentBundle\Entity\UploadProfile
**/
protected $uploadprofile;
/**
* #ORM\Column(type="decimal", precision=7, scale=7, nullable = true)
*/
protected $esScore;
/**
* Constructor
*/
public function __construct()
{
$this->locked = false;
$this->signed = false;
$this->market = new \Doctrine\Common\Collections\ArrayCollection();
$this->agency = new \Doctrine\Common\Collections\ArrayCollection();
$this->channel = new \Doctrine\Common\Collections\ArrayCollection();
$this->product = new \Doctrine\Common\Collections\ArrayCollection();
}
/* Entity: Functions ************************************************************************/
public function getCurrentDate(){
return new \DateTime();
}
/**
* #ORM\PrePersist
*/
public function prePersist() {
date_default_timezone_set('Europe/Berlin');
if($this->created == null)
{
$this->created = new \DateTime("now");
}
$market = $this->getMarket();
if(count($this->getAgency()) > 0){
foreach($this->getAgency() as $agency) {
if(!$this->market->contains($agency->getMarket())) {
$this->addMarket($agency->getMarket());
}
}
}
}
/**
* #ORM\PreUpdate
*/
public function preUpdate() {
date_default_timezone_set('Europe/Berlin');
if($this->modified == null)
{
$this->modified = new \DateTime("now");
}
$market = $this->getMarket();
if(count($this->getAgency()) > 0){
foreach($this->getAgency() as $agency) {
if(!$this->market->contains($agency->getMarket())) {
$this->addMarket($agency->getMarket());
}
}
}
}
/**
* Get statusStealth
*
* #return boolean
*/
public function getStatusStealth()
{
return $this->status_stealth;
}
public function getStealthException(){
if ($this->getEffective() < $this->getCurrentDate() && $this->getStatusStealth() == true){
return true;
} else{
return false;
}
}
public function show()
{
return array(
"domain" => "document",
"id" => $this->id,
"info" => $this->getHeadline(),
"title" => $this->name . ' - ' . $this->vkaNumber,
"airlines" => $this->getAirline(),
"agencies" => $this->getAgency(),
"markets" => $this->getMarket(),
"vkaNumber" => $this->vkaNumber,
"path" => "documentBundle_view_document",
"pathVariable" => "id",
"pathValue" => $this->id,
"bootstrapColor" => 'primary',
);
}
/* Entity: Functions ************************************************************************/
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Document
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* #param string $description
*
* #return Document
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set headline
*
* #param string $headline
*
* #return Document
*/
public function setHeadline($headline)
{
$this->headline = $headline;
return $this;
}
/**
* Get headline
*
* #return string
*/
public function getHeadline()
{
return $this->headline;
}
/**
* Set vkaNumber
*
* #param string $vkaNumber
*
* #return Document
*/
public function setVkaNumber($vkaNumber)
{
$this->vkaNumber = $vkaNumber;
return $this;
}
/**
* Get vkaNumber
*
* #return string
*/
public function getVkaNumber()
{
return $this->vkaNumber;
}
/**
* Set linkedDocument
*
* #param string $linkedDocument
*
* #return Document
*/
public function setLinkedDocument($linkedDocument)
{
$this->linkedDocument = $linkedDocument;
return $this;
}
/**
* Get linkedDocument
*
* #return string
*/
public function getLinkedDocument()
{
return $this->linkedDocument;
}
/**
* Set linkedDocumentExpires
*
* #param string $linkedDocumentExpires
*
* #return Document
*/
public function setLinkedDocumentExpires($linkedDocumentExpires)
{
$this->linkedDocumentExpires = $linkedDocumentExpires;
return $this;
}
/**
* Get linkedDocumentExpires
*
* #return string
*/
public function getLinkedDocumentExpires()
{
return $this->linkedDocumentExpires;
}
/**
* Set active
*
* #param boolean $active
*
* #return Document
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
/**
* Get active
*
* #return boolean
*/
public function getActive()
{
return $this->active;
}
/**
* Set statusStealth
*
* #param boolean $statusStealth
*
* #return Document
*/
public function setStatusStealth($statusStealth)
{
$this->status_stealth = $statusStealth;
return $this;
}
/**
* Set locked
*
* #param boolean $locked
*
* #return Document
*/
public function setLocked($locked)
{
$this->locked = $locked;
return $this;
}
/**
* Get locked
*
* #return boolean
*/
public function getLocked()
{
return $this->locked;
}
/**
* Set created
*
* #param \DateTime $created
*
* #return Document
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* #return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set effective
*
* #param \DateTime $effective
*
* #return Document
*/
public function setEffective($effective)
{
$this->effective = $effective;
return $this;
}
/**
* Get effective
*
* #return \DateTime
*/
public function getEffective()
{
return $this->effective;
}
/**
* Set expires
*
* #param \DateTime $expires
*
* #return Document
*/
public function setExpires($expires)
{
$this->expires = $expires;
return $this;
}
/**
* Get expires
*
* #return \DateTime
*/
public function getExpires()
{
return $this->expires;
}
/**
* Set modified
*
* #param \DateTime $modified
*
* #return Document
*/
public function setModified($modified)
{
$this->modified = $modified;
return $this;
}
/**
* Get modified
*
* #return \DateTime
*/
public function getModified()
{
return $this->modified;
}
/**
* Set signature
*
* #param boolean $signature
*
* #return Document
*/
public function setSignature($signature)
{
$this->signature = $signature;
return $this;
}
/**
* Get signature
*
* #return boolean
*/
public function getSignature()
{
return $this->signature;
}
/**
* Set signed
*
* #param boolean $signed
*
* #return Document
*/
public function setSigned($signed)
{
$this->signed = $signed;
return $this;
}
/**
* Get signed
*
* #return boolean
*/
public function getSigned()
{
return $this->signed;
}
/**
* Set internalCom
*
* #param boolean $internalCom
*
* #return Document
*/
public function setInternalCom($internalCom)
{
$this->internalCom = $internalCom;
return $this;
}
/**
* Get internalCom
*
* #return boolean
*/
public function getInternalCom()
{
return $this->internalCom;
}
/**
* Set esScore
*
* #param string $esScore
*
* #return Document
*/
public function setEsScore($esScore)
{
$this->esScore = $esScore;
return $this;
}
/**
* Get esScore
*
* #return string
*/
public function getEsScore()
{
return $this->esScore;
}
/**
* Set type
*
* #param \App\DocumentBundle\Entity\DocumentType $type
*
* #return Document
*/
public function setType(\App\DocumentBundle\Entity\DocumentType $type = null)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return \App\DocumentBundle\Entity\DocumentType
*/
public function getType()
{
return $this->type;
}
/**
* Set createdBy
*
* #param \App\UserBundle\Entity\User $createdBy
*
* #return Document
*/
public function setCreatedBy(\App\UserBundle\Entity\User $createdBy = null)
{
$this->createdBy = $createdBy;
return $this;
}
/**
* Get createdBy
*
* #return \App\UserBundle\Entity\User
*/
public function getCreatedBy()
{
return $this->createdBy;
}
/**
* Set modifiedBy
*
* #param \App\UserBundle\Entity\User $modifiedBy
*
* #return Document
*/
public function setModifiedBy(\App\UserBundle\Entity\User $modifiedBy = null)
{
$this->modifiedBy = $modifiedBy;
return $this;
}
/**
* Get modifiedBy
*
* #return \App\UserBundle\Entity\User
*/
public function getModifiedBy()
{
return $this->modifiedBy;
}
/**
* Add agency
*
* #param \App\Entity\Agency $agency
*
* #return Document
*/
public function addAgency(\App\Entity\Agency $agency)
{
$this->agency[] = $agency;
return $this;
}
/**
* Remove agency
*
* #param \App\Entity\Agency $agency
*/
public function removeAgency(\App\Entity\Agency $agency)
{
$this->agency->removeElement($agency);
}
/**
* Get agency
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getAgency()
{
return $this->agency;
}
/**
* Set signedBy
*
* #param \App\UserBundle\Entity\User $signedBy
*
* #return Document
*/
public function setSignedBy(\App\UserBundle\Entity\User $signedBy = null)
{
$this->signedBy = $signedBy;
return $this;
}
/**
* Get signedBy
*
* #return \App\UserBundle\Entity\User
*/
public function getSignedBy()
{
return $this->signedBy;
}
/**
* Add comment
*
* #param \App\DocumentBundle\Entity\Comment $comment
*
* #return Document
*/
public function addComment(\App\DocumentBundle\Entity\Comment $comment)
{
$this->comment[] = $comment;
return $this;
}
/**
* Remove comment
*
* #param \App\DocumentBundle\Entity\Comment $comment
*/
public function removeComment(\App\DocumentBundle\Entity\Comment $comment)
{
$this->comment->removeElement($comment);
}
/**
* Get comment
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getComment()
{
return $this->comment;
}
/**
* Add market
*
* #param \App\Entity\Market $market
*
* #return Document
*/
public function addMarket(\App\Entity\Market $market)
{
$this->market[] = $market;
return $this;
}
/**
* Remove market
*
* #param \App\Entity\Market $market
*/
public function removeMarket(\App\Entity\Market $market)
{
$this->market->removeElement($market);
}
/**
* Get market
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getMarket()
{
return $this->market;
}
/**
* Add airline
*
* #param \App\Entity\Airline $airline
*
* #return Document
*/
public function addAirline(\App\Entity\Airline $airline)
{
$this->airline[] = $airline;
return $this;
}
/**
* Remove airline
*
* #param \App\Entity\Airline $airline
*/
public function removeAirline(\App\Entity\Airline $airline)
{
$this->airline->removeElement($airline);
}
/**
* Get airline
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getAirline()
{
return $this->airline;
}
/**
* Add product
*
* #param \App\Entity\Product $product
*
* #return Document
*/
public function addProduct(\App\Entity\Product $product)
{
$this->product[] = $product;
return $this;
}
/**
* Remove product
*
* #param \App\Entity\Product $product
*/
public function removeProduct(\App\Entity\Product $product)
{
$this->product->removeElement($product);
}
/**
* Get product
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getProduct()
{
return $this->product;
}
/**
* Add channel
*
* #param \App\ReferentialBundle\Entity\Channel1 $channel
*
* #return Document
*/
public function addChannel(\App\ReferentialBundle\Entity\Channel1 $channel)
{
$this->channel[] = $channel;
return $this;
}
/**
* Remove channel
*
* #param \App\ReferentialBundle\Entity\Channel1 $channel
*/
public function removeChannel(\App\ReferentialBundle\Entity\Channel1 $channel)
{
$this->channel->removeElement($channel);
}
/**
* Get channel
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getChannel()
{
return $this->channel;
}
/**
* Get file
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getFile()
{
return $this->file;
}
}
File.php (child class)
<?php
namespace App\DocumentBundle;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\File as BaseFile;
/**
* This class extends the base file class. Only the mapped entity and table name are different.
*
* #ORM\HasLifecycleCallbacks()
* #ORM\Table(name="document_file")
* #ORM\Entity
*/
class File extends BaseFile {
/**
* #ORM\ManyToOne(targetEntity="App\DocumentBundle\Entity\Document", inversedBy="file")
* #ORM\JoinColumn(name="entity", referencedColumnName="id", nullable=true)
* #var \App\DocumentBundle\Entity\Document
*/
protected $entity;
}
I already tried to clean the cache, restart the server, update doctrine or commented the property out, but nothing has worked so far. Has anyone an idea how to ressolve this issue?

PHP error: Call to a member function format() on string

i have a problem, i don't know where is the problem come from. When i save my data to server, i've got error Call to a member function format() on string.
This is my Controller code:
public function newAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_HRM_SALARY_ADJUSTMENT_CREATE');
$corp = $this->getSelectedCorporationEntity();
$entity = new SalaryAdjustment();
$entity->setCorporation($corp);
if ($request->isMethod('POST')) {
$data = json_decode($request->getContent());
dump($data);
//$this->validationBeforeProgress('new', $data, $data->level, '', $data->docType);
$employeeEntity = $this->getDoctrine()->getRepository(Employee::class)->find($data->employee);
$entity->setEmployee($employeeEntity);
$entity->setNumber($data->number);
$entity->setEffectiveDate($data->effectiveDate);
$entity->setReference($data->reference);
$entity->setReferenceNumber($data->referenceNumber);
$entity->setRemarks($data->remarks);
$entity->setApprove(false);
$entity->setVoid(false);
$entity->setCreatedBy($this->getCurrentUser()->getUsername());
$entity->setCreatedDate(new \DateTime());
$em = $this->getEM();
$this->validateEntity($entity);
$em->persist($entity);
$newEntityDetail = $data->detail;
if (count($newEntityDetail) > 0) {
foreach ($newEntityDetail as $item) {
dump($item);
if ($item->newValue > 0 || $item->oldValue > 0){
$entityDetail = new SalaryAdjustmentDetail();
$lookupSalaryEntity = $this->getDoctrine()->getRepository(LookupSalary::class)->find($item->lookupSalaryId);
$entityDetail->setLookupSalary($lookupSalaryEntity);
if ($item->oldEffectiveDate != null) {
$entityDetail->setOldEffectiveDate($item->oldEffectiveDate);
}
$entityDetail->setOldValue($item->oldValue);
$entityDetail->setNewValue($item->newValue);
$entityDetail->setSalaryAdjustment($entity);
$this->validateEntity($entityDetail);
$em->persist($entityDetail);
}
}
} else {
throw new \Exception('Detail must be have minimal 1 row');
}
$em->flush();
}
return new JsonResponse(['data' => 'Done']);
}
the error come from when the code $em->flush(). I feel the problem come from when the code $entityDetail->setOldValue($item->oldValue); and entityDetail->setNewValue($item->newValue); because i'm using decimal type in entity for the first time.
i use 2 entity for save my data, the entity is SalaryAdjustment and SalaryAdjustmentDetail. There is the code:
class SalaryAdjustment
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="SalaryAdjustmentDetail", mappedBy="salaryAdjustment", cascade={"persist", "remove"})
*/
private $salaryAdjustmentDetail;
public function __construct() {
$this->salaryAdjustmentDetail = new ArrayCollection();
}
/**
* #ORM\ManyToOne(targetEntity="Dsp\DspAppsBundle\Entity\HRM\Employee\Employee")
* #ORM\JoinColumn(name="employee_id", referencedColumnName="id", nullable=false)
*/
private $employee;
/**
* #ORM\ManyToOne(targetEntity="Dsp\DspAdministrationBundle\Entity\Corporation")
* #ORM\JoinColumn(name="corporation_id", referencedColumnName="id", nullable=false)
*/
private $corporation;
/**
* #var string
*
* #ORM\Column(name="number", type="string", length=20)
*/
private $number;
/**
* #var \DateTime
*
* #ORM\Column(name="effective_date", type="date")
*/
private $effectiveDate;
/**
* #var string
*
* #ORM\Column(name="reference", type="string", length=50, nullable=true)
*/
private $reference;
/**
* #var string
*
* #ORM\Column(name="reference_number", type="string", length=100, nullable=true)
*/
private $referenceNumber;
/**
* #var string
*
* #ORM\Column(name="remarks", type="string", length=300, nullable=true)
*/
private $remarks;
/**
* #var bool
*
* #ORM\Column(name="approve", type="boolean")
*/
private $approve;
/**
* #var bool
*
* #ORM\Column(name="void", type="boolean")
*/
private $void;
/**
* #var string
*
* #ORM\Column(name="created_by", type="string", length=50)
*/
private $createdBy;
/**
* #var \DateTime
*
* #ORM\Column(name="created_date", type="datetime")
*/
private $createdDate;
/**
* #var string
*
* #ORM\Column(name="modified_by", type="string", length=50, nullable=true)
*/
private $modifiedBy;
/**
* #var \DateTime
*
* #ORM\Column(name="modified_date", type="datetime", nullable=true)
*/
private $modifiedDate;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Add salaryAdjustmentDetail
*
* #param \Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustmentDetail $salaryAdjustmentDetail
*
* #return SalaryAdjustment
*/
public function addSalaryAdjustmentDetail(SalaryAdjustmentDetail $salaryAdjustmentDetail)
{
$this->salaryAdjustmentDetail[] = $salaryAdjustmentDetail;
return $this;
}
/**
* Remove salaryAdjustmentDetail
*
* #param \Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustmentDetail $salaryAdjustmentDetail
*/
public function removeSalaryAdjustmentDetail(\Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustmentDetail $salaryAdjustmentDetail)
{
$this->salaryAdjustmentDetail->removeElement($salaryAdjustmentDetail);
}
/**
* Set employee
*
* #param \Dsp\DspAppsBundle\Entity\HRM\Employee\Employee $employee
*
* #return SalaryAdjustment
*/
public function setEmployee(\Dsp\DspAppsBundle\Entity\HRM\Employee\Employee $employee)
{
$this->employee = $employee;
return $this;
}
/**
* Get employee
*
* #return \Dsp\DspAppsBundle\Entity\HRM\Employee\Employee
*/
public function getEmployee()
{
return $this->employee;
}
/**
* Set corporation
*
* #param \Dsp\DspAdministrationBundle\Entity\Corporation $corporation
*
* #return SalaryAdjustment
*/
public function setCorporation(\Dsp\DspAdministrationBundle\Entity\Corporation $corporation)
{
$this->corporation = $corporation;
return $this;
}
/**
* Get corporation
*
* #return \Dsp\DspAdministrationBundle\Entity\Corporation
*/
public function getCorporation()
{
return $this->corporation;
}
/**
* Set number
*
* #param string $number
*
* #return SalaryAdjustment
*/
public function setNumber($number)
{
$this->number = $number;
return $this;
}
/**
* Get number
*
* #return string
*/
public function getNumber()
{
return $this->number;
}
/**
* Set effectiveDate
*
* #param \DateTime $effectiveDate
*
* #return SalaryAdjustment
*/
public function setEffectiveDate($effectiveDate)
{
$this->effectiveDate = $effectiveDate;
return $this;
}
/**
* Get effectiveDate
*
* #return \DateTime
*/
public function getEffectiveDate()
{
return $this->effectiveDate;
}
/**
* Set reference
*
* #param string $reference
*
* #return SalaryAdjustment
*/
public function setReference($reference)
{
$this->reference = $reference;
return $this;
}
/**
* Get reference
*
* #return string
*/
public function getReference()
{
return $this->reference;
}
/**
* Set referenceNumber
*
* #param string $referenceNumber
*
* #return SalaryAdjustment
*/
public function setReferenceNumber($referenceNumber)
{
$this->referenceNumber = $referenceNumber;
return $this;
}
/**
* Get referenceNumber
*
* #return string
*/
public function getReferenceNumber()
{
return $this->referenceNumber;
}
/**
* Set remarks
*
* #param string $remarks
*
* #return SalaryAdjustment
*/
public function setRemarks($remarks)
{
$this->remarks = $remarks;
return $this;
}
/**
* Get remarks
*
* #return string
*/
public function getRemarks()
{
return $this->remarks;
}
/**
* Set approve
*
* #param boolean $approve
*
* #return SalaryAdjustment
*/
public function setApprove($approve)
{
$this->approve = $approve;
return $this;
}
/**
* Get approve
*
* #return bool
*/
public function getApprove()
{
return $this->approve;
}
/**
* Set void
*
* #param boolean $void
*
* #return SalaryAdjustment
*/
public function setVoid($void)
{
$this->void = $void;
return $this;
}
/**
* Get void
*
* #return bool
*/
public function getVoid()
{
return $this->void;
}
/**
* Set createdBy
*
* #param string $createdBy
*
* #return SalaryAdjustment
*/
public function setCreatedBy($createdBy)
{
$this->createdBy = $createdBy;
return $this;
}
/**
* Get createdBy
*
* #return string
*/
public function getCreatedBy()
{
return $this->createdBy;
}
/**
* Set createdDate
*
* #param \DateTime $createdDate
*
* #return SalaryAdjustment
*/
public function setCreatedDate($createdDate)
{
$this->createdDate = $createdDate;
return $this;
}
/**
* Get createdDate
*
* #return \DateTime
*/
public function getCreatedDate()
{
return $this->createdDate;
}
/**
* Set modifiedBy
*
* #param string $modifiedBy
*
* #return SalaryAdjustment
*/
public function setModifiedBy($modifiedBy)
{
$this->modifiedBy = $modifiedBy;
return $this;
}
/**
* Get modifiedBy
*
* #return string
*/
public function getModifiedBy()
{
return $this->modifiedBy;
}
/**
* Set modifiedDate
*
* #param \DateTime $modifiedDate
*
* #return SalaryAdjustment
*/
public function setModifiedDate($modifiedDate)
{
$this->modifiedDate = $modifiedDate;
return $this;
}
/**
* Get modifiedDate
*
* #return \DateTime
*/
public function getModifiedDate()
{
return $this->modifiedDate;
}
}
And this is my SalaryAdjustmentDetail:
class SalaryAdjustmentDetail
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Dsp\DspAppsBundle\Entity\HRM\Lookup\LookupSalary")
* #ORM\JoinColumn(name="lookup_salary_id", referencedColumnName="id", nullable=false)
*/
private $lookupSalary;
/**
* #ORM\ManyToOne(targetEntity="Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustment", inversedBy="SalaryAdjustmentDetail")
* #ORM\JoinColumn(name="salary_adjustment_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
*/
private $salaryAdjustment;
/**
* #var \DateTime
*
* #ORM\Column(name="old_effective_date", type="date", nullable=true)
*/
private $oldEffectiveDate;
/**
* #var string
*
* #ORM\Column(name="old_value", type="decimal", precision=20, scale=4)
*/
private $oldValue;
/**
* #var string
*
* #ORM\Column(name="new_value", type="decimal", precision=20, scale=4)
*/
private $newValue;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set lookupSalary
*
* #param \Dsp\DspAppsBundle\Entity\HRM\Lookup\LookupSalary $lookupSalary
*
* #return SalaryAdjustmentDetail
*/
public function setLookupSalary(\Dsp\DspAppsBundle\Entity\HRM\Lookup\LookupSalary $lookupSalary)
{
$this->lookupSalary = $lookupSalary;
return $this;
}
/**
* Get lookupSalary
*
* #return \Dsp\DspAppsBundle\Entity\HRM\Lookup\LookupSalary
*/
public function getLookupSalary()
{
return $this->lookupSalary;
}
/**
* Set salaryAdjustment
*
* #param \Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustment $salaryAdjustment
*
* #return SalaryAdjustmentDetail
*/
public function setSalaryAdjustment(\Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustment $salaryAdjustment)
{
$this->salaryAdjustment = $salaryAdjustment;
return $this;
}
/**
* Get salaryAdjustment
*
* #return \Dsp\DspAppsBundle\Entity\HRM\SalaryAdjustment\SalaryAdjustment
*/
public function getSalaryAdjustment()
{
return $this->salaryAdjustment;
}
/**
* Set oldEffectiveDate
*
* #param \DateTime $oldEffectiveDate
*
* #return SalaryAdjustmentDetail
*/
public function setOldEffectiveDate($oldEffectiveDate)
{
$this->oldEffectiveDate = $oldEffectiveDate;
return $this;
}
/**
* Get oldEffectiveDate
*
* #return \DateTime
*/
public function getOldEffectiveDate()
{
return $this->oldEffectiveDate;
}
/**
* Set oldValue
*
* #param string $oldValue
*
* #return SalaryAdjustmentDetail
*/
public function setOldValue($oldValue)
{
$this->oldValue = $oldValue;
return $this;
}
/**
* Get oldValue
*
* #return string
*/
public function getOldValue()
{
return $this->oldValue;
}
/**
* Set newValue
*
* #param string $newValue
*
* #return SalaryAdjustmentDetail
*/
public function setNewValue($newValue)
{
$this->newValue = $newValue;
return $this;
}
/**
* Get newValue
*
* #return string
*/
public function getNewValue()
{
return $this->newValue;
}
}
Please help me for resolve my problem :')

Convert SQL To Doctrine in Symfony

I have three tables and two relations. One table is many to many and the other table is one to many relation. I created a query in mysql but I'm not converting to dql or querybuilder in Symfony.
Sample query is:
SELECT * FROM `resturant` LEFT JOIN `food` ON `resturant`.`id` = `food`.`resturant_id`
WHERE `food`.`name` LIKE "%pizza%"
GROUP BY `resturant`.`name`
food entity in my project
<?php
namespace Food\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Food
*
* #ORM\Table(name="food")
* #ORM\Entity(repositoryClass="Food\AdminBundle\Repository\FoodRepository")
*/
class Food
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="contents", type="string", length=500, nullable=true)
*/
private $contents;
/**
* #var string
*
* #ORM\Column(name="price", type="integer", nullable=true)
*/
private $price;
/**
* #var bool
*
* #ORM\Column(name="approve", type="boolean")
*/
private $approve;
/**
* #var \Food\AdminBundle\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Food\AdminBundle\Entity\Category")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE" )
* })
*/
private $category;
/**
* #var \Food\AdminBundle\Entity\Resturant
*
* #ORM\ManyToOne(targetEntity="Food\AdminBundle\Entity\Resturant")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="resturant_id", referencedColumnName="id", onDelete="CASCADE" )
* })
*/
private $resturant;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Food
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set contents
*
* #param string $contents
*
* #return Food
*/
public function setContents($contents)
{
$this->contents = $contents;
return $this;
}
/**
* Get contents
*
* #return string
*/
public function getContents()
{
return $this->contents;
}
/**
* Set approve
*
* #param boolean $approve
*
* #return Food
*/
public function setApprove($approve)
{
$this->approve = $approve;
return $this;
}
/**
* Get approve
*
* #return bool
*/
public function getApprove()
{
return $this->approve;
}
/**
* Set category
*
* #param \Food\AdminBundle\Entity\Category $category
*
* #return Food
*/
public function setCategory(\Food\AdminBundle\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return \Food\AdminBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
/**
* Set resturant
*
* #param \Food\AdminBundle\Entity\Resturant $resturant
*
* #return Food
*/
public function setResturant(\Food\AdminBundle\Entity\Resturant $resturant = null)
{
$this->resturant = $resturant;
return $this;
}
/**
* Get resturant
*
* #return \Food\AdminBundle\Entity\Resturant
*/
public function getResturant()
{
return $this->resturant;
}
/**
* Set price
*
* #param integer $price
*
* #return Food
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* #return integer
*/
public function getPrice()
{
return $this->price;
}
}
Resturant entity
<?php
namespace Food\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Resturant
*
* #ORM\Table(name="resturant")
* #ORM\Entity(repositoryClass="Food\AdminBundle\Repository\ResturantRepository")
*/
class Resturant
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=50)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="logo", type="string", length=30, nullable=true)
*/
private $logo;
/**
* #var string
*
* #ORM\Column(name="duration", type="string", length=150, nullable=true)
*/
private $duration;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=800, nullable=true)
*/
private $address;
/**
* #var int
*
* #ORM\Column(name="minimal", type="integer")
*/
private $minimal;
/**
* #var int
*
* #ORM\Column(name="delivery", type="integer")
*/
private $delivery;
/**
* #var int
*
* #ORM\Column(name="score", type="integer", nullable=true)
*/
private $score;
/**
* #var int
*
* #ORM\Column(name="cost", type="integer")
*/
private $cost;
/**
* #var int
*
* #ORM\Column(name="startlunch", type="time", nullable=true)
*/
private $startlunch;
/**
* #var int
*
* #ORM\Column(name="endlunch", type="time", nullable=true)
*/
private $endlunch;
/**
* #var int
*
* #ORM\Column(name="startdinner", type="time", nullable=true)
*/
private $startdinner;
/**
* #var int
*
* #ORM\Column(name="enddinner", type="time", nullable=true)
*/
private $enddinner;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Food\AdminBundle\Entity\Zone", inversedBy="Resturant")
* #ORM\JoinTable(name="resturant_has_zone",
* joinColumns={
* #ORM\JoinColumn(name="resturant_id", referencedColumnName="id" )
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="zone_id", referencedColumnName="id" )
* }
* )
*/
private $zone;
/**
* #ORM\OneToMany(targetEntity="Food\AdminBundle\Entity\Food", mappedBy="Resturant")
*/
private $food;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Resturant
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set logo
*
* #param string $logo
*
* #return Resturant
*/
public function setLogo($logo)
{
$this->logo = $logo;
return $this;
}
/**
* Get logo
*
* #return string
*/
public function getLogo()
{
return $this->logo;
}
/**
* Set duration
*
* #param string $duration
*
* #return Resturant
*/
public function setDuration($duration)
{
$this->duration = $duration;
return $this;
}
/**
* Get duration
*
* #return string
*/
public function getDuration()
{
return $this->duration;
}
/**
* Set address
*
* #param string $address
*
* #return Resturant
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set minimal
*
* #param integer $minimal
*
* #return Resturant
*/
public function setMinimal($minimal)
{
$this->minimal = $minimal;
return $this;
}
/**
* Get minimal
*
* #return int
*/
public function getMinimal()
{
return $this->minimal;
}
/**
* Set delivery
*
* #param integer $delivery
*
* #return Resturant
*/
public function setDelivery($delivery)
{
$this->delivery = $delivery;
return $this;
}
/**
* Get delivery
*
* #return int
*/
public function getDelivery()
{
return $this->delivery;
}
/**
* Set score
*
* #param integer $score
*
* #return Resturant
*/
public function setScore($score)
{
$this->score = $score;
return $this;
}
/**
* Get score
*
* #return int
*/
public function getScore()
{
return $this->score;
}
/**
* Set cost
*
* #param integer $cost
*
* #return Resturant
*/
public function setCost($cost)
{
$this->cost = $cost;
return $this;
}
/**
* Get cost
*
* #return int
*/
public function getCost()
{
return $this->cost;
}
/**
* Set startlunch
*
* #param \DateTime $startlunch
*
* #return Resturant
*/
public function setStartlunch($startlunch)
{
$this->startlunch = $startlunch;
return $this;
}
/**
* Get startlunch
*
* #return \DateTime
*/
public function getStartlunch()
{
return $this->startlunch;
}
/**
* Set endlunch
*
* #param \DateTime $endlunch
*
* #return Resturant
*/
public function setEndlunch($endlunch)
{
$this->endlunch = $endlunch;
return $this;
}
/**
* Get endlunch
*
* #return \DateTime
*/
public function getEndlunch()
{
return $this->endlunch;
}
/**
* Set startdinner
*
* #param \DateTime $startdinner
*
* #return Resturant
*/
public function setStartdinner($startdinner)
{
$this->startdinner = $startdinner;
return $this;
}
/**
* Get startdinner
*
* #return \DateTime
*/
public function getStartdinner()
{
return $this->startdinner;
}
/**
* Set enddinner
*
* #param \DateTime $enddinner
*
* #return Resturant
*/
public function setEnddinner($enddinner)
{
$this->enddinner = $enddinner;
return $this;
}
/**
* Get enddinner
*
* #return \DateTime
*/
public function getEnddinner()
{
return $this->enddinner;
}
/**
* Constructor
*/
public function __construct()
{
$this->zone = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add zone
*
* #param \Food\AdminBundle\Entity\Zone $zone
*
* #return Resturant
*/
public function addZone(\Food\AdminBundle\Entity\Zone $zone)
{
$this->zone[] = $zone;
return $this;
}
/**
* Remove zone
*
* #param \Food\AdminBundle\Entity\Zone $zone
*/
public function removeZone(\Food\AdminBundle\Entity\Zone $zone)
{
$this->zone->removeElement($zone);
}
/**
* Get zone
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getZone()
{
return $this->zone;
}
/**
* Add food
*
* #param \Food\AdminBundle\Entity\Food $food
*
* #return Resturant
*/
public function addFood(\Food\AdminBundle\Entity\Food $food)
{
$this->food[] = $food;
return $this;
}
/**
* Remove food
*
* #param \Food\AdminBundle\Entity\Food $food
*/
public function removeFood(\Food\AdminBundle\Entity\Food $food)
{
$this->food->removeElement($food);
}
/**
* Get food
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getFood()
{
return $this->food;
}
/**
* #Assert\File(
* maxSize="2m",
* mimeTypes = {"image/jpeg", "image/png"},
* mimeTypesMessage = "Please upload a valid Image File"
*
* )
*/
private $file;
// Uploading File LOL
/**
* Sets file.
*
* #param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* #return UploadedFile
*/
public function getFile()
{
return $this->file;
}
public function getAbsolutePath()
{
return null === $this->logo
? null
: $this->getUploadRootDir() . '/' . $this->logo;
}
public function getWebPath()
{
return null === $this->picture
? null
: $this->getUploadDir() . '/' . $this->logo;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'upload/resturant/logo';
}
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
// use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and then the
// target filename to move to
$stringArray = explode(".", $this->getFile()->getClientOriginalName());
$suffix = $stringArray[count($stringArray) - 1];
$this->logo = $this->random_string() . '.' . $suffix;
$this->getFile()->move(
$this->getUploadRootDir(),
$this->logo
);
// clean up the file property as you won't need it anymore
$this->file = null;
}
private function random_string($hashstring = null, $randLengh = null)
{
$string = $hashstring;
$randLengh = 15;
if ($string == null) {
$string = 'abcdefghijklmnopqrstuvwxyz0123456789';
}
$charactersLength = strlen($string);
$randomString = '';
for ($i = 0; $i < $randLengh; $i++) {
$randomString .= $string[rand(0, $charactersLength - 1)];
}
return $randomString;
}
public function __toString()
{
return $this->getName();
}
}
and zone entity and relation
<?php
namespace Food\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Zone
*
* #ORM\Table(name="zone")
* #ORM\Entity(repositoryClass="Food\AdminBundle\Repository\ZoneRepository")
*/
class Zone
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=100, unique=true)
*/
private $name;
/**
* #var bool
*
* #ORM\Column(name="approve", type="boolean")
*/
private $approve;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Zone
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set approve
*
* #param boolean $approve
*
* #return Zone
*/
public function setApprove($approve)
{
$this->approve = $approve;
return $this;
}
/**
* Get approve
*
* #return bool
*/
public function getApprove()
{
return $this->approve;
}
public function __toString()
{
return $this->getName();
}
}

Doctrine2: how to get one of the inversed side entities with ManyToOne relationship for a given id

I've two doctrine entities with ManyToOne relationship:
/**
* #ORM\ManyToOne(targetEntity="Iballot\CmsBundle\Entity\PollingStation2", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
* #Expose
*/
private $pollingStation2;
and I'd like get a specific one result for a given PollingStation2 ID and I've try that but doesn't work:
public function getPresidential($polId)
{
$qb = $this->createQueryBuilder('r');
$qb->where('r.pollingStation2 = :polling_station2_id')
->setParameter('polling_station2_id', $polId);
return $qb->getQuery()
->getResult();
}
Entities
namespace Iballot\CmsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* PollingStation2
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Iballot\CmsBundle\Entity\PollingStation2Repository")
*/
class PollingStation2
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="verfierNumber", type="integer", length=255, nullable=true)
*/
protected $verfierNumber;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="verifier_number", type="string", length=255)
*/
private $verifierNumber;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* #ORM\OneToMany(targetEntity="Iballot\CmsBundle\Entity\PollingStation2", mappedBy="pollingStation")
*/
protected $result;
/**
* #ORM\ManyToOne(targetEntity="Iballot\CmsBundle\Entity\Constituency", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
*/
private $constituency;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function __toString() {
return $this->getName();
}
/**
* Set name
*
* #param string $name
*
* #return PollingStation2
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set address
*
* #param string $address
*
* #return PollingStation2
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set verifierNumber
*
* #param integer $verifierNumber
*
* #return PollingStation2
*/
public function setVerifierNumber($verifierNumber)
{
$this->verifierNumber = $verifierNumber;
return $this;
}
/**
* Get verifierNumber
*
* #return integer
*/
public function getVerifierNumber()
{
return $this->verifierNumber;
}
/**
* Set verfierNumber
*
* #param integer $verfierNumber
*
* #return PollingStation2
*/
public function setVerfierNumber($verfierNumber)
{
$this->verfierNumber = $verfierNumber;
return $this;
}
/**
* Get verfierNumber
*
* #return integer
*/
public function getVerfierNumber()
{
return $this->verfierNumber;
}
/**
* Set constituency
*
* #param \Iballot\CmsBundle\Entity\Constituency $constituency
*
* #return PollingStation2
*/
public function setConstituency(\Iballot\CmsBundle\Entity\Constituency $constituency)
{
$this->constituency = $constituency;
return $this;
}
/**
* Get constituency
*
* #return \Iballot\CmsBundle\Entity\Constituency
*/
public function getConstituency()
{
return $this->constituency;
}
/**
* Constructor
*/
public function __construct()
{
$this->result = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add result
*
* #param \Iballot\CmsBundle\Entity\PollingStation2 $result
*
* #return PollingStation2
*/
public function addResult(\Iballot\CmsBundle\Entity\PollingStation2 $result)
{
$this->result[] = $result;
return $this;
}
/**
* Remove result
*
* #param \Iballot\CmsBundle\Entity\PollingStation2 $result
*/
public function removeResult(\Iballot\CmsBundle\Entity\PollingStation2 $result)
{
$this->result->removeElement($result);
}
/**
* Get result
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getResult()
{
return $this->result;
}
}

symfony 2 how to create or map entity in different bundles

I have project which have different bundles. I want to create or divide entities in different bundle how can I create this. I am new in symfony please help me . I created but I can not do mapping correctly . a table which have primary key can't use in another bundle.
Here is my Entity
namespace DomainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* WebsiteDomainConfigTest
*
* #ORM\Table(name="website_domain_config_test", indexes={#ORM\Index(name="fk_website_domain_config_test_1_idx", columns={"vendor_id"}), #ORM\Index(name="fk_website_domain_config_test_2_idx", columns={"country_id"})})
* #ORM\Entity
*/
class WebsiteDomainConfigTest
{
/**
* #var string
*
* #ORM\Column(name="domain", type="string", length=255, nullable=true)
*/
private $domain;
/**
* #var string
*
* #ORM\Column(name="vendor_code", type="string", length=15, nullable=true)
*/
private $vendorCode;
/**
* #var string
*
* #ORM\Column(name="domain_path", type="string", length=255, nullable=true)
*/
private $domainPath;
/**
* #var string
*
* #ORM\Column(name="assets_path", type="string", length=45, nullable=true)
*/
private $assetsPath;
/**
* #var string
*
* #ORM\Column(name="language", type="string", length=3, nullable=true)
*/
private $language;
/**
* #var string
*
* #ORM\Column(name="affiliate", type="string", length=25, nullable=true)
*/
private $affiliate;
/**
* #var string
*
* #ORM\Column(name="affiliate_logo", type="string", length=255, nullable=true)
*/
private $affiliateLogo;
/**
* #var string
*
* #ORM\Column(name="affiliate_address", type="string", length=255, nullable=true)
*/
private $affiliateAddress;
/**
* #var integer
*
* #ORM\Column(name="status", type="integer", nullable=true)
*/
private $status;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \MobileSplash\SplashRequestBundle\Entity\Countries
*
* #ORM\ManyToOne(targetEntity="MobileSplash\SplashRequestBundle\Entity\Countries")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="country_id", referencedColumnName="id")
* })
*/
private $country;
/**
* #var \MobileSplash\SplashRequestBundle\Entity\Vendors
*
* #ORM\ManyToOne(targetEntity="MobileSplash\SplashRequestBundle\Entity\Vendors")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="vendor_id", referencedColumnName="id")
* })
*/
private $vendor;
/**
* Set domain
*
* #param string $domain
* #return WebsiteDomainConfigTest
*/
public function setDomain($domain)
{
$this->domain = $domain;
return $this;
}
/**
* Get domain
*
* #return string
*/
public function getDomain()
{
return $this->domain;
}
/**
* Set vendorCode
*
* #param string $vendorCode
* #return WebsiteDomainConfigTest
*/
public function setVendorCode($vendorCode)
{
$this->vendorCode = $vendorCode;
return $this;
}
/**
* Get vendorCode
*
* #return string
*/
public function getVendorCode()
{
return $this->vendorCode;
}
/**
* Set domainPath
*
* #param string $domainPath
* #return WebsiteDomainConfigTest
*/
public function setDomainPath($domainPath)
{
$this->domainPath = $domainPath;
return $this;
}
/**
* Get domainPath
*
* #return string
*/
public function getDomainPath()
{
return $this->domainPath;
}
/**
* Set assetsPath
*
* #param string $assetsPath
* #return WebsiteDomainConfigTest
*/
public function setAssetsPath($assetsPath)
{
$this->assetsPath = $assetsPath;
return $this;
}
/**
* Get assetsPath
*
* #return string
*/
public function getAssetsPath()
{
return $this->assetsPath;
}
/**
* Set language
*
* #param string $language
* #return WebsiteDomainConfigTest
*/
public function setLanguage($language)
{
$this->language = $language;
return $this;
}
/**
* Get language
*
* #return string
*/
public function getLanguage()
{
return $this->language;
}
/**
* Set affiliate
*
* #param string $affiliate
* #return WebsiteDomainConfigTest
*/
public function setAffiliate($affiliate)
{
$this->affiliate = $affiliate;
return $this;
}
/**
* Get affiliate
*
* #return string
*/
public function getAffiliate()
{
return $this->affiliate;
}
/**
* Set affiliateLogo
*
* #param string $affiliateLogo
* #return WebsiteDomainConfigTest
*/
public function setAffiliateLogo($affiliateLogo)
{
$this->affiliateLogo = $affiliateLogo;
return $this;
}
/**
* Get affiliateLogo
*
* #return string
*/
public function getAffiliateLogo()
{
return $this->affiliateLogo;
}
/**
* Set affiliateAddress
*
* #param string $affiliateAddress
* #return WebsiteDomainConfigTest
*/
public function setAffiliateAddress($affiliateAddress)
{
$this->affiliateAddress = $affiliateAddress;
return $this;
}
/**
* Get affiliateAddress
*
* #return string
*/
public function getAffiliateAddress()
{
return $this->affiliateAddress;
}
/**
* Set status
*
* #param integer $status
* #return WebsiteDomainConfigTest
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return integer
*/
public function getStatus()
{
return $this->status;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set country
*
* #param \MobileSplash\SplashRequestBundle\Entity\Countries $country
* #return WebsiteDomainConfigTest
*/
public function setCountry(\MobileSplash\SplashRequestBundle\Entity\Countries $country = null)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* #return \MobileSplash\SplashRequestBundle\Entity\Countries
*/
public function getCountry()
{
return $this->country;
}
/**
* Set vendor
*
* #param \MobileSplash\SplashRequestBundle\Entity\Vendors $vendor
* #return WebsiteDomainConfigTest
*/
public function setVendor(\MobileSplash\SplashRequestBundle\Entity\Vendors $vendor = null)
{
$this->vendor = $vendor;
return $this;
}
/**
* Get vendor
*
* #return \MobileSplash\SplashRequestBundle\Entity\Vendors
*/
public function getVendor()
{
return $this->vendor;
}
}
in your annotation use namespace like this:
targetEntity="\Mj\FooBundle\Entity\Foo"

Categories