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

Insert array to Postgresql error

I try to insert array to postgresql :

array(4) { [0]=> string(1) "1" [1]=> string(1) "6" [2]=> string(1) "9" [3]=> string(2) "12" }

Column datatype: varchar[]

But got this error : SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

Please help me fix this

edited Nov '16

Phalcon ORM does not support PGSQL arrays, so you'll have to write your native SQL queries like so:

class SomeController extends \Phalcon\Mvc\Controller
{
    public function someAction()
    {
        $set = array("1","6","9","12");
        settype($set, 'array'); // can be called with a scalar or array
        $result = array();
        foreach ($set as $t) {
            if (is_array($t)) {
                $result[] = to_pg_array($t);
            } else {
                $t = str_replace('"', '\\"', $t); // escape double quote
                if (! is_numeric($t)) // quote only non-numeric values
                    $t = '"' . $t . '"';
                $result[] = $t;
            }
        }
        $result = '{' . implode(",", $result) . '}'; // format
        $this->db->execute("INSERT INTO tbl (colName) VALUES (".$result.")");
    }
}

https://stackoverflow.com/questions/5631387/php-array-to-postgres-array