我有名为Sections的实体,他们有:
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="sections")
*/
protected $products;
名为Product的实体,其中包含:
/**
* @ORM\OneToMany(targetEntity="Price", mappedBy="product")
*/
protected $price;
现在我用:
$sectionsarray = $this->getDoctrine()
->getRepository('AsortBundle:Sections')
->createQueryBuilder('e')
->leftJoin('e.products', 'p')
->leftJoin('e.colors', 'c')
->select('e, p, c')
->orderBy('e.sequence', 'asc')
->addOrderBy('p.kolejnosc', 'asc')
->addOrderBy('c.sequence', 'asc')
->getQuery()
->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
我如何加入这些产品的价格?价格是数组,因为它有四种货币。
答案 0 :(得分:0)
实体/ Product.php
/**
* @ORM\ManyToOne(targetEntity="Price", inversedBy="products")
* @ORM\JoinColumn(name="price_id", referencedColumnName="id")
*/
private $price;
实体/ Price.php
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="price")
*/
private $products;
存储库/ SectionsRpository.php
$sectionsarray = $this->getDoctrine()
->getRepository('AsortBundle:Sections')
->createQueryBuilder('e')
->leftJoin('e.products', 'p')
**->leftJoin('p.price', 'pr')**
->leftJoin('e.colors', 'c')
->select('e, p, c, pr')
->orderBy('e.sequence', 'asc')
->addOrderBy('p.kolejnosc', 'asc')
->addOrderBy('c.sequence', 'asc')
->getQuery()
->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
答案 1 :(得分:0)
您需要添加另一个联接:
$sectionsarray = $this->getDoctrine()
->getRepository('AsortBundle:Sections')
->createQueryBuilder('e')
->select('e, p, c, prices')
->leftJoin('e.products', 'p')
->leftJoin('p.prices', 'prices')
->leftJoin('e.colors', 'c')
->orderBy('e.sequence', 'asc')
->addOrderBy('p.kolejnosc', 'asc')
->addOrderBy('c.sequence', 'asc')
->getQuery()
->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
我使用的是价格而不是价格,因为代表一对多的字段应该是复数形式。
答案 2 :(得分:0)
可能不是我的意思
我的产品实体:
<?
namespace AsortBundle\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Translatable;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="asortyment")
*/
class Product implements Translatable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Gedmo\Translatable
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $name;
/**
* @Gedmo\Translatable
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $img;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $rozmiar;
/**
* @ORM\Column(type="integer")
*/
protected $dostepnosc = 1;
/**
* @ORM\Column(type="integer")
*/
protected $kolejnosc;
/**
* @ORM\Column(type="bigint", length=13)
*/
protected $kodog;
/**
* @ORM\Column(type="bigint", length=13)
*/
protected $kodsz;
/**
* @ORM\Column(type="bigint", length=13, nullable=true)
*/
protected $parent;
/**
* @ORM\Column(type="integer", nullable=true)
*/
protected $min;
/**
* @ORM\Column(type="integer", nullable=true)
*/
protected $opt;
/**
* @ORM\OneToMany(targetEntity="Price", mappedBy="product")
*/
protected $price;
/**
* @Gedmo\Translatable
* @ORM\Column(type="text", nullable=true)
*/
protected $description;
/**
* @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;
/**
* @ORM\ManyToOne(targetEntity="Sections", inversedBy="products")
* @ORM\JoinColumn(name="section_id", referencedColumnName="id")
*/
protected $sections;
/**
* @ORM\ManyToOne(targetEntity="Sections", inversedBy="lazyproducts")
* @ORM\JoinColumn(name="section_id", referencedColumnName="id")
*/
//protected $sections2;
/**
* @ORM\ManyToOne(targetEntity="Colors")
* @ORM\JoinColumn(name="color_id", referencedColumnName="id")
*/
protected $color;
/**
* @ORM\OneToOne(targetEntity="Magazyn")
* @ORM\JoinColumn(name="magazyn_id", referencedColumnName="id", nullable=true)
**/
private $magazyn;
public function __construct() {
$this->price = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Product
*/
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 Product
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
/**
* Set img
*
* @param string $img
* @return Product
*/
public function setImg($img)
{
$this->img = $img;
return $this;
}
/**
* Get img
*
* @return string
*/
public function getImg()
{
return $this->img;
}
/**
* Set rozmiar
*
* @param string $rozmiar
* @return Product
*/
public function setRozmiar($rozmiar)
{
$this->rozmiar = $rozmiar;
return $this;
}
/**
* Get rozmiar
*
* @return string
*/
public function getRozmiar()
{
return $this->rozmiar;
}
/**
* Set dostepnosc
*
* @param integer $dostepnosc
* @return Product
*/
public function setDostepnosc($dostepnosc)
{
$this->dostepnosc = $dostepnosc;
return $this;
}
/**
* Get dostepnosc
*
* @return integer
*/
public function getDostepnosc()
{
return $this->dostepnosc;
}
/**
* Set kolejnosc
*
* @param integer $kolejnosc
* @return Product
*/
public function setKolejnosc($kolejnosc)
{
$this->kolejnosc = $kolejnosc;
return $this;
}
/**
* Get kolejnosc
*
* @return integer
*/
public function getKolejnosc()
{
return $this->kolejnosc;
}
/**
* Set kodog
*
* @param integer $kodog
* @return Product
*/
public function setKodog($kodog)
{
$this->kodog = $kodog;
return $this;
}
/**
* Get kodog
*
* @return integer
*/
public function getKodog()
{
return $this->kodog;
}
/**
* Set kodsz
*
* @param integer $kodsz
* @return Product
*/
public function setKodsz($kodsz)
{
$this->kodsz = $kodsz;
return $this;
}
/**
* Get kodsz
*
* @return integer
*/
public function getKodsz()
{
return $this->kodsz;
}
/**
* Set parent
*
* @param integer $parent
* @return Product
*/
public function setParent($parent)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return integer
*/
public function getParent()
{
return $this->parent;
}
/**
* Set min
*
* @param integer $min
* @return Product
*/
public function setMin($min)
{
$this->min = $min;
return $this;
}
/**
* Get min
*
* @return integer
*/
public function getMin()
{
return $this->min;
}
/**
* Set opt
*
* @param integer $opt
* @return Product
*/
public function setOpt($opt)
{
$this->opt = $opt;
return $this;
}
/**
* Get opt
*
* @return integer
*/
public function getOpt()
{
return $this->opt;
}
/**
* Set sections
*
* @param \AsortBundle\Entity\Sections $sections
* @return Product
*/
public function setSections(\AsortBundle\Entity\Sections $sections = null)
{
$this->sections = $sections;
return $this;
}
/**
* Get sections
*
* @return \AsortBundle\Entity\Sections
*/
public function getSections()
{
return $this->sections;
}
/**
* Set magazyn
*
* @param \AsortBundle\Entity\Magazyn $magazyn
* @return Product
*/
public function setMagazyn(\AsortBundle\Entity\Magazyn $magazyn = null)
{
$this->magazyn = $magazyn;
return $this;
}
/**
* Get magazyn
*
* @return \AsortBundle\Entity\Magazyn
*/
public function getMagazyn()
{
return $this->magazyn;
}
/**
* Set color
*
* @param \AsortBundle\Entity\Colors $color
* @return Product
*/
public function setColor(\AsortBundle\Entity\Colors $color = null)
{
$this->color = $color;
return $this;
}
/**
* Get color
*
* @return \AsortBundle\Entity\Colors
*/
public function getColor()
{
return $this->color;
}
/**
* Add price
*
* @param \AsortBundle\Entity\Price $price
* @return Product
*/
public function addPrice(\AsortBundle\Entity\Price $price)
{
$this->price[] = $price;
return $this;
}
/**
* Remove price
*
* @param \AsortBundle\Entity\Price $price
*/
public function removePrice(\AsortBundle\Entity\Price $price)
{
$this->price->removeElement($price);
}
/**
* Get price
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPrice()
{
return $this->price;
}
}
我的价格实体:
<?
namespace AsortBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="price")
*/
class Price
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Currency")
* @ORM\JoinColumn(name="currency_id", referencedColumnName="id")
*/
private $currency;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="price")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
/**
* @ORM\Column(type="decimal", nullable=true, precision=10, scale=2)
*/
protected $val;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set val
*
* @param string $val
* @return Price
*/
public function setVal($val)
{
$this->val = $val;
return $this;
}
/**
* Get val
*
* @return string
*/
public function getVal()
{
return $this->val;
}
/**
* Set currency
*
* @param \AsortBundle\Entity\Currency $currency
* @return Price
*/
public function setCurrency(\AsortBundle\Entity\Currency $currency = null)
{
$this->currency = $currency;
return $this;
}
/**
* Get currency
*
* @return \AsortBundle\Entity\Currency
*/
public function getCurrency()
{
return $this->currency;
}
/**
* Set product
*
* @param \AsortBundle\Entity\Product $product
* @return Price
*/
public function setProduct(\AsortBundle\Entity\Product $product = null)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* @return \AsortBundle\Entity\Product
*/
public function getProduct()
{
return $this->product;
}
}
我的货币实体:
<?
namespace AsortBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Translatable;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="currency")
*/
class Currency implements Translatable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Gedmo\Translatable
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $iso;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Currency
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set iso
*
* @param string $iso
* @return Currency
*/
public function setIso($iso)
{
$this->iso = $iso;
return $this;
}
/**
* Get iso
*
* @return string
*/
public function getIso()
{
return $this->iso;
}
}
在会议中,我获得了当前的ISO格式,如PLN,USD,EUR,RUB。
现在我想从产品中得到他实际的货币价格,我保持会话,就像PLN一样。
答案 3 :(得分:0)
好的我写了自定义的getter,现在工作得很好:)。
价格实体中的:
/**
* Get val
*
* @return string
*/
public function getValWithCurrency()
{
return array($this->currency->getIso() => $this->val);
}
在产品实体中:
/**
* Get val
*
* @return string
*/
public function getPriceArray()
{
$arr = array();
$pra = $this->price->toArray();
foreach($pra as $k => $v){
$a = $v->getValWithCurrency();
$arr = array_merge($arr, $a);
}
return $arr;
}
我现在可以使用会话中的货币添加getter,或者像使用密钥一样使用货币会话。
为了得到所有:
$c = $this->get('session')->get('currency');
$sectionsarray = $this->getDoctrine()
->getRepository('AsortBundle:Sections')
->createQueryBuilder('e')
->select('e, p, c, prices, cur')
->leftJoin('e.products', 'p')
->leftJoin('p.price', 'prices')
->leftJoin('e.colors', 'c')
->leftJoin('prices.currency', 'cur')
->where('cur.iso = :sesscurr')
->setParameter('sesscurr', 'PLN')
->orderBy('e.sequence', 'asc')
->addOrderBy('p.kolejnosc', 'asc')
->addOrderBy('c.sequence', 'asc')
->getQuery()
->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
谢谢!