Is there a method similar to the D7 dynamic query where method to allow us to use arbitrary snippets of SQL to restrict the result set?

Take the following MYSQL query as an example:

SELECT id, publish_date
FROM teaching_period
WHERE publish_date IS NULL OR UNIX_TIMESTAMP(STR_TO_DATE(CONCAT(publish_date, ' 23:59:59'), '%Y-%m-%d %H:%i:%s')) > UNIX_TIMESTAMP(CURRENT_TIMESTAMP)
LIMIT 0, 1

How can I create the where statement in this query using the D8 entity query API? Is it even possible?


No, in general, it is not. Entity query is not an SQL-query replacement, it designed to also work with other storage backends, so you can not use 100% of the SQL features.

If you need that, you have to implement your own SQL query.

That said, I don't quite understand why your special SQL logic is even necessary, date fields can be queried natively, e.g. publish_date > :date should just work, as long as you provide the date in the correct format as and timezone input.

See How to do a date range entityQuery with a Date only field in Drupal 8?

  • Thanks for the link - that looks like a much better solution (especially with the timezone consideration) – Felix Eve Sep 18 '17 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.