轻量级PHP ORM

v1.0.0 2018-05-20 15:59 UTC

This package is auto-updated.

Last update: 2024-09-14 05:11:47 UTC


README

轻量级PHP ORM

LightQL是一个基于查询对象数据映射器设计模式的面向对象映射器(ORM)。它使用注解来帮助您创建、编辑、删除、查找实体等,无需编写SQL查询。

示例

1. 创建持久化单元

{
  "DBMS": "mysql",
  "Hostname": "127.0.0.1",
  "DatabaseName": "my_app_db",
  "Username": "root",
  "Password": ""
}

2. 创建实体

<?php

namespace MyApp\Entities;

/**
 * Class UserEntity
 *
 * @entity('table' => 'users', 'fetchMode' => \ElementaryFramework\LightQL\Entities\Entity::FETCH_EAGER)
 * @namedQuery('name' => 'findAll', 'query' => 'SELECT * FROM users')
 * @namedQuery('findById', 'SELECT * FROM users u WHERE u.user_id = :id')
 */
class UserEntity extends \ElementaryFramework\LightQL\Entities\Entity
{
    /**
     * @id
     * @autoIncrement
     * @column('name' => 'user_id', 'type' => 'int')
     * @size(11)
     * @notNull
     *
     * @var int
     */
    public $ID = null;

    /**
     * @column('name' => 'first_name', 'type' => 'string')
     * @size(255)
     * @notNull
     *
     * @var string
     */
    public $firstName = null;

    /**
     * @column('name' => 'last_name', 'type' => 'string')
     * @size(255)
     * @notNull
     *
     * @var string
     */
    public $lastName = null;

    /**
     * @column('name' => 'login', 'type' => 'string')
     * @size(15)
     * @notNull
     *
     * @var string
     */
    public $login = null;

    /**
     * @manyToOne(
     *     'entity' => 'TopicEntity',
     *     'column' => 'user_id',
     *     'referencedColumn' => 'author_id'
     * )
     *
     * @var TopicEntity[]
     */
    public $topicEntityCollection;
}
<?php

namespace MyApp\Entities;

/**
 * Class TopicEntity
 *
 * @entity('table' => 'topics')
 * @namedQuery('name' => 'findAll', 'query' => 'SELECT * FROM topics')
 * @namedQuery('findById', 'SELECT * FROM topics t WHERE t.topic_id = :id')
 * @namedQuery('findByUser', 'SELECT * FROM topics t WHERE t.author_id = :id')
 */
class TopicEntity extends \ElementaryFramework\LightQL\Entities\Entity
{
    /**
     * @id
     * @autoIncrement
     * @column('name' => 'topic_id', 'type' => 'int')
     * @size(11)
     * @notNull
     *
     * @var int
     */
    public $ID = null;

    /**
     * @column('name' => 'title', 'type' => 'string')
     * @size(255)
     * @notNull
     *
     * @var string
     */
    public $title = null;

    /**
     * @column('name' => 'content', 'type' => 'string')
     * @notNull
     *
     * @var string
     */
    public $text = null;

    /**
     * @oneToMany('entity' => 'UserEntity')
     *
     * @var UserEntity
     */
    public $userEntityReference;
}

3. 创建实体外观

<?php

namespace MyApp\Sessions;

use MyApp\Entities\UserEntity;

/**
 * Class UserFacade
 */
class UserFacade extends \ElementaryFramework\LightQL\Sessions\Facade
{
    /**
     * @persistenceUnit('myAppPersistenceUnit')
     *
     * @var \ElementaryFramework\LightQL\Entities\EntityManager
     */
    protected $entityManager;

    public function __construct()
    {
        // Constructs the base class with the entity class name managed by this facade
        parent::__construct(UserEntity::class);
    }
}
<?php

namespace MyApp\Sessions;

use MyApp\Entities\TopicEntity;

/**
 * Class TopicFacade
 */
class TopicFacade extends \ElementaryFramework\LightQL\Sessions\Facade
{
    /**
     * @persistenceUnit('myAppPersistenceUnit')
     *
     * @var \ElementaryFramework\LightQL\Entities\EntityManager
     */
    protected $entityManager;

    public function __construct()
    {
        // Constructs the base class with the entity class name managed by this facade
        parent::__construct(TopicEntity::class);
    }
}

4. 玩游戏!

<?php

namespace MyApp\Controllers;

use ElementaryFramework\LightQL\LightQL;
use ElementaryFramework\LightQL\Persistence\PersistenceUnit;

abstract class BaseController
{
    public function __construct()
    {
        // Register LightQL annotations
        LightQL::registerAnnotations();

        // Register persistence unit
        PersistenceUnit::register("myAppPersistenceUnit", __DIR__ . "/files/persistenceUnit.json");
    }

    public function renderView(string $view, array $data)
    {
        // Your logic to render static views
    }
}
<?php

namespace MyApp\Controllers;

use MyApp\Entities\TopicEntity;
use MyApp\Sessions\TopicFacade;

class TopicController extends BaseController
{
    private $_topicFacade;

    public function __construct()
    {
        parent::__construct();

        // Create a new facade
        $this->_topicFacade = new TopicFacade();
    }

    public function newTopic()
    {
        // Create a topic entity from form data
        $topic = new TopicEntity($_POST);
        // Insert the entity into the database
        $this->_topicFacade->create($topic);
    }

    public function editTopic()
    {
        // Get the original topic from the database
        $topic = $this->_topicFacade->find($_POST["topic_id"]);
        // Edit the topic with form data
        $topic->hydrate($_POST);
        // Update the data in the database
        $this->_topicFacade->edit($topic);
    }

    public function getTopics($start = null, $length = null)
    {
        $topics = array();

        if ($start === null && $length === null) {
            $topics = $this->_topicFacade->findAll();
        } else {
            $topics = $this->_topicFacade->findRange($start, $length);
        }

        $this->renderView("topics_page", array("topics" => $topics));
    }

    public function getTopicsOfUser($userId)
    {
        // Get the named query
        $query = $this->_topicFacade->getNamedQuery("findByUser");
        // Set query parameters
        $query->setParam("id", $userId);
        // Execute the query
        $query->run();
        // Retrieve results
        $topics = $query->getResults();

        $this->renderView("topics_page", array("topics" => $topics));
    }

    // etc...
}

您可以使用UserController做同样的事情

许可证

© 2018 - Aliens Group

在MIT许可下使用(阅读许可证