onPHP

Mapped DAO example.

 

en / ru

 

Criteria
OSQL
DAOs
Form
Cache
Application
Metaconfiguration

 

handout

    This example is relevant only for legacy 0.4.x series.

DB stuff:

    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())
    );

business class:

    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 ...
    }

main/DAOs

sample 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()
                    );
        }
    }

hint:

    final class DAO
    {
        public static function employer()
        {
            return Singleton::getInstance('EmployerDAO');
        }
    }

usage:

    // 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
                )
            )
        );

powered by `tar xfj /dev/tty`
$Id: examples.DAOs.en.html 3205 2007-04-30 20:14:42Z voxus $