How do I convert Doctrine Annotation Information To Array? - php

I have the following inventory entity object for Doctrine that I created for use in Symfony 3.0.
Simply put how do I get access to the information that I put in the entity via annotations?
For example, companyID, has a ManyToOne annotation that references inversedBy="location". This particular information is very useful to me so I can tell if its a child relationship by foreign key or parent relationship.
If I can get the information about the entity that I described via annotations somehow in an array with Doctrine that would be great. Is this possible to do? Essentially I'm looking for introspection functions on the entity.
<?php
namespace AppBundle\Entity;
use Gedmo\Translatable\Translatable;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Inventory
*
* #ORM\Table(name="distribution_inventory")
* #ORM\Entity(repositoryClass="AppBundle\Repository\InventoryRepository")
*/
class Inventory implements Translatable {
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
* #Gedmo\Translatable
* #ORM\Column(name="name", type="string", length=255, unique=true)
*/
private $name;
/**
* #var string
* #Gedmo\Slug(fields={"name","id"},suffix=".html")
* #ORM\Column(name="inventoryslug", type="string", length=255, nullable=false, nullable=true)
*/
private $inventoryslug;
/**
* #var string
*
* #ORM\Column(name="barcode", type="string", length=255, nullable=true)
*/
private $barcode;
/**
* #var string
* #Gedmo\Translatable
* #ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* #var string
* #ORM\Column(name="imagename", type="string", nullable=true)
*/
private $imagename;
/**
* #Gedmo\Locale
* Used locale to override Translation listener`s locale
* this is not a mapped field of entity metadata, just a simple property
*/
private $locale;
/**
* #var \AppBundle\Entity\InventoryCategory
*
* #ORM\ManyToOne(targetEntity="\AppBundle\Entity\InventoryCategory")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="categoryid", referencedColumnName="id")
* })
*/
private $category;
/**
* #var \AppBundle\Entity\Company
* #Assert\Type(type="\AppBundle\Entity\Company")
* #Assert\Valid()
* #ORM\ManyToOne(targetEntity="\AppBundle\Entity\Company", inversedBy="Location")
* #ORM\JoinColumn(name="companyid", referencedColumnName="id", onDelete="CASCADE")
*/
protected $companyId;
/**
* #var \DateTime $created
*
* #Gedmo\Timestampable(on="create")
* #ORM\Column(type="datetime")
*/
private $created;
/**
* #var \DateTime $updated
*
* #Gedmo\Timestampable(on="update")
* #ORM\Column(type="datetime")
*/
private $updated;
/**
* #var string
* #ORM\Column(name="defaultsellprice",precision=14, scale=2, nullable=true)
*/
private $defaultsellprice;
/**
* #var boolean
*
* #ORM\Column(name="onwaycount", type="integer", nullable=false)
*/
private $onwaycount;
/**
* #var boolean
*
* #ORM\Column(name="instorecount", type="integer", nullable=false)
*/
private $instorecount;
/**
* #var boolean
*
* #ORM\Column(name="wayoutcount", type="integer", nullable=false)
*/
private $wayoutcount;
/**
* #var boolean
*
* #ORM\Column(name="instore", type="string", length=10, nullable=false)
*/
private $instore;
/**
* #var string
*
* #ORM\Column(name="isarchived", type="string", length=5, nullable=false,options={"default":false})
*/
private $isarchived;
/**
* #var string
*
* #ORM\Column(name="archivestatus", type="string", length=5, nullable=false,options={"default":true})
*/
private $archivestatus;
function __construct() {
$this->onwaycount=0;
$this->instore=FALSE;
$this->instorecount=0;
$this->wayoutcount=0;
}
/**
* Get id
*
* #return int
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Inventory
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName() {
return $this->name;
}
/**
* Set barcode
*
* #param string $barcode
*
* #return Inventory
*/
public function setBarcode($barcode) {
$this->barcode = $barcode;
return $this;
}
/**
* Get barcode
*
* #return string
*/
public function getBarcode() {
return $this->barcode;
}
/**
* Set description
*
* #param string $description
*
* #return Inventory
*/
public function setDescription($description) {
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription() {
return $this->description;
}
public function getImagename() {
return $this->imagename;
}
public function getCategory() {
return $this->category;
}
public function getCompanyId() {
return $this->companyId;
}
public function getCreated() {
return $this->created;
}
public function getUpdated() {
return $this->updated;
}
public function getOnwaycount() {
return $this->onwaycount;
}
public function getInstorecount() {
return $this->instorecount;
}
public function getWayoutcount() {
return $this->wayoutcount;
}
public function getInstore() {
return $this->instore;
}
public function setImagename($imagename) {
$this->imagename = $imagename;
return $this;
}
public function setCategory(\AppBundle\Entity\InventoryCategory $category) {
$this->category = $category;
return $this;
}
public function setCompanyId(\AppBundle\Entity\Company $companyId) {
$this->companyId = $companyId;
return $this;
}
public function setCreated(\DateTime $created) {
$this->created = $created;
return $this;
}
public function setUpdated(\DateTime $updated) {
$this->updated = $updated;
return $this;
}
public function setOnwaycount($onwaycount) {
$this->onwaycount = $onwaycount;
return $this;
}
public function setInstorecount($instorecount) {
$this->instorecount = $instorecount;
return $this;
}
public function setWayoutcount($wayoutcount) {
$this->wayoutcount = $wayoutcount;
return $this;
}
public function setInstore($instore) {
$this->instore = $instore;
return $this;
}
public function getDefaultsellprice() {
return $this->defaultsellprice;
}
public function setDefaultsellprice($defaultsellprice) {
$this->defaultsellprice = $defaultsellprice;
return $this;
}
public function getInventoryslug() {
return $this->inventoryslug;
}
public function setInventoryslug($inventoryslug) {
$this->inventoryslug = $inventoryslug;
return $this;
}
public function setTranslatableLocale($locale) {
$this->locale = $locale;
}
public function getIsarchived() {
return $this->isarchived;
}
public function getArchivestatus() {
return $this->archivestatus;
}
public function setIsarchived($isarchived) {
$this->isarchived = $isarchived;
return $this;
}
public function setArchivestatus($archivestatus) {
$this->archivestatus = $archivestatus;
return $this;
}
}
Found this not sure if it will help though (http://tocacar.com/2013/01/25/doctrine2-object-introspection/)

To get an array of metadata:
$cmf = $em->getMetadataFactory();
$metadata = $cmf->getMetadataFor(\AppBundle\Entity\Inventory::class);
//Doctrine\ORM\Mapping\ClassMetadata instance
//as array:
$metadata = (array) $metadata;
To get the inversed information:
$metadata->getAssociationMapping('companyId')['inversedBy'];
//as array
$metadata['associationMappings']['companyId']['inversedBy'];
You can find more info on the docs.

Related

Relation One to Many return empty object

I have a Evaluation entity which has one Product and Product which can have several Evaluations. I'm trying to fetch one Product and to get the list of Evaluations associated with my entity
Produit.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use phpDocumentor\Reflection\Types\This;
/**
* Produit
*
* #ORM\Table(name="produit", indexes={#ORM\Index(name="fk_idcatedel", columns={"idCategorie"})})
* #ORM\Entity
*/
class Produit
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string|null
*
* #ORM\Column(name="libelle", type="string", length=20, nullable=true)
*/
private $libelle;
/**
* #var float|null
*
* #ORM\Column(name="prix", type="float", precision=10, scale=0, nullable=true)
*/
private $prix;
/**
* #var string|null
*
* #ORM\Column(name="description", type="string", length=50, nullable=true)
*/
private $description;
/**
* #var int
*
* #ORM\Column(name="qt", type="integer", nullable=false)
*/
private $qt;
/**
* #var string|null
*
* #ORM\Column(name="img", type="string", length=255, nullable=true)
*/
private $img;
/**
* #var \Categorie
*
* #ORM\ManyToOne(targetEntity="Categorie")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="idCategorie", referencedColumnName="id")
* })
*/
private $idcategorie;
/**
* #ORM\OneToMany(targetEntity="Evaluation", mappedBy="idProduit")
*/
private $rates;
public function __construct()
{
$this->rates = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLibelle(): ?string
{
return $this->libelle;
}
public function setLibelle(?string $libelle): self
{
$this->libelle = $libelle;
return $this;
}
public function getPrix(): ?float
{
return $this->prix;
}
public function setPrix(?float $prix): self
{
$this->prix = $prix;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getQt(): ?int
{
return $this->qt;
}
public function setQt(int $qt): self
{
$this->qt = $qt;
return $this;
}
public function getImg(): ?string
{
return $this->img;
}
public function setImg(?string $img): self
{
$this->img = $img;
return $this;
}
public function getIdcategorie(): ?Categorie
{
return $this->idcategorie;
}
public function setIdcategorie(?Categorie $idcategorie): self
{
$this->idcategorie = $idcategorie;
return $this;
}
/**
* #return Collection|Evaluation[]
*/
public function getRates(): Collection
{
return $this->rates;
}
}
Evaluation.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Evaluation
*
* #ORM\Table(name="evaluation", indexes={#ORM\Index(name="fk_idprodevaldel", columns={"id_produit"}), #ORM\Index(name="fk_iduser", columns={"id_user"})})
* #ORM\Entity
*/
class Evaluation
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var int
*
* #ORM\Column(name="note", type="integer", nullable=false)
*/
private $note;
/**
* #var \Produit
*
* #ORM\ManyToOne(targetEntity="Produit", inversedBy="rates")
* #ORM\JoinColumn(name="id_produit", referencedColumnName="id")
*/
private $idProduit;
/**
* #var \Compte
*
* #ORM\ManyToOne(targetEntity="Compte")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_user", referencedColumnName="email")
* })
*/
private $idUser;
public function getId(): ?int
{
return $this->id;
}
public function getNote(): ?int
{
return $this->note;
}
public function setNote(int $note): self
{
$this->note = $note;
return $this;
}
public function getIdProduit(): ?Produit
{
return $this->idProduit;
}
public function setIdProduit(?Produit $idProduit): self
{
$this->idProduit = $idProduit;
return $this;
}
public function getIdUser(): ?Compte
{
return $this->idUser;
}
public function setIdUser(?Compte $idUser): self
{
$this->idUser = $idUser;
return $this;
}
}
The database
In my controller I succeed to get informations from the products but rates are empty
$produits = $this->getDoctrine()
->getRepository(Produit::class)
->find(1);
dump($produits);
$rates = $produits->getRates();
dump($rates); // #collection: ArrayCollection is empty
The Output :
The collection is not yet initialized due to lazy loading, and rightfully so. If you don't access at least to an element in the collection, it's pointless to load the whole collection because doctrine can safely assume you'll "discard" it. As soon as you access an element (either by looping onto collection or getting a specific element), the collection will be initialized and you have all items.
Another way is to use an EAGER fetch that will load the whole collection in the hydration phase. I would not reccomend it however, unless you're sure that everytime you load a Produit, you need this collection "ready". Even in the latter case, I would handle the collection "manually" as I recommend not to lose control on it (let's pretend you have A LOT of element inside it).
Read more about proxies and association, here

Doctrine : how to set value to null instead entity

I have a problem with my Doctrine Entity.
In my module, I have a history to list actions performed by users.
During the process, I use a ManyToOne relation of my Entity towards herself.
When I update an item in my database, I use "setGain()" to refer this result to another, but if I want to cancel an action from user I need to set as null the value of setGain(), but Doctrine doesn't accept it and return :
PHP Catchable fatal error: Argument 1 passed to Paris\Entity\Historique::setGain() must be an instance of Paris\Entity\Historique, null given
My Entity is :
<?php
namespace Paris\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Historique
*
* #ORM\Table(name="paris__historique")
* #ORM\Entity
*/
class Historique {
/**
* #var integer
*
* #ORM\Column(name="id_historique", type="integer", precision=0, scale=0, nullable=false, unique=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id_historique;
/**
* #var integer
*
* #ORM\Column(name="id_jol", type="integer", length=11, precision=0, scale=0, nullable=true, unique=false)
*/
private $id_jol;
/**
* #var string
*
* #ORM\Column(name="action", type="string", length=50, precision=0, scale=0, nullable=false, unique=false)
*/
private $action;
/**
* #var string
*
* #ORM\Column(name="detail", type="text", precision=0, scale=0, nullable=true, unique=false)
*/
private $detail;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="\Paris\Entity\Historique")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_gain", referencedColumnName="id_historique", nullable=true)
* })
*/
private $id_gain;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="\Paris\Entity\Parieur")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_participant", referencedColumnName="id_participant", nullable=true)
* })
*/
private $id_participant;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="\Paris\Entity\Concours")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_concours", referencedColumnName="id_concours", nullable=true)
* })
*/
private $id_concours;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="\Paris\Entity\Periode")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_periode", referencedColumnName="id_periode", nullable=true)
* })
*/
private $id_periode;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="\Paris\Entity\Paris")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_paris", referencedColumnName="id_paris", nullable=true)
* })
*/
private $id_paris;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="\Paris\Entity\Mise")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_mise", referencedColumnName="id_mise", nullable=true)
* })
*/
private $id_mise;
/**
* #var integer
*
* #ORM\Column(name="date_historique", type="integer", length=11, precision=0, scale=0, nullable=false, unique=false)
*/
private $date_historique;
public function getIdHistorique() {
return $this->id_historique;
}
public function setIdJol($idJol) {
$this->id_jol = $idJol;
return $this;
}
public function getIdJol() {
return $this->id_jol;
}
public function setAction($action) {
$this->action = $action;
return $this;
}
public function getAction() {
return $this->action;
}
public function getGain() {
return $this->id_gain;
}
public function setGain(\Paris\Entity\Historique $id_gain) {
$this->id_gain = $id_gain;
return $this;
}
public function setDetail($detail) {
$this->detail = $detail;
return $this;
}
public function getDetail() {
return $this->detail;
}
public function setParieur(\Paris\Entity\Parieur $id_participant) {
$this->id_participant = $id_participant;
return $this;
}
public function getParieur() {
return $this->id_participant;
}
public function setConcours(\Paris\Entity\Concours $id_concours) {
$this->id_concours = $id_concours;
return $this;
}
public function getConcours() {
return $this->id_concours;
}
public function setPeriode(\Paris\Entity\Periode $id_periode) {
$this->id_periode = $id_periode;
return $this;
}
public function getPeriode() {
return $this->id_periode;
}
public function setPari(\Paris\Entity\Paris $id_paris) {
$this->id_paris = $id_paris;
return $this;
}
public function getPari() {
return $this->id_paris;
}
public function setmise(\Paris\Entity\Mise $id_mise) {
$this->id_mise = $id_mise;
return $this;
}
public function getMise() {
return $this->id_mise;
}
public function setDate($dateHistorique) {
$this->date_historique = $dateHistorique;
return $this;
}
public function getDate() {
return $this->date_historique;
}
}
In my database, the associated field is correctly set to Null by default and Nullable.
Can you help me ? ^^
Thank you in advance for your answer.
Define setGain as:
public function setGain(?\Paris\Entity\Historique $id_gain) {
$this->id_gain = $id_gain;
return $this;
}
? in front of class typehint allows to pass null as an argument.
Another option is:
public function setGain(\Paris\Entity\Historique $id_gain = null) {
$this->id_gain = $id_gain;
return $this;
}

