We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

dynamic prepared statement in controller

currently I'm experimenting with https://buysql.com/mysql/14-how-to-automate-pivot-tables.html

table:

+----+---------+---------------+--------+ | id | item_id | property_name | value | +----+---------+---------------+--------+ | 1 | 1 | color | blue | | 2 | 1 | size | large | | 3 | 1 | weight | 65 | | 4 | 2 | color | orange | | 5 | 2 | weight | 57 | | 6 | 2 | size | large | | 7 | 3 | size | small | | 8 | 3 | color | red | | 9 | 3 | weight | 12 | | 10 | 4 | color | violet | | 11 | 4 | size | medium | | 12 | 4 | weight | 34 | | 13 | 5 | color | green | | 14 | 5 | weight | 10 | +----+---------+---------------+--------+

the query is

SET @sql = NULL; SELECT GROUP_CONCAT(DISTINCT CONCAT( 'MAX(IF(property_name = ''', property_name, ''', value, NULL)) AS ', property_name ) ) INTO @sql FROM properties; SET @sql = CONCAT('SELECT item_id, ', @sql, ' FROM properties GROUP BY item_id');

PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

Question: how to run this statement from controller and get fetched resutls..

edited Mar '16

With registered MySQL db service in DI, you can simply execute in controller context

$statement = $this->db->prepare('SELECT * FROM robots WHERE name = :name');
result = $this->db->executePrepared($statement, array('name' => 'Voltron'));

more info in API doc

https://docs.phalcon.io/en/latest/api/Phalcon_Db_Adapter_Pdo.html