Got this entity:
/**
* @ORM\Table(name="shop_payment_details")
* @ORM\Entity(repositoryClass="Acme\ShopBundle\Entity\PaymentRepository")
*/
class Payment extends ArrayObject
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*
* @var integer $id
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
**/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="Acme\ShopBundle\Entity\Item")
* @ORM\JoinColumn(name="item_id", referencedColumnName="id")
**/
protected $item;
/**
* @ORM\Column(name="date", type="datetime", nullable=true)
*/
protected $date;
/**
* @ORM\Column(name="amount", type="float", nullable=true)
*/
protected $amount;
I need to file a sales table between two dates he selected. The table must contain all days between two dates and reference codes of the objects in the store.
How can I get it? Can do it just with Doctrine or i must use PHP to build that table?
Create a custom DQL (in a repository I suggest) to retrieve all payments where payment.date => yourSmallerUserDate and payment.date <= yourBiggerUserDate.
The result will be all payments between your two dates.
Within your logic you build a table with a row per day between your two dates. And whenever you have a date in your result matching your row date add it to your output.
It is possible, what you need is the orX()
function supplied by Doctrine-ORM
You will need to create two expressions using the function exp()
then you can put both expressions in the orX()
statement to get the wanted results.
For the expressions you need to define you will need the functiongt()/lt()
- greater than/ lower than
This will give you the possibility to compare two dates
For a further reference check this link:
https://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html#high-level-api-methods
PS: For readabilty and logic it should be easier to create the expression before inserting them into your query:
$exp1 = $qb->expr()->gt('..', '?value');
$exp2 = $qb->expr()->gt('..', '?value2'),
...
$qb->where($qb->expr()->orX($exp1, $exp2));
Notice that orX
can aggregate an unlimited number of expressions