I’ve a builder for selecting people with a phone number not null, a permission flag true (in the first builder call), and with a birthday template not null in this function. There is also a birthday field:
public function getAllForBirthdaySmsSend()
{
$qb = $this->getAllSuitableForSmsQueryBuilder();
$alias = $qb->getRootAlias();
$today = new \DateTime();
return $qb->andWhere(
$qb->expr()->andX(
$qb->expr()->isNotNull("$alias.sms_birthday_template"),
/* Filter if today is his birthday */
))
;
}
Now i should filter people by birthday, that is if birthday column formatted as 'm-d-' . date('Y') equals $today.
Anyone knows how to to this with query builder? I don’t want to write pure SQL query but i prefer reusing the other query builder to be DRY.
You can use native sql and then map it in order to deal with real entities makeing use og
ResultSetMapping. Of curse, the YEAR is ignored:Mapping the query fields:
Create a query using native date functions (each selected column now is mapped ):
Get the users:
Tip: Try to avoid casting column values when you want to do a query. It forces MySQL(if it is your case) to convert the column into a CHAR before performing the comparison and alter the performance.
Edit birthday column is datetime type. And the user model has the birtdate attibute mapped as
\DateTime object