Search code examples
phpzend-frameworkpostgresqlzend-db

Zend Database, Join Query with Typecast error (I think)


Im wondering if there is some means of just plopping a prepaired statement into the Zend Database class, and using that rather then using ->select(), ->from(), ->where(). I have tried the below query in just ->query() but that fails to. I only ask cause all the references I find even in the zend docs speak towards mysqli only. I'm finding the DB class to be very annoying with more complex queries (and even then really not that complex).

I have this query

SELECT 
org.orgid, 
org.roleid, 
users.userid, 
users.email, 
users.firstname, 
users.lastname, 
users.contact, 
users.state, 
users.ts, 
users.alternate_delivery_email, 
users.unbound, 
users.blocked 
FROM my_mapping AS org 
INNER JOIN my_users AS users ON org.userid = users.userid
WHERE (org.orgid = 'generated-id')
AND (org.roleid::text LIKE 'partner-%');

And I have been trying to conform it to the matching zend DB version

$select = $db->select()
        ->from(array('org' => 'my_mapping'),
            array(
                'orgid' => 'org.orgid',
                'roleid' =>'org.roleid',
                'userid' =>'users.userid',
                'email' =>'users.email',
                'firstname' =>'users.firstname',
                'lastname' =>'users.lastname',
                'contact' =>'users.contact',
                'state' =>'users.state',
                'ts' =>'users.ts',
                'alternate_delivery_email' =>'users.alternate_delivery_email',
                'unbound' =>'users.unbound',
                'blocked' =>'users.blocked'
            ))
        ->join(array('my_users' => 'users'),'org.userid = users.userid',array())
        ->where('org.orgid = ?',$mspID)
        ->where('org.roleid::text LIKE ?', 'partner-%');  

Which gives me nothing but problems. No matter which way I try to change it to work. Yet, going in the terminal and using the straight query works fine, I get results and everything else. So with that I set out to see if I could find a means of using that exact query passed into the the Zend DB class, so I can work with the output like i would otherwise with queries that actually work with Zend DB class. But all references I find in that manor are specific to mysql or mysqli, so I am not sure if it will work, and if it will how exactly do I string it together?

Also worth mentioning is "org.roleid" is a "enum" hence the ::text, also roleid and userid are "uuid".

The Error I am getting is:

Warning: PDO::quote() expects parameter 1 to be string, object given in ........ on line ...
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at end of input LINE 1: SELECT "my_users".* FROM "my_users" WHERE ( ^

Solution

  • You need to use Zend_Db_Statement to do prepared statements in Zend Framework - http://framework.zend.com/manual/1.12/en/zend.db.statement.html

    Also, here is another stackoverflow thread discussing the same - How to use prepared statements in Zend Framework