PHP code example of elementaryframework / light-ql
1. Go to this page and download the library: Download elementaryframework/light-ql library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
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;
}
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);
}
}
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);
}
}
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
}
}
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...
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.