ublaboo datagrid dopočíváníní celkové sumy

Petr Parolek
Člen | 455
+
0
-

ahoj, zjistil jsem jednu nemilou věc v Doctrine, nemůžu přečíst přes querybuilder dopočítanou property v entitě. Jde mi o vytvoření vyhledání v ublaboo datagridu rozmezí částky. Jak na to prosím? Mé pokusy nevychází. v nextras ORM je to jedooché přes virtuární property v entitě

<?php

namespace App\Model\Entities;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity
 */
class Some
{

	use \Nette\SmartObject;

	/**
	 * @ORM\Id
	 * @ORM\Column(type="integer")
	 * @ORM\GeneratedValue
	 */
	private $id;

	private $totalPrice;

	/**
	 * @ORM\OneToMany(targetEntity="SomeItem", mappedBy="Some")
	 */
	private $items;

	public function __construct()
	{
		$this->items = new ArrayCollection();
	}

	public function getId()
	{
		return $this->id;
	}

	/**
	 * @return float
	 */
	public function getTotalPrice()
	{
		$total = 0;
		foreach ($this->items as $item) {
			$total += $item->getPrice();
		}

		return $total;
	}

	public function getItems()
	{
		return $this->items;
	}
}
```
?>
<?php

....
	public function createComponentSomeGrid($name)
	{
		$grid = new DataGrid();
		$this->addComponent($grid, $name);

		$data = $this->someRepository->createQueryBuilder('er')
			->select('i')
			->from(App\Model\Entities\Some::class, 'i');

		$grid->setDataSource($data);

		$grid->addColumnText('totalPrice', 'invoice_datagrid.total')
				->setFilterRange('totalPrice', '')
				->setCondition(function ($collection, $value) {
					$collection->select('SUM(ii.quantity * ii.unitPrice) AS price')
					->leftJoin(\App\Model\Entities\SomeItem::class, 'ii')
					->andWhere('price > ?1')
					->andWhere('price < ?2')
					->setParameter(1, $value->from)
					->setParameter(2, $value->to);
				})
....
	}
?>

Háže mi to `Doctrine\ORM\Query\QueryException [Semantical Error] line 0, col 163 near ‚totalPrice >=‘: Error: Class App\Model\Entities\Some has no field or association named totalPrice `

Editoval ppar (18. 9. 2018 23:21)

Petr Parolek
Člen | 455
+
0
-

pokus o řešení:

<?php
...

			$grid->addColumnText('totalPrice', 'some_datagrid.total')
				->setFilterRange('totalPrice', '')
				->setCondition(function ($collection, $value) {
					$collection->select('SUM(ii.quantity * ii.unitPrice) AS price')
					->leftJoin(\App\Model\Entities\SomeItem::class, 'ii')
					->andWhere('price > ?1')
					->andWhere('price < ?2')
					->setParameter(1, $value->from)
					->setParameter(2, $value->to);
				});
...
?>

RuntimeException
Cannot count query which selects two FROM components, cannot make distinction

Editoval ppar (18. 9. 2018 23:16)

Petr Parolek
Člen | 455
+
+1
-

nakonec vyřešeno přes Doctrine Eventy