Re: DB_DataObject: how to implement a calculated data inside a object FROM another

From: Date: Sat, 05 Jun 2004 17:38:51 +0000
Subject: Re: DB_DataObject: how to implement a calculated data inside a object FROM another
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-12934@lists.php.net to get a copy of this message
J llarens wrote:
HI, Im using DB_DataObject only in a basic approach, now I want to "updgrade" mi skill with this. Suppose I have a database with data about people and an account of credit and purchases for each one. Say, table people
id    int(6)
name varchar(20) table movements
id            int(10)
id_people     int(6)
amount        float(8,2)
The real thing uses more fields and tables, but for purposes of simplicity i think is enough. Each row of "movements" will point to a given "people" via the foreign key "id_people" and the amount can be
0 if the movement is a credit or <0 if is a debit(a
purchase), so to know the status of the account of a given people with id=345, I do: select sum(amount) from movements where id_people=345 What I want, is a "status" data in the people dataobject that do that, a query over movements to get that sum(amount).
Override the fetch() method of your People DataObject. function fetch() { parent::fetch(); $db = $this->getDatabaseConnectrion(); $this->status = $db->getOne('select sum(amount) from movements where id_people='.$this->id); } Of course, if you edit this field, update() and insert() woin't update it. -- paperCrane <Justin Patrin>

« previous php.pear.general (#12934) next »