📜 ⬆️ ⬇️

Entity “framework” for PHP from one class

Since the development of technology has led to the fact that every programmer now has his own computer, as a side effect, we have thousands of various libraries, frameworks, services, API, etc. for all occasions. But when this case of life comes, the problem arises - what to use and what to do if it is not quite suitable - to rewrite, write your own from scratch or fasten several solutions for different use cases.

I think many have noticed that often creating a project is reduced not so much to programming as to writing the code for integrating several ready-made solutions. Sometimes such combinations turn into new solutions that can be repeatedly used in subsequent tasks.

Let us turn to a specific "running" task - the object layer for working with databases in PHP. There are a lot of solutions, starting from PDO and ending with multi-level (and, in my opinion, not quite relevant in PHP) ORM engines.

Most of these solutions migrated to PHP from other platforms. But often the authors do not take into account the features of PHP, which would dramatically simplify both the writing and the use of portable constructs.
One of the common architectures for this class of tasks is the Active Record pattern. In particular, the so-called Entity (entities) are used in this pattern, in one form or another, used in a number of platforms, ranging from persistent bins in EJB3 to EF in .NET.
')
So, let's build a similar construction for PHP. Connect between two cool things - the finished ADODB library and the weakly typed and dynamic properties of objects in the PHP language.
One of the many features of ADODB is the so-called auto-generation of SQL queries for insertion (INSERT) and update (UPDATE) of records based on associative arrays with data.
Actually there is nothing military to take an array, where the keys are the names of the fields and the values ​​are respectively the data and generate the SQL query string. But ADODB does it more intelligently. The query is based on the structure of the table, which is previously read from the database schema. As a result, firstly, only existing fields are included in sql, and secondly, the field type is taken into account - quotation marks are added for strings, date formats can be formed based on the timestamp if ADODB sees it instead of a string in the transmitted value, etc. .

Now we will come from PHP.
Imagine such a class (simplified).

class Entity{
   protected $fields = array();
   public final function __set($name, $value) {
        $this->fields[$name] = $value;
   }
   public final function __get($name) {
        return $this->fields[$name];
   }

}


ADODB SQL , , XML . . , , , .

, .
Gist. , . , — , .

, :

CREATE TABLE   `users` (
  `username` varchar(255) ,
  `created` date  ,
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`user_id`)
)

— ADODB .

, Entity

/**
 * @table=users
 * @keyfield=user_id
 */
class User extends Entity{

}


.
:


$user = new User();
$user->username=' ';
$user->created=time();
$user->save(); //   

//   
$thesameuser = User::load($user->user_id);
echo $thesameuser ->username;



.
( , view=usersview) , , . . getMetatada() .

Entity ?

, init(), Entity, .
afterLoad(), , timestamp .
.

/**
 * @table=users
 * @view=usersview
 * @keyfield=user_id
 */
class User extends Entity{
    protected function init() {
        $this->created = time();
    }
    protected function afterLoad() {
        $this->created = strtotime($this->created);
    }
}


beforeSave beforeDelete , , — , .

( WHERE ).
$users = User::load("username like '' ");

Entity , «» SQL . , . , ( user_id, ), . , , , , . . EF -.

, AR. — . , Entity Manager .

— , , .

— PHP , ( Entity) -. , - .

? , , — , , , . , , ( 99.9%) . , , - .

P.S. GitHub

Source: https://habr.com/ru/post/282844/


All Articles