qsir1;10880488 wrote:Thanks but I'm looking for something straightforward and simple. Any ideas??
Me too. Thats why I rolled my own. I've systematically tried propel (which Doctrine is built on I believe) and Doctrine...and maybe another or two I can't remember.
So here's my simple approach:
interface Mapper {
find($id); //returns domain obj
findAll($where=NULL); // returns array of domain objs
save(&$domainObj); // return boolean
delete(&$domainObj); // return boolean
}
/**
* Domain object
*/
class User {
private $user_id;
private $user_email;
/**
* Getters and setters for all persistable attributes
*/
public function getId() {
return $this->user_id;
}
}
/**
* Mapper depends on the User class, this is OK, the reverse is not
*/
class UserMapper implements Mapper {
// with of course all error and return value checking completely ignored!
public function find($id) {
// my db abstraction layer allows for '?' style placeholders
$sql = "select user_id, user_email from users where id = ?";
// substitutes $id for the '?'
$row = $this->dbConnectionObject->selectOne($sql, $id);
$user = new User();
$user->setId($row['user_id']);
$user->setEmail($row['user_email']);
return $user;
}
// etc... w/ other functions
}
So now when I have domain objects that span several tables I can control all the SQL inside the mapper. By only every relying on the Mapper interface I have abstracted a mapping layer, crude though it may be.
The "$where" in findAll works like this: if its NULL then ALL objects are found, otherwise its a hash:
$where = array('user_email'=>'someone@somedomain.com');
$users = $mapperInstance->findAll($where);
Its a little icky because you have to know that the column in the table is 'user_email', but given how simple it makes this I find it to be a tolerable offense. Also, its really fast and small memory foot print compared to, say, Doctrine.
If each domain class maps to its own table, like an Active Record pattern, you can use adodb or something similar, and not have to rewrite a new mapper for each class.