Doctrine součet dvou entit
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.

- Petr Parolek
 - Člen | 458
 
Ahoj, nemůžu přijít, jak sečíst v entitě položky.
<?php
namespace App\Model\Entities;
use Doctrine\ORM\Mapping AS ORM;
use Kdyby\Doctrine\Entities\BaseEntity;
/**
 * @ORM\Entity
 * @ORM\Table(name="orders")
 */
class Order extends BaseEntity
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    protected $id;
    /**
     * @ORM\Column(type="string", nullable=false)
     * @var string
     */
    protected $subject;
    /**
     * @ORM\Column(type="decimal", precision=9, scale=2, nullable=false)
     */
    protected $price;
    /**
     * @ORM\Column(type="date", nullable=false)
     */
    protected $issuedDate;
    public function __construct()
    {
        $this->issuedDate = new \DateTime();
    }
}
?>
<?php
namespace App\Model\Entities;
use Doctrine\ORM\Mapping AS ORM;
use Kdyby\Doctrine\Entities\BaseEntity;
use Kdyby\Doctrine\Entities\Attributes\Identifier;
/**
 * @ORM\Entity
 * @ORM\Table(name="items")
 */
class Item extends BaseEntity
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    protected $id;
    /**
     * @ORM\Column(type="string", nullable=false)
     * @var string
     */
    protected $title;
    /**
     * @ORM\ManyToOne(targetEntity="Order")
     * @ORM\JoinColumn(name="order_id", nullable=false, referencedColumnName="id", onDelete="RESTRICT")
     */
    protected $order;
    /**
     * @ORM\Column(type="decimal", precision=9, scale=2, nullable=false)
     */
    protected $quantity;
    /**
     * @ORM\Column(type="string", nullable=false)
     * @var string
     */
    protected $unit;
    /**
     * @ORM\Column(type="decimal", precision=9, scale=2, nullable=false)
     */
    protected $unitPrice;
    protected $price;
    public function __construct()
    {
        $this->order = new Order();
    }
    public function getOrder()
    {
        return $this->order;
    }
    public function getPrice() {
        $price = $this->quantity * $this->unitPrice;
        return number_format((float)$price, 2, '.', '');
    }
}
?>
Jde mi o to, aby se celková cena dala jednodušše vytáhnout do datagridu nebo do výpisu v šabloně.
Jak prosím spočítám celkovou cenu v entitě?

- Petr Parolek
 - Člen | 458
 
Zkušel jsem si hrát s aggregate field, ale $order->price mi nevrátil celkovou cenu

- petr.jirous
 - Člen | 128
 
udělej i inverzní vazbu k itemu a pak stačí jednoduše foreach:
/**
 * @ORM\Entity
 * @ORM\Table(name="orders")
 */
class Order extends BaseEntity
{
    /**
     * @ORM\OneToMany(targetEntity="Item", mappedBy="order")
     */
    protected $items;
	public function getTotalPrice()
	{
		$total = 0;
		foreach ($this->items as $item) {
			$total += $item->getPrice();
		}
		return $total;
	}
}
				
- Petr Parolek
 - Člen | 458
 
díky moc, takhle jsem to včera poobně zkoušel a nešlo mi to, te_ˇxd už vše počítá, jak má