arojunior / php-orm-pdo
微型PHP ORM库
1.1
2017-03-14 11:35 UTC
Requires
- php: >=5.6.0
This package is auto-updated.
Last update: 2024-09-16 07:01:24 UTC
README
通过Composer安装并作为库使用
composer require arojunior/php-orm-pdo
创建一个文件以覆盖数据库配置
use SimpleORM\core\model\Model; class AppModel extends Model { public $db_config = [ 'db_host' => '192.168.1.1', 'db_name' => 'test', 'db_user' => 'root', 'db_pass' => '' ]; }
然后您可以在自己的类中扩展这个类
use YourNamespace\AppModel; class Example extends AppModel { public $table = 't_user'; public $pk = 'user_id'; public function getAll() { return $this->findAll(); } }
CRUD
namespace SimpleORM\app\model; use SimpleORM\core\model\Model; class Users extends Model { /* * * Basic configuration * These arguments are optionals * protected $table = 'users'; //just if the class name a table name are different * protected $pk = 'id'; //just if the primary key name is not id */ }
创建新用户(不检查)
$this->Users->create([ 'name' => 'Junior Oliveira', 'email' => 'arojunior@gmail.com' ]);
让ORM选择是创建还是更新。ORM将在决定是否创建或更新数据之前执行find方法
保存数据
$this->Users->save([ 'id' => 1, 'name' => 'Junior Oliveira' ]);
获取id
$this->Users->lastSavedId();
更新id为1的用户
$this->Users->update([ 'id' => 1, 'email' => 'contato@arojunior.com' ]);
删除
$this->Users->delete(['id' => 1]);
读取
$this->Users->findAll(); // fetchAll $this->Users->findOne(['email' => 'arojunior@gmail.com']); $this->Users->findById($id);
检查
$this->Users->exists($id);
如果是true,您可以使用以下方式获取数据:
$this->Users->fetch();
作为框架使用时的功能
- CRUD函数
- 在控制器中自动加载模型类
- 要使用自动功能,您应该使用文件名和结构约定
- 只需按照示例文件 /controller/UsersController.php
- 所有位于 /app/controllers 文件夹中的控制器
- 所有位于 /app/models 文件夹中的模型
约定
- 所有位于 /app/controller 路径中的控制器
- 所有位于 /app/model 路径中的模型
- 所有位于 /app/view 路径中的视图
- 文件名和类名必须同名