0

So I am using a plugin to import data and it isn't assigning the values (asset is inside of Matrix) so I am going to modify the plugin and add code to lookup values that I need to assign to MatrixBlockModel and then re-save the item.

I looked up how to create a command and based on that I wrote 3 lines of code but I have no idea on how to iterate through the results. Here is the sample code:

$query=craft()->db->createCommand()
  ->select('id, folderId, filename, kind')
  ->from('{{ assetfiles }}')
  ->where('filename= :fileName', array(':fileName' => 'somefilename'));

How do I iterate through the results and how can I access info from the query?

1

I'd suggest reading through the Yii QueryBuilder docs, which is what the code you've written is using.

->from('{{ assetfiles }}')

should be:

->from('assetfiles')

And if you want multiple rows returns as a a result, you need to add a:

->queryAll();

to the end of the statement.

From there, you can do a foreach loop to access the results of your query:

foreach ($query as $row)
{
    $id = $result['id'];
}
  • Thank you @Brad, after I posted it started looking in the app folder to see how the code was written and then used it as an example. But this is going to be helping in future when I forget where I wrote my code. – Subrato M Sep 21 '16 at 3:09

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.