5

To use where in a MySQL table row update in Zend Framework I have something like:

public function updateBySiteId(array $data, $id) {
        $table = $this->gettable();

        $where = $table->getAdapter()->quoteInto('site_id = ?', $id);

        return $table->update($data, $where);
    }

and this, I expect, gives me something like...

UPDATE foo SET ponies = 'sparkly' WHERE site_id = '1'

But what if I want to create the following:

UPDATE foo SET ponies = 'sparkly' WHERE site_id = '1' AND type = 'zombie'

In the manual I don't see how to do this with quoteInto (or quote or some other safe method... which could just mean I'm looking in the wrong place but... sigh).

1 Answer 1

9

Since the table update() method proxies to the database adapter update() method, the second argument can be an array of SQL expressions. The expressions are combined as Boolean terms using an AND operator.

http://framework.zend.com/manual/en/zend.db.table.html

$data = array(
'updated_on'      => '2007-03-23',
'bug_status'      => 'FIXED'
);
$where[] = "reported_by = 'goofy'";
$where[] = "bug_status = 'OPEN'";
$n = $db->update('bugs', $data, $where);

Resulting SQL is:

UPDATE "bugs" SET "update_on" = '2007-03-23', "bug_status" = 'FIXED' WHERE ("reported_by" = 'goofy') AND ("bug_status" = 'OPEN')

http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.write.update

Sign up to request clarification or add additional context in comments.

1 Comment

That did it. I read that bit in the manual but created the array incorrectly: $where = $table->getAdapter()->quoteInto(array('site_id = ?' => $id, 'foo' => $bar));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.