View Full Version : Active record pattern issues

02-21-2007, 10:58 PM
Hiya, just threw together a generic active record class and I've sort of got a bit of coder's block... Just can't see my next step :rolleyes:...

Basically my problem is binding it to a row, how should I handle it?

public function commit(){
$this->prv_dbInst->query(sql::update($this->prv_table, $this->prv_vars, [...]));
$this->prv_dbInst->query(sql::insert($this->prv_table, $this->prv_vars));

The sql helper, update, has to take a primary key.

If the object is created from a row request, then it needs to be bound to a primary key so changes can be commited... But right now the neatest way to do this eludes me. Right now the sql::update helper expects `id` as a primary key. But this isn't exactly flexible.


02-21-2007, 11:02 PM
default the argument to 'id' so you only have to (but can) specify it if it's different?

public static function update($table_name,$vars,$id_field='id') {

02-21-2007, 11:06 PM
Can I expect the first field in a returned row of a query to be a primary key, if any?

02-21-2007, 11:29 PM
not necessarily, what would happen with composite keys?

One option would be for your models to be reading the metadata from the database to determine such things. In postgres you can use tables with the pg_ prefix to work out such things, I assume MySQL has a similar thing. If you take a look at the MetaColumns() function in adodb (http://adodb.sourceforge.net/) then it should give you a pointer.

02-22-2007, 12:06 AM
Woohoo! Decent reference material!

Cheers man, I'll see what I can dig up.