This has been bothering me for a few days now.
Using Migrate module, I am using a GROUP_CONCAT in an expression and the prepareRow always has a null value for the expression.
For example, heres how my query looks at the moment;
$query = Database::getConnection('default', 'custom_migrate')
->select('Articles', 'a')
->fields('a')
->condition('a.SiteID', 'site-id', 'LIKE');
// Join our tags. Use LIKE again.
$query->leftJoin('TagAssociation', 'ta', 'ta.InstanceId = a.id AND ta.Class LIKE :param', array(':param' => 'article'));
$query->leftJoin('Tag', 't', 'ta.TagId = t.id AND t.SiteId LIKE :param', array(':param' => 'site-id'));
$query->addExpression("GROUP_CONCAT(t.Tag)", 'customTags');
// Join our show data.
$query->leftJoin('ShowContent', 'sc', 'sc.ContentId = a.id AND sc.Type LIKE :param', array(':param' => 'article'));
$query->leftJoin('Shows', 's', 's.Id = sc.ShowId');
$query->addField('s', 'id', 'ShowId');
$query->groupBy('a.id');
Pretty simple stuff, but in both prepare()
and prepareRow()
I get a null value for $row->customTags
. Even If I leave the alias off and use $row->expression
. Null.
"Make the join a subquery!" I hear you say. Same thing.
Dumping the $query object (cast as string to see query) and putting my params in, I can query the db and get the values I expect.
SELECT a.*, s.id AS ShowId, GROUP_CONCAT(t.Tag) AS expression
FROM Articles a
LEFT OUTER JOIN TagAssociation ta ON ta.InstanceId = a.id AND ta.Class LIKE 'article'
LEFT OUTER JOIN Tag t ON ta.TagId = t.id AND t.SiteId LIKE 'fiveaa'
LEFT OUTER JOIN ShowContent sc ON sc.ContentId = a.id AND sc.Type LIKE 'article'
LEFT OUTER JOIN Shows s ON s.Id = sc.ShowId
WHERE (a.SiteID LIKE 'fiveaa')
GROUP BY a.id
LIMIT 20; // Added by me.
Yep, the expression column is populated with comma separated tags, as expected.
SO, somewhere between me creating this query and the prepareRow method, the value is being lost.
Now, before I dig into the module code I thought I'd get a sanity check from anybody that can to see if I'm doing something dumb that I've missed.
Note The field mapping for this field has been removed while I work out what is going on.
UPDATE Ok, so, the file sql.inc in the migrate module where it executes the query looks like this (around line 335);
migrate_instrument_start('MigrateSourceSQL execute');
$this->result = $this->query->execute();
migrate_instrument_stop('MigrateSourceSQL execute');
If I add some debug ...
var_dump($this->query->getExpressions());
var_dump((string)$this->query);
migrate_instrument_start('MigrateSourceSQL execute');
$this->result = $this->query->execute();
$row = $this->result->fetchAssoc();
var_dump($row);
I can see my expression ...
array(1) {
["contentTags"]=>
array(3) {
["expression"]=>
string(19) "GROUP_CONCAT(t.Tag)"
["alias"]=>
string(11) "contentTags"
["arguments"]=>
array(0) {
}
}
}
And I can see my query (as above) but ... null result on that col.
["contentTags"]=>
NULL
UPDATE 2 So, if I change my leftJoins to joins, I get no results at all ... While running the query manually with joins returns rows.
FINAL UPDATE I am now doing a separate query in the prepare method to get these values. I couldn't work out what was going on or the best course of action. I'll leave this open incase others have issues and post in here.