Failed to create object. Many-To-Many relation

I have a problem with my code. It's about a restaurant. I have two objects with a Many-To-Many relation: Products and Days. When I try to create a Day, I receive this error: "Failed to create object: App\Entity\Day\Day". Where am I wrong?
I put the code for entities here. If you need more just tell me. I don't think that it is from admin part.
Here is the Product entity:
<?php
namespace App\Entity\Product;
use App\Entity\Category\Category;
use App\Entity\Day\Day;
use App\Entity\ProductEntry\ProductEntry;
use App\Entity\Restaurant\Restaurant;
use App\Entity\Schedule\Schedule;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
/**
* Class Product
* #ORM\Entity(repositoryClass="App\Repository\Product\ProductRepository")
* #ORM\Table(name="products")
* #package App\Entity\Product
*/
class Product
{
/**
* #var int
*
* #ORM\Id
* #ORM\Column(name="id", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=255, nullable=true)
*/
private $description;
/**
* #var \DateTime
*
* #ORM\Column(name="added_date", type="datetime", nullable=false)
*/
private $addedDate;
/**
* #var boolean
*
* #ORM\Column(name="availability", type="boolean", nullable=true)
*/
private $availability;
/**
* #var Category
*
* #ORM\ManyToOne(targetEntity="App\Entity\Category\Category", inversedBy="products", cascade={"persist"})
* #ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
*/
private $category;
/**
* #var float
*
* #ORM\Column(name="price", type="float", length=255, nullable=true)
*/
private $price;
/**
* #var ProductEntry
*
* #ORM\OneToMany(targetEntity="App\Entity\ProductEntry\ProductEntry", mappedBy="originalProduct", cascade={"persist"})
* #Serializer\Exclude()
*/
private $productsEntries;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Schedule\Schedule", inversedBy="products", cascade={"persist"})
* #ORM\JoinColumn(name="schedule_id", referencedColumnName="id", onDelete="CASCADE")
*
* #var Schedule
*/
private $schedule;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Restaurant\Restaurant", inversedBy="products", cascade={"persist"})
* #ORM\JoinColumn(name="restaurant_id", referencedColumnName="id", onDelete="CASCADE")
*
* #var Restaurant
*/
private $restaurant;
/**
* #var Day
*
* #ORM\ManyToMany(targetEntity="App\Entity\Day\Day", mappedBy="products", cascade={"persist"})
*/
private $days;
public function __toString()
{
return $this->name ?: "";
}
public function __construct()
{
$this->addedDate = new \DateTime();
$this->days = new ArrayCollection();
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #param int $id
*/
public function setId(int $id)
{
$this->id = $id;
}
/**
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* #param string $name
*/
public function setName(string $name)
{
$this->name = $name;
}
/**
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* #param string $description
*/
public function setDescription(string $description)
{
$this->description = $description;
}
/**
* #return mixed
*/
public function getAddedDate()
{
return $this->addedDate;
}
/**
* #param mixed $addedDate
*/
public function setAddedDate($addedDate)
{
$this->addedDate = $addedDate;
}
/**
* #return bool
*/
public function isAvailability()
{
return $this->availability;
}
/**
* #param bool $availability
*/
public function setAvailability($availability)
{
$this->availability = $availability;
}
/**
* #return float
*/
public function getPrice()
{
return $this->price;
}
/**
* #param float $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* #return Category
*/
public function getCategory()
{
return $this->category;
}
/**
* #param Category $category
*/
public function setCategory($category): void
{
$this->category = $category;
}
/**
* #return ProductEntry
*/
public function getProductsEntries()
{
return $this->productsEntries;
}
/**
* #param ProductEntry $productsEntries
*/
public function setProductsEntries(ProductEntry $productsEntries): void
{
$this->productsEntries = $productsEntries;
}
/**
* #return Schedule
*/
public function getSchedule()
{
return $this->schedule;
}
/**
* #param Schedule $schedule
*/
public function setSchedule(Schedule $schedule): void
{
$this->schedule = $schedule;
}
/**
* #return Restaurant
*/
public function getRestaurant()
{
return $this->restaurant;
}
/**
* #param Restaurant $restaurant
*/
public function setRestaurant(Restaurant $restaurant): void
{
$this->restaurant = $restaurant;
}
/**
* #return Day
*/
public function getDays()
{
return $this->days;
}
/**
* #param Day $days
*/
public function setDays(Day $days): void
{
$this->days = $days;
}
}
Here is Day entity:
<?php
namespace App\Entity\Day;
use App\Entity\Product\Product;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Day
* #package App\Entity\Day
* #ORM\Entity(repositoryClass="App\Repository\Day\DayRepository")
* #ORM\Table(name="days")
*/
class Day
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(name="id", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", nullable=false)
*/
private $name;
/**
* #var \DateTime
*
* #ORM\Column(name="current_date", type="datetime", nullable=false)
*/
private $currentDate;
/**
* #var \DateTime
*
* #ORM\Column(name="max_date", type="datetime", nullable=true)
*/
private $maxDate;
/**
* #var Product
*
* #ORM\ManyToMany(targetEntity="App\Entity\Product\Product", inversedBy="days", cascade={"persist"})
* #ORM\JoinTable(name="product_day",
* joinColumns={#ORM\JoinColumn(name="day_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="product_id", referencedColumnName="id")})
*/
private $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* #return int
*/
public function getId(): int
{
return $this->id;
}
/**
* #param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* #param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* #return \DateTime
*/
public function getCurrentDate()
{
return $this->currentDate;
}
/**
* #param \DateTime $currentDate
*/
public function setCurrentDate(\DateTime $currentDate): void
{
$this->currentDate = $currentDate;
}
/**
* #return \DateTime
*/
public function getMaxDate()
{
return $this->maxDate;
}
/**
* #param \DateTime $maxDate
*/
public function setMaxDate(\DateTime $maxDate): void
{
$this->maxDate = $maxDate;
}
/**
* #return Product
*/
public function getProducts()
{
return $this->products;
}
/**
* #param Product $products
*/
public function setProducts(Product $products): void
{
$this->products = $products;
}
}
It seems that you created a column for your Day Entity that is called current_date
/**
* #var \DateTime
*
* #ORM\Column(name="current_date", type="datetime", nullable=false)
*/
private $currentDate;
If you are using MySQL, current_date is a reserved word. Please check https://dev.mysql.com/doc/refman/5.5/en/keywords.html for further reference.
Like Vincent said in comment, your namespaces are fishy...
Unless you really placed them in a sub-folder, it should be namespace App\Entity
In Product entity you can shorten you JoinColumn for some parameters.
Unless you really need something specific, you don't need to write that much. Symfony have 'default' values.
Also, you don't add #var on a mapping. Symfony will take is as a parameter only instead of mapping.
/**
* #var Category
*
* #ORM\ManyToOne(targetEntity="App\Entity\Category\Category", inversedBy="products", cascade={"persist"})
* #ORM\JoinColumn(onDelete="CASCADE", nullable=true)
*/
private $category;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Schedule\Schedule", inversedBy="products", cascade={"persist"})
* #ORM\JoinColumn(onDelete="CASCADE")
*/
private $schedule;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Restaurant\Restaurant", inversedBy="products", cascade={"persist"})
* #ORM\JoinColumn(onDelete="CASCADE")
*/
private $restaurant;
The same apply to your Day entity.
/**
* #var Product
*
* #ORM\ManyToMany(targetEntity="App\Entity\Product\Product", inversedBy="days", cascade={"persist"})
* #ORM\JoinTable(name="product_day")
*/
private $products;
Doing so will prevent you to make some smalls mapping errors.
Try to correct those points first, then let us know if you still have your problem.

Why my symfony entity is not managed?

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

Symfony2 wrong instance passed with twig extension

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

Categories