2017 © Pedro Peláez
 

library light-ql

The lightweight PHP ORM

image

elementaryframework/light-ql

The lightweight PHP ORM

  • Wednesday, July 11, 2018
  • by na2axl
  • Repository
  • 1 Watchers
  • 1 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

LightQL

The lightweight PHP ORM, (*1)

LightQL is an Object Oriented Mapper (ORM) based on Query Object and Data Mapper design patterns. It uses Annotations to help you create, edit, delete, find entities and much more without writing a SQL query., (*2)

Example

1. Create a persistence unit

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

2. Create entities

<?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. Create entity Facades

<?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. Play the game !

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

You can do the same with an UserController, (*3)

License

© 2018 - Aliens Group, (*4)

Licensed under MIT (read license), (*5)

The Versions

11/07 2018

dev-master

9999999-dev

The lightweight PHP ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database pdo

20/05 2018

v1.0.0

1.0.0.0

The lightweight PHP ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database pdo

20/05 2018

dev-develop

dev-develop

The lightweight PHP ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database pdo

22/04 2018

v0.0.1

0.0.1.0

The lightweight PHP ORM

  Sources   Download

MIT

The Requires

  • php ^7.1.10

 

The Development Requires

orm database pdo