packanhu / myorm
一个简单的用于MySQL数据库的PHP ORM
v1.0.0
2018-07-11 19:10 UTC
Requires
- php: >=5.5
This package is not auto-updated.
Last update: 2024-09-26 21:26:14 UTC
README
packanhu/myorm 是一个简单且可扩展的PHP ORM库,用于与MySQL数据库一起工作。它具有一个可表达的查询构建器,可以支持多表读取数据。
安装
使用composer安装 composer require pakanhu/myorm
使用方法
首先,创建一个 "Database" 实例。它会创建一个静态数据库连接,该连接将被 "Model" 内部使用。
new \paKanhu\MyORM\Database( 'db_host', 'db_name', 'db_username', 'db_password', 'db_timeZone' );
从 "paKanhu\MyORM\Model" 扩展你的模型。数据库列名应使用蛇形大小写,而类中的对应属性应使用驼峰大小写。
class Product extends \paKanhu\MyORM\Model { protected $id; // DB Column Name - id (Primary Key) protected $sku; // DB Column Name - sku protected $name; // DB Column Name - name protected $price; // DB Column Name - price protected $categoryId; // DB Column Name - category_id protected $createdAt; // DB Column Name - created_at // Table name is mandatory protected static $tableName = 'products'; // Define if primary key in your table is different than "id" // protected static $primaryColumn = 'sku'; /* Define if table contains fractional timestamp. This is required for workaround a PHP PDO bug while dealing with fractional timestamp in MySQL (TIMESTAMP(>0)). If it's defined a new property named 'createdAtFractional' will be created having fractional timestamp value. */ // protected static $fractionalTimestamps = ['createdAt']; // Define to direct access of property outside of class // protected static $guarded = ['categoryId']; } // To get product with id 100 $product = Product::getById(100); // To get product with unique sku SKU-123 $product = Product::getBySku('SKU-123', ['isUnique' => true]); // To get all products of category id 10 $products = Product::getByCategoryId(10); // is same as $products = Product::getAll([ 'where' => [[ 'column' => 'categoryId', // Property name 'value' => 10, // 'condition' => '!=', // MySQL condition (Default is =) ]] ]); // To get 5 new products of category id 10 $products = Product::getAll([ 'where' => [[ 'column' => 'categoryId', 'value' => 10, ]], 'orderBy' => [ 'column' => 'createdAt', 'mode' => 'DESC' ], 'limit' => [ 'rowCount' => 5, // 'offset' => 10, // To set offset (Default is 0) ] ]); // To get all products of category id 10 with category details $products = Product::getByCategoryId(10, [ 'with' => [[ 'column' => 'categoryId', 'class' => Category::class, 'property' => 'category', // Required when table is not joined with primary key of other table // 'foreignColumn' => 'categoryUniqueId', // Required for one-to-many relationship (Default value - true) // 'isUnique' => false, // Complete 'filters' array can be passed as it's in 'getAll' method // 'filters' => [], ]] ]); // echo $products[0]->category->name; // To get all products of category id 10 with only category name $products = Product::getByCategoryId(10, [ 'with' => [[ 'column' => 'categoryId', 'class' => Category::class, 'property' => 'category', 'filters' => [ // If 'select' filter is used, then result will be in array 'select' => ['name'] ], ]] ]); // echo $products[0]->category['name']; // To get all product names $products = Product::getAll([ 'select' => 'name', 'onlyValues' => true ]); // echo $products[0]; // To get all products of category id 10, 11, 12 AND name starts with 'Apple' $products = Product::getAll([ 'where' => [[ 'column' => 'categoryId', 'condition' => 'IN' 'value' => [10, 11, 12], ], [ 'column' => 'name', 'condition' => 'LIKE', 'value' => 'Apple%', ]] ]); // To get all products of category id 10, 11, 12 OR name starts with 'Apple' $products = Product::getAll([ 'whereOr' => [[ 'column' => 'categoryId', 'condition' => 'IN' 'value' => [10, 11, 12], ], [ 'column' => 'name', 'condition' => 'LIKE', 'value' => 'Apple%', ]] ]); /* To get all products of category id 10 and name starts with 'Apple' OR category id 11 and name starts with 'Google'. SQL Query Representation: WHERE (category_id = 10 AND name LIKE 'Apple%') OR (category_id = 11 AND name LIKE 'Google%') */ $products = Product::getAll([ 'where' => [ 'operator' => 'OR', 'operands' => [[ 'operator' => 'AND', 'operands' => [[ 'column' => 'categoryId', 'value' => 10 ], [ 'column' => 'name', 'condition' => 'LIKE', 'value' => 'Apple%' ]] ], [ 'operator' => 'AND', 'operands' => [[ 'column' => 'categoryId', 'value' => 11 ], [ 'column' => 'name', 'condition' => 'LIKE', 'value' => 'Google%' ]] ]] ] ]);