$connections = array( 'development' => 'mysql://invalid', 'production' => 'mysql://test:test@127.0.0.1/test' ); ActiveRecord\Config::initialize(function($cfg) use ($connections) { $cfg->set_model_directory('.'); $cfg->set_connections($connections); });
//Orm.php class Orm { /** * array $models_ , , * [ ]=>array('path'=> , 'namespace'=> ) */ public $models_ = array(); /** * PHP, , , * * @param null $name */ function __construct($name = null) { if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 50300) die('PHP ActiveRecord requires PHP 5.3 or higher'); define('PHP_ACTIVERECORD_VERSION_ID', '1.0'); include_once 'lib/Singleton.php'; include_once 'lib/Config.php'; include_once 'lib/Utils.php'; include_once 'lib/DateTime.php'; include_once 'lib/Model.php'; include_once 'lib/Table.php'; include_once 'lib/ConnectionManager.php'; include_once 'lib/Connection.php'; include_once 'lib/SQLBuilder.php'; include_once 'lib/Reflections.php'; include_once 'lib/Inflector.php'; include_once 'lib/CallBack.php'; include_once 'lib/Exceptions.php'; spl_autoload_register(__NAMESPACE__ . '\ActiveRecord::activerecord_autoload'); Config::initialize(function ($cfg) { $cfg->set_connections(array( 'development' => Configuration::$dbtype . "://" . Configuration::$db_user . ":" . Configuration::$db_password . "@" . Configuration::$db_host . "/" . Configuration::$db_name )); /* , AR, "Ymd H:i:s T" , datetime MySQL */ $cfg->set_date_format("Ymd H:i:s"); }); } /** , , FALSE */ public function getModel($model) { $config = Config::instance(); if (array_key_exists($model, $this->models_)) { $config->set_model_directory($this->models_[$model]['path']); if( $this->models_[$model]['namespace'] ) $class = "\\" . $this->models_[$model]['namespace'] . "\\" . $model; else $class = $model; return new $class; } else { return false; } } /** , getModel() $class_name , NEW getModel() */ public static function activerecord_autoload($class_name) { $root = Config::instance()->get_model_directory(); $class_name = explode('\\', $class_name); $class_name = end($class_name); $file = $root . $class_name . ".php"; if (file_exists($file)) require $file; } } //Model.php class Model extends \ActiveRecord\Model { /* , , */ static $table_name = 'simple_name'; // static $primary_key = 'id'; // static $connection = 'production'; // , SQL - db.table_name static $db = 'test'; /* * */ }
class Configuration{ /*.....*/ /** * $db_host */ static $db_host = 'localhost'; /** * $db_user */ static $db_user = 'root'; /** * $db_password */ static $db_password = 'root'; /** * $db_name */ static $db_name = 'db_name'; /** * $dbtype */ static $dbtype = 'mysql'; /*.....*/ }
return new DateTime($value->format('Ymd H:i:s T'))
return new DateTime($value->format(Config::instance()->get_date_format()))
<?php class Controller{ public static $ORM; function __construct(){ $this->loadOrm(); } function loadOrm(){ include 'Orm.php' self::$ORM = new Orm(); self::_models = array('Book'=>array('path'=>'models', 'namespace'=>__NAMESPACE__)); } } new Controller; ?> // <?php $model = Controller::$ORM ->getModel('Book'); $books = $model->all(); foreach($books as $book) echo $book->author;
Source: https://habr.com/ru/post/211913/
All Articles