Advertisement
Guest User

Alexya's ORM class

a guest
Feb 22nd, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.22 KB | None | 0 0
  1. <?php
  2. namespace Alexya\Database;
  3.  
  4. /**
  5.  * Base class for all ORM classes.
  6.  *
  7.  * This class provides helper methods for ORM related classes.
  8.  *
  9.  * To build your ORM classes they must extend this class and be located
  10.  * in the package `\Application\ORM`, the class only needs database's
  11.  * columns as properties. In order to use ORM your database MUST follow
  12.  * the naming conventions located in `examples/Conventions/Database.md`.
  13.  *
  14.  * Once you have made your class you can call any of the static methods
  15.  * to get the record from the database using the method {@see \Alexya\Database\ORM::get}.
  16.  *
  17.  * When you want to update the values on the database simply call the method
  18.  * {@see \Alexya\Database\ORM::save}.
  19.  *
  20.  * Example:
  21.  *
  22.  *     namespace Application\ORM
  23.  *     {
  24.  *         class Users extends \Alexya\Database\ORM
  25.  *         {
  26.  *             private $id;
  27.  *             private $name;
  28.  *             private $password;
  29.  *             private $email;
  30.  *             private $orders;
  31.  *         }
  32.  *     }
  33.  *
  34.  *     $user = \Application\ORM\Users::get("1");
  35.  *     if($user->password == "test") {
  36.  *         $user->password = "[!52test]::";
  37.  *     }
  38.  *     if($user->name == "test") {
  39.  *         $user->name = "Test name";
  40.  *     }
  41.  *
  42.  *     $user->save();
  43.  *
  44.  *     $new_user = new \Application\ORM\Users();
  45.  *     $new_user->id       = 2;
  46.  *     $new_user->name     = "test2";
  47.  *     $new_user->password = "test2";
  48.  *     $new_user->email    = "tes2@email.com";
  49.  *
  50.  * @author Manulaiko <manulaiko@gmail.com>
  51.  *
  52.  * @package \Alexya\Database
  53.  */
  54. class ORM
  55. {
  56.     /**
  57.      * Builds and returns an instance of the ORM class.
  58.      *
  59.      * The parameter can be an integer, a string, an array or a callable.
  60.      *
  61.      * The integer parameter represents the amount of records to get
  62.      * from database. It returns an array of ORM classes.
  63.      *
  64.      * Example:
  65.      *
  66.      *     $users = \Application\ORM\Users::get(10);
  67.      *     // SELECT * FROM `users` LIMIT 10;
  68.      *
  69.      * The string parameter is the value of the primary key of the table.
  70.      * It returns an instance of the ORM class.
  71.      *
  72.      * Example:
  73.      *
  74.      *     $user = \Application\ORM\Users::get("1");
  75.      *     // SELECT * FROM `users` WHERE `id`=1;
  76.      *
  77.      * The string parameter represents the WHERE clause of the query.
  78.      * It works the same way as {@see \Alexya\Database\QueryBuilder::where}.
  79.      * If there are more than 1 record it will return an array of classes, if not
  80.      * it will return the ORM class.
  81.      *
  82.      * Example:
  83.      *
  84.      *     $users = \Application\ORM\Users::get([
  85.      *         "orders[>=]" => 10
  86.      *     ]);
  87.      *     // SELECT * FROM `users` WHERE `orders`>=10;
  88.      *
  89.      * If the parameter is a callable it should be a function that will filter
  90.      * the array that will be returned. It works the same way as {@see \Alexya\Tools\Collection::filter}
  91.      * If there are more than 1 record it will return an array of classes, if not
  92.      * it will return the ORM class.
  93.      *
  94.      * Example:
  95.      *
  96.      *     $users = \Application\ORM\Users::get(function($user) {
  97.      *         if($user["name"] == "test" || $user["password"] == "test") {
  98.      *             return true;
  99.      *         }
  100.      *
  101.      *          return false;
  102.      *     });
  103.      *     //Although it doesn't generates a query it would look like this
  104.      *     // SELECT * FROM `users` WHERE `name`='test' OR `password`='test';
  105.      *
  106.      * @param int|string|callable $param The parameter on which will depend the resulting value
  107.      *
  108.      * @return array|\Alexya\Database\ORM An array if more than one record is found on database.
  109.      *                                    An instance of the ORM class if not
  110.      */
  111.     public static get($param)
  112.     {
  113.         global $Database;
  114.  
  115.         $class  = get_called_class();
  116.         $table  = str_replace("\\", "_", str_replace("\\Application\\ORM\\", "", $class));
  117.         $query  = new QueryBuilder();
  118.         $return = null;
  119.  
  120.         //Build appropiate query
  121.         if(is_int($param) || is_double($param)) {
  122.             $query->select()
  123.                   ->from($table)
  124.                   ->limit($param);
  125.         } else if(is_array($param)) {
  126.             $query->select()
  127.                   ->from($table)
  128.                   ->where($param);
  129.         } else if(is_string($param)) {
  130.             $query->select()
  131.                   ->from($table)
  132.                   ->where([
  133.                         "id" => $param
  134.                     ]);
  135.         } else if(is_callable($param)) {
  136.             $query->select()
  137.                   ->from($table);
  138.         }
  139.  
  140.         $results = $Database->execute($query);
  141.  
  142.         //Check query result
  143.         if(count($results) == 1) {
  144.             $return = new $class($result[0]);
  145.         } else {
  146.             $return = [];
  147.  
  148.             //Build return array
  149.             foreach($result as $res) {
  150.                 if(is_callable($param)) {
  151.                     if($param($res)) {
  152.                         $return[] = new $class($res);
  153.                     }
  154.                 } else {
  155.                     $return[] = new $class($res);
  156.                 }
  157.             }
  158.         }
  159.  
  160.         return $return;
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement