раздаточный материал
Данный пример актуален только для безнадежно устаревших 0.4.x версий.
таблица:
create sequence employer_id;
create table employer(
id bigint default nextval('employer_id') primary key,
name varchar(255) not null,
birth_date timestamp not null,
created timestamp not null default(now()),
modified timestamp not null default(now())
);
бизнес-класс:
final class Employer extends NamedObject
{
private $birthDate = null;
private $created = null;
private $modified = null;
public function getBirthDate()
{
return $this->birthDate;
}
public function setBirthDate(Timestamp $birth)
{
$this->birth = $birth;
return $this;
}
... and so on ...
}
пример DAO-класса:
final class EmployerDAO extends MappedStorableDAO
{
protected $mapping = array(
'id' => null,
'name' => null,
'birthDate' => 'birth_date',
'modified' => null
);
public function getTable()
{
return 'employer';
}
public function getObjectName()
{
return 'Employer';
}
public function makeObject(&$array, $prefix = null)
{
$employer = new Employer();
return
$employer->
setId($array[$prefix.'id'])->
setName($array[$prefix.'name'])->
setBirthDate(
new Timestamp(
$array[$prefix.'birth_date']
)
)->
setModified(
new Timestamp(
$array[$prefix.'modified']
)
);
}
public function setQueryFields(
InsertOrUpdateQuery $query, Employer $employer
)
{
if ($query instanceof UpdateQuery)
$query->
set(
'modified',
$employer->
setModified(new Timestamp(time()))->
getModified()->
toDialectString()
);
return
$query->
set('id', $employer->getId())->
set('name', $employer->getName())->
set(
'birth_date',
$employer->getBirthDate()->toString()
);
}
}
намёк:
final class DAO
{
public static function employer()
{
return Singleton::getInstance('EmployerDAO');
}
}
использование:
// get employer with id == 2
$employer = DAO::employer()->getById(2);
// get employers list sorted by birth date
$youngEmployersList =
DAO::employer()->getList(
ObjectQuery::create()->
sort('birthDate')->asc()->
addLogic(
Expression::gt(
'birthDate',
1970
)
)
);
|