Div PHP 对象关系映射

1.0.0 2019-09-19 04:04 UTC

This package is auto-updated.

Last update: 2024-09-15 06:55:33 UTC


README

此类允许您在您的数据库对象和PHP对象之间建立映射。

您可以通过继承divengine\orm实现您的模型。以下是一个示例,它实现了一个类层次结构(方案、映射、集合、权限)并所有都继承自相同的divengine\orm类。

<?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;
}

现在看看如何使用您的模型的一个示例

<?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]);

享受!

--

@rafageist

https://rafageist.com