Score:-2

SQL error in Module

bw flag

I'm trying to query the database inside my module, but I get this error.

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM "commerce_product_variation" "cpv" WHERE ("variation_id" NOT IN (NULL)) AND' at line 1: SELECT FROM "commerce_product_variation" "cpv" WHERE ("variation_id" NOT IN (:db_condition_placeholder_0)) AND ("status" = :db_condition_placeholder_1); Array ( [:db_condition_placeholder_0] => [:db_condition_placeholder_1] => 1 ) in deactivateProductVariantsNotInList() (regel 816 van /data/sites/web/pcreathingbe/subsites/staging.protectasolutions.be/web/modules/custom/importproducts/importproducts.module).

The code I am using is the following.

function deactivateProductVariantsNotInList($variant_type) {
  global $milieu_data;

  if ($variant_type == 'milieutechniek') {
    $database = \Drupal::service('database');
    $query = $database->select('commerce_product_variation', 'cpv');
    $query->condition('variation_id', $milieu_data, 'NOT IN')
      ->condition('status', 1)
      ->execute();
  }
}
cn flag
Is this a module you are building or is it a contrib from Drupal.org ?? Because if so there is a module that does this for a 1x Commerce site at https://www.drupal.org/project/commerce_import and also https://www.drupal.org/project/commerce_smart_importer - if you are trying to import Ubercart use https://www.drupal.org/project/commerce_migrate_ubercart OTHERWISE, I do not beleive there should be a " ; " behind the select statement or the array fields until the execute statement in your code example
arnemichiels avatar
bw flag
@wilburunion Hi, thanks for your answer. This is a module we are building which extends the commerce variants.
Score:2
de flag

If you take a closer look at your error message, your query probably fails because your $milieu_data variable is null.

FROM "commerce_product_variation" "cpv" WHERE ("variation_id" NOT IN (NULL)) AND where (NULL) is your $milieu_data variable value.

In a NOT IN condition, it should be formatted like NOT IN (value1, value2, ...). If $milieu_data is allowed to be null, then you should add a condition to have your query condition modified to $query->isNotNull("variation_id");

apaderno avatar
us flag
Yes, this is what happened.
Score:-2
cn flag

I cannot speak to misterdidi's answer it may apply also . . . but

Your error message points specifically to a syntax error. As I said in the comments it appears to be a " ; " - the semicolon at

$query = $database->select('commerce_product_variation', 'cpv');

which should be

$query = $database->select('commerce_product_variation', 'cpv')

A PHP code checker will report no issue with either but Drupal is sensitive to it

For instance in Drupal 7 this code below

   $weight = db_select('system', 's')
     ->fields('s', array('weight'))
     ->condition('name', 'uc_quote', '=')
     ->execute()
     ->fetchField();
   db_update('system')
     ->fields(array('weight' => $weight + 1))
     ->condition('name', 'uc_addresses', '=')
     ->execute();

ONLY has the semicolon at the end or execute and in Drupal 8/9 Drupalmoduleupgrader changed it to

   $weight = \Drupal::database()->select('system', 's')
     ->fields('s', array('weight'))
     ->condition('name', 'uc_quote', '=')
     ->execute()
     ->fetchField();
   // TODO: Drupal Rector Notice: Please delete the following comment after you've made any necessary changes.
   // You will need to use 
  \Drupal\core\Database\Database::getConnection()` if you 
 do not yet have access to the container here.
   \Drupal::database()->update('system')
     ->fields(array('weight' => $weight + 1))
     ->condition('name', 'uc_addresses', '=')
     ->execute();

While there is no "system" table any longer in Drupal 8 / 9 the purpose of DrupalModuleUpgader and Drupal Rector is to correct syntax and apply coding standards

You will notice whether it is $weight or your $query variable there is no semicolon behind the statement.

The semicolon needs to be removed

apaderno avatar
us flag
No, `$query = $database->select('commerce_product_variation', 'cpv');` is correct, given that the next lines starts with `$query->condition('variation_id', $milieu_data, 'NOT IN')`.
cn flag
@apaderno you are WRONG. See https://www.drupal.org/docs/8/api/database-api/dynamic-queries/introduction-to-dynamic-queries at "The dynamic query would use the following code." just above "#Executing the query" NEVER when there are array conditions below the the database select or other command is there a " ;" semicolon in the format the Original Poster has presented. The query does not "fail" like misterdidi says, the rror message says there is a syntax issue - and that semicolon like that in Drupal will throw that message
apaderno avatar
us flag
This is plain PHP syntax: `$query = $database->select('commerce_product_variation', 'cpv'); $query->condition('variation_id', $milieu_data, 'NOT IN');` is correct; `$query = $database->select('commerce_product_variation', 'cpv') $query->condition('variation_id', $milieu_data, 'NOT IN');` throws a parsing/syntax error.
cn flag
The question is edited, but the original had //'s and the error. The way it shows now is correct - and someone edited it to change it. It is NOT the same questiion I saw originally
apaderno avatar
us flag
I just removed the commented lines, but those don't change the code, since PHP ignores the comments between code lines.
cn flag
I do not remeber that second $query - in any even the code you present in such a case as the question is edited to is correct. Without the second $query mine is correct
apaderno avatar
us flag
You cannot remove the semicolon from `$query = $database->select('commerce_product_variation', 'cpv'); $query->condition('variation_id', $milieu_data, 'NOT IN');` as PHP would report a syntax/parse error. That line has been always shown in the question; I didn't add any code to the question, but eventually I removed the commented out code, which was just making difficult to read the code. Anyway, that isn't the reason for the error the OP is getting. The correct answer is given from the other answer.
cn flag
As I said I rember the second line as ->condition('variation_id', $milieu_data, 'NOT IN') - NOT with a $query. Your comment supports my point as the error pointed specifally to " at line 1: SELECT FROM "commerce_product_variation" "cpv" " That would be the semicolon because there was no second $query as I remember it. I ran the code in devel php eval and removing the semicolon fixed it. This is why American law at 47 USC 230 does not allow people to edit other people's posts. I thnk stackoverflow may soon end this practice. I can't see the original post so I have no way to agree
apaderno avatar
us flag
Revisions for a post are accessible to every user. For the question, it's sufficient to click on [edited Nov 5 at 8:58](https://drupal.stackexchange.com/posts/313560/revisions).
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.