I want to use a native SQL query for my source migration class, but I can't find a way. I've tried to use the $this->database->query() but in that case, it will fail because there's no countQuery() method defined. And for some reason, I not really want to convert every time the queries between the two formats especially when there are 6 joins and 8-9 subqueries in it. Is there a way to use the SQL query instead of the Drupal select?


 * Custom source from a database.
 * @MigrateSource(
 *   id = "custom_source",
 *   source_module = "custom"
 * )
class CustomSourceMigratePlugin extends SqlBase {

   * {@inheritdoc}
  public function query() {
    return $this->database->query("SELECT ... FROM table t
     INNER JOIN table2 t2 on ...
     INNER JOIN table3 t3 on ...
     INNER JOIN table4 t4 on ...
     INNER JOIN table5 t5 ON ...

Will fail with Error:

Call to undefined method Drupal\Core\Database\Statement::countQuery() in Drupal\migrate\Plugin\migrate\source\SqlBase->count() (line 389 of /home/ec2-user/environment/ecif-reference/web/core/modules/migrate/src/Plugin/migrate/source/SqlBase.php). If I return back $this->select('table'); then it works, but I want to supply raw SQL because I don't want to write every time drupal queries...

  • Can you give an example? – Rainer Feike Nov 20 '19 at 12:07
  • Updated, but it seems now it's not supported anyhow. I need to overwrite almost all the methods to make it work... :( – golddragon007 Nov 20 '19 at 12:44
  • There's no solution for your approach. SqlBase::query() must return a \Drupal\Core\Database\Query\SelectInterface. The only way is to also override count() in your class. Don't know if this is less work for you. – Rainer Feike Nov 20 '19 at 13:11
  • If that would be the only one thing then yes, the problem that's not enought, because then other errors will be pop up, like stepping through the rows, etc, so the whole class should be rewritten... Sooo sad. Now I need to find a converter tool that works with really complex queries. – golddragon007 Nov 20 '19 at 14:25
  • Would creating a view in the database solve your problem? – anoopjohn Nov 20 '19 at 14:37

Your Answer

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

Browse other questions tagged or ask your own question.