-1

I have this sql segment:

WHERE MONTH(pd.field_publish_date_value) = MONTH(CURRENT_TIMESTAMP) AND YEAR(pd.field_publish_date_value) = YEAR(CURRENT_TIMESTAMP) AND fd.langcode='sk' AND fd.type='article'

I need to transform it into something like this:

->condition(MONTH(pd.field_publish_date_value), MONTH(CURRENT_TIMESTAMP), '=')
->condition(YEAR(pd.field_publish_date_value), YEAR(CURRENT_TIMESTAMP), '=')
->condition(fd.langcode, 'sk', '=')
->condition(fd.type, 'article', '=');

but, how to interpret MONTH() and YEAR() functions in D8? Google offered me only Drupal 7 ->where() - I need to make it with ->condition().

[EDIT]

$sqlSK = "SELECT COUNT(fd.nid)
    FROM node fd                                
    LEFT JOIN node__field_publish_date pd
    ON fd.nid=pd.entity_id
    WHERE MONTH(pd.field_publish_date_value) = MONTH(CURRENT_TIMESTAMP) AND 
    YEAR(pd.field_publish_date_value) = YEAR(CURRENT_TIMESTAMP) AND 
    fd.langcode='sk' AND fd.type='article'
";

Thanks.

  • 1
    Can you please edit your question and add some more information? I'd like to see all code that builds the query, not just the ->condition() part. Also, tell us if you're using a MySQL database or something else. Finally: why do you think you must use ->condition()? If I'm not mistaken, Drupal 8 also has a where() method. – marcvangend Dec 20 '17 at 21:28
1

With a Connection object (\Drupal::service('database') or @database with DI).

$database = \Drupal::service('database');
$database->select('node_field_data', 'fd');
$database->join('table','pd', 'pd.XXX=fd.XXX'); //trying to match your query
$database->where('MONTH(pd.field_publish_date_value = MONTH(CURRENT_TIMESTAMP)');
$database->condition('fd.langcode', 'sk')
$database->condition('fd.type', 'article');
$result = $database->execute();
//do something
$result->fetchAll(); 

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.