Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Alexya\Database;
- /**
- * Base class for all ORM classes.
- *
- * This class provides helper methods for ORM related classes.
- *
- * To build your ORM classes they must extend this class and be located
- * in the package `\Application\ORM`, the class only needs database's
- * columns as properties. In order to use ORM your database MUST follow
- * the naming conventions located in `examples/Conventions/Database.md`.
- *
- * Once you have made your class you can call any of the static methods
- * to get the record from the database using the method {@see \Alexya\Database\ORM::get}.
- *
- * When you want to update the values on the database simply call the method
- * {@see \Alexya\Database\ORM::save}.
- *
- * Example:
- *
- * namespace Application\ORM
- * {
- * class Users extends \Alexya\Database\ORM
- * {
- * private $id;
- * private $name;
- * private $password;
- * private $email;
- * private $orders;
- * }
- * }
- *
- * $user = \Application\ORM\Users::get("1");
- * if($user->password == "test") {
- * $user->password = "[!52test]::";
- * }
- * if($user->name == "test") {
- * $user->name = "Test name";
- * }
- *
- * $user->save();
- *
- * $new_user = new \Application\ORM\Users();
- * $new_user->id = 2;
- * $new_user->name = "test2";
- * $new_user->password = "test2";
- * $new_user->email = "tes2@email.com";
- *
- * @author Manulaiko <manulaiko@gmail.com>
- *
- * @package \Alexya\Database
- */
- class ORM
- {
- /**
- * Builds and returns an instance of the ORM class.
- *
- * The parameter can be an integer, a string, an array or a callable.
- *
- * The integer parameter represents the amount of records to get
- * from database. It returns an array of ORM classes.
- *
- * Example:
- *
- * $users = \Application\ORM\Users::get(10);
- * // SELECT * FROM `users` LIMIT 10;
- *
- * The string parameter is the value of the primary key of the table.
- * It returns an instance of the ORM class.
- *
- * Example:
- *
- * $user = \Application\ORM\Users::get("1");
- * // SELECT * FROM `users` WHERE `id`=1;
- *
- * The string parameter represents the WHERE clause of the query.
- * It works the same way as {@see \Alexya\Database\QueryBuilder::where}.
- * If there are more than 1 record it will return an array of classes, if not
- * it will return the ORM class.
- *
- * Example:
- *
- * $users = \Application\ORM\Users::get([
- * "orders[>=]" => 10
- * ]);
- * // SELECT * FROM `users` WHERE `orders`>=10;
- *
- * If the parameter is a callable it should be a function that will filter
- * the array that will be returned. It works the same way as {@see \Alexya\Tools\Collection::filter}
- * If there are more than 1 record it will return an array of classes, if not
- * it will return the ORM class.
- *
- * Example:
- *
- * $users = \Application\ORM\Users::get(function($user) {
- * if($user["name"] == "test" || $user["password"] == "test") {
- * return true;
- * }
- *
- * return false;
- * });
- * //Although it doesn't generates a query it would look like this
- * // SELECT * FROM `users` WHERE `name`='test' OR `password`='test';
- *
- * @param int|string|callable $param The parameter on which will depend the resulting value
- *
- * @return array|\Alexya\Database\ORM An array if more than one record is found on database.
- * An instance of the ORM class if not
- */
- public static get($param)
- {
- global $Database;
- $class = get_called_class();
- $table = str_replace("\\", "_", str_replace("\\Application\\ORM\\", "", $class));
- $query = new QueryBuilder();
- $return = null;
- //Build appropiate query
- if(is_int($param) || is_double($param)) {
- $query->select()
- ->from($table)
- ->limit($param);
- } else if(is_array($param)) {
- $query->select()
- ->from($table)
- ->where($param);
- } else if(is_string($param)) {
- $query->select()
- ->from($table)
- ->where([
- "id" => $param
- ]);
- } else if(is_callable($param)) {
- $query->select()
- ->from($table);
- }
- $results = $Database->execute($query);
- //Check query result
- if(count($results) == 1) {
- $return = new $class($result[0]);
- } else {
- $return = [];
- //Build return array
- foreach($result as $res) {
- if(is_callable($param)) {
- if($param($res)) {
- $return[] = new $class($res);
- }
- } else {
- $return[] = new $class($res);
- }
- }
- }
- return $return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement