What is an Algorithm for ORM?

I searched for ORM for use in php / mysql application. However, none of them attracted my attention with past hello world trials. So, I decided to do some research and try to code my own ORM. However, I was unable to find resources that explain at the code level how to handle db relationships. The concept of ORM is understandable, but trying to lay it out in code, I do not know which approach is best. Is it better to build several small queries or build one complex query for each possible scenario? Any understanding of the ORM algorithm or architecture is welcome!

+3
source share
3 answers

Ok, let's create an ORM structure on the fly. As you tagged the php tag, let's code it in PHP.

But before we write it, we need to know some basic concepts or some basic terminology about surrounding objects. In this example, we will have:

  • ORM structure. The ORM framework takes responsibility for taking care of server connections and server connection abstractions. (The full orm framework also supports automatic mapping of classes to tables).

  • . . , , . (. ORM . http://dbphp.net Doctrine ).

  • - - . - , -.

- . , , -:

<?php
class user
{
    public $id;
    public $name
    public function __construct ($name=null)
    {
        $this->name = $name;
    }
}
?>

, - , . , . , .

-, ORM:

<?php

//The connection link which can be changed any time
class link
{
    public $link;
    public function __construct ($hostname, $database, $username, $password)
    {
        $this->link = new \PDO ('mysql:host='.$hostname.';dbname='.$database, $username, $password);
        $this->link->query('use '.$database);
    }
    public function fetch ($query)
    {
        $result = $this->link->query($query)->fetch();
    }
    public function query ($query)
    {
        return $this->link->query($query);
    }
    public function error ()
    {
        return $this->link->errorInfo();
    }
}

//A structure which collects all link(s) and table/class handlers togather
class database
{
    public $link;
    public $tables = array ();
    public function __construct ($link)
    {
        $this->link = $link;
        table::$database = $this;
    }
}

//A basic table handler class
//In recent ORM frameworks they do all the default mappings
class table
{
    public static $database;
}
?>

, ORM . , .

, ORM, -.

, . , , :

<?php
class users extends table
{
    public function create ($row)
    {
        $return = new user ();
        $return->id = $row[0];
        $return->name = $row[1];
        var_export($row);
        return $return;
    }
    public function load ($id=null)
    {
        if ($id==null)
        {
            $result = self::$database->link->fetch("select * from users");
            if ($result)
            {
                $return = array();
                foreach ($result as $row)
                {
                    $return[$row[0]] = $this->create($row);
                }
                return $return;
            }
        }
        else
        {
            $result = self::$database->link->fetch("select * from users where id='".$id."'");
            if ($result)
            {
                return $this->create(reset($result));
            }
            else
            {
                echo ("no result");
            }
        }
    }
    public function save ($user)
    {
        if (is_array($save))
        {
            foreach ($save as $item) $this->save ($item);
        }
        if ($user->id==null)
        {
            return self::$database->link->query("insert into users set
                                                 name='".$user->name."'");
        }
        else
        {
            return self::$database->link->query("update users set name='".$user->name."'
                                                 where id='".$user->id."'");
        }
    }
    public function delete ($user)
    {
        self::$database->link->query ("delete from users where id='".$user->id."'");
    }
}
?>
  • , $database
  • sql.
  • .
  • .

:

<?
$database = new database (new link('127.0.0.1', 'system_db', 'root', '1234'));
$database->tables['users'] = new users();

if (!$database->tables['users']->save (new user('Admin')))
{
    var_export($database->link->error());
}

var_export($database->tables['users']->load(2));
?>

php ORM,

+2

ORM Active Record pattern . , , , :

, "":

$user_list = Users.getAllUsers(); //SELECT * FROM Users
//foreach row, create a new User() instance
$user_list[0].name = "Peter";
//UPDATE Users SET name = 'Peter' WHERE user_id = <$user_list[0].id>
$user_list[0].commit(); 

, , .

+1

ORM . ORM , . , , , , .

CRUD- . , . .. , .

, , .

, ORM, , . , ORM, DAO- SQL, , .

Honestly, adopting an ORM is complicated enough to justify, depending on the size of the project. Writing your own funds is most likely not worth the investment.

+1
source

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


All Articles