Re: DB_DataObject - updating columns with relative values
| From: | Justin Patrin | Date: | Tue, 18 Jan 2005 21:26:49 +0000 |
| Subject: | Re: DB_DataObject - updating columns with relative values | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-17003@lists.php.net to get a copy of this message | ||
On Tue, 18 Jan 2005 16:23:15 +0000, Phil Gyford <gyford@gmail.com> wrote:
> Hi,
>
> Using DB_DataObject, is it possible to perform queries along the lines of:
>
> UPDATE votes SET count = count+1 WHERE id > 5;
>
> ie, updating column values depending on current column values for
> several rows? I can use whereAdd() to set the WHERE constraint of
> course, but I'm not sure how to set the 'count' column to be
> incremented by 1 using DB_DataObject. Is this possible?
>
This is perfectly possible through DB, which DB_DataObject is based
on. This isn't something that DB_DataObject supports on its own (I
don't think so anyway). You *may* be able to do somehting like this:
require_once('DB/DataObject.php');
require_once('DB/DataObject/Cast.php');
$do = DB_DataObject::factory('votes');
$do->whereAdd();
$do->whereAdd('id > 5');
$do->count = DB_DataObject_Cast::sql('count+1');
$do->update();
But this is basically the same as doing it yourself. If you need to
run a manual query this is as simple as:
$do->query('UPDATE votes SET count = count+1 WHERE id > 5');
--
Justin Patrin