I mean the latter version - flush was the method name that came to mind for me too 🙂
What I meant about prepared statements is that once the statement is built the database has an optimisation strategy for succcessive queries using that statement, and so are much faster - batch operations have a big performance increase.
I think leaving the statements until they are needed fits in with JIT mentality which is usually a good thing in web apps.
In a project I'm working on currently I've used Active Record rather than data mappers, for simplicities sake. To generate the insert/update queries I've used a bit of a hack with the visibility of properties. It looks something like this:
// ModelObject is basically a dumb data container with some accessors
abstract class ActiveRecord extends ModelObject
{
static private $db; // ADODB
public function save( $table, $key )
{
/*
This is the magic bit, when called from a subclass get_object vars
only returns public and protected properties of the subclass, I generate
the SQL statements from this array
*/
$params = get_object_vars( $this );
if( $this->getPrimary() )
{
return $this->update( $table, $params, $key );
}
else
{
return $this->insert( $table, $params );
}
}
}
class Recipe extends ActiveRecord
{
const TABLE = 'Recipes';
const PRIMARY_KEY = 'id';
protected
$id,
$value;
}
$recipe = new Recipe();
$recipe->save( Recipe::TABLE, Recipe::PRIMARY_KEY );
// Generated query is INSERT INTO Recipes VALUES( '$id', '$value' ));
Ideally ActiveRecord should be able to figure out what the child class is automatically but I don't think there is a way. Also it only works for 1 object = 1 table, which quickly became too restrictive so I end up overriding ActiveRecord::save() in some cases.
When is the next PHPLondon meeting?