Div PHP Object Relational Mapping

1.0.0 2019-09-19 04:04 UTC

This package is auto-updated.

Last update: 2024-11-15 07:17:26 UTC


README

This class allow to you make a mapping between your database objects and your PHP objects.

You can implement your model inheriting from divengine\orm. Look at the following example as it implements a hierarchy of classes (scheme, map, collection, entitlement) and all inherit from the same divengine\orm class.

<?php

use divengine\orm;

class PublicMap extends orm
{
    protected $__map_type = self::SCHEMA;
    protected $__map_schema = 'public';
    protected $__map_identity = 'id = :id';
}

class PersonMap extends PublicMap
{
    protected $__map_type = self::RECORD;
    protected $__map_name = 'person';
    protected $__map_class = Person::class;
}

class Person extends PersonMap {

    private $id = self::AUTOMATIC;
    private $name;

    public function getId() {
        return $this->id;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function getName() {
        return $this->name;
    }
   
    public function setName($name) {
        $this->name = $name;
    }
   
}

class PersonCollection extends PersonMap {
    protected $__map_type = self::TABLE;
}

Now look at an example of how to use your model:

<?php

use divengine\orm;

$pdo = new PDO(); // or use orm::buildPDO();
orm::connectGlobal($pdo); // or pass true to second param of orm::buildPDO()

$person = new Person(['name' => 'Peter']);
// $person::connect($pdo); 
$person->insert();

$list = new PersonCollection();
$list->addItem($person);

$entity = $list->getFirstItem('id = ?', [100]);

Enjoy!

--

@rafageist

https://rafageist.com