2015-11-26 2 views
0

У меня есть объектные имени Разделы, которые имеют:Доктрина присоединиться Entity с присоединиться OneToMany

/** 
* @ORM\OneToMany(targetEntity="Product", mappedBy="sections") 
*/ 
protected $products; 

И сущность имени продукта, которые:

/** 
* @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

хорошо я написал пользовательские добытчиками и теперь работают хорошо :)

в Цена Entity:

/** 
* Get val 
* 
* @return string 
*/ 
public function getValWithCurrency() 
{ 
    return array($this->currency->getIso() => $this->val); 
} 

в Entity продукта:

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

enter image description here

я теперь могу добавить геттер с валютой из сеанса или использовать валютную сессию как ключ.

И получить все:

$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); 

Спасибо!

0

Entity/product.php

/** 
* @ORM\ManyToOne(targetEntity="Price", inversedBy="products") 
* @ORM\JoinColumn(name="price_id", referencedColumnName="id") 
*/ 
private $price; 

Entity/Price.php

/** 
* @ORM\OneToMany(targetEntity="Product", mappedBy="price") 
*/ 
private $products; 

Repository/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); 
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); 

Я использовал цены вместо цены, потому что поле, представляющее один для многих, должно быть во множественном числе.

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

} 

Моя цена Entity:

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

Моя Валюта Entity:

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

} 

В сессии I получил текущий cyrrenc y в формате ISO, таком как PLN, USD, EUR, RUB.

Теперь я хочу получить от продукта его фактическую цену в валюте, которую я держу в сессии, как PLN.

Смежные вопросы