I'm not really sure I fully understand what you are asking, but here's a solution to what I think you are asking:
function orm_for($type) {
return strtolower($type);
}
function peer_for($type) {
return ucfirst($type)."Peer";
}
function exception_for($type) {
return ucfirst($type)."Exception";
}
function query($type, $data) {
$peer = $peer_for($type);
$exception = $exception_for($type);
$obj = null;
$criteria = new Criteria();
$criteria->add($peer::CONTRACTNR, $data["contractnr"]);
$result = $peer::doSelect($criteria);
if(count($result) > 1) {
throw new $exception("status: more than one row with the specified contractnr.");
} else if(count($result) == 0) {
$obj = $this->factory->createORM(orm_for($type));
$obj->setCreatedAt(time());
} else {
$obj = $result[0];
}
}
I think the code is self-explanatory. Let me know whether or not I interpreted your question correctly.
A live example (just a POC) can be found here