gigorok/php-orm

开源 ORM 库

0.2.3 2014-04-24 16:06 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:03:18 UTC


README

=============================

PHP 的开源 ORM 库。

简介

PHP-ORM 遵循 ActiveRecord 架构模式。

更多详情请查看 这里

最低要求

  • PHP 5.4+
  • 您数据库的 PDO 驱动

支持数据库

  • MySQL
  • PostgreSQL

特性

  • 查找方法
  • 写入方法
  • 关系
  • 验证
  • 回调
  • 事务
  • 支持多种适配器
  • 表的架构

安装

使用 composer 安装 PHP ORM 库。只需将以下文本添加到您的 composer.json 文件中,并运行 php composer.phar update 命令即可安装

{
    "require": {
        "gigorok/php-orm": "0.2.*"
    }
}

基本 CRUD

检索

这些都是您查找和从数据库中检索记录的基本方法。

$post = Post::find(1);
echo $post->title; # 'Test title!'
echo $post->author_id; # 5

# also the same since it is the first record in the db
$post = Post::first();

# finding using a conditions array
$posts = Post::where('name=? or id > ?', array('The Bridge Builder', 100));

创建

在这里,我们通过创建一个新对象并调用 save() 方法来创建一个新帖子。

$post = new Post();
$post->title = 'My first blog post!!';
$post->author_id = 5;
$post->save();
# INSERT INTO `posts` (title,author_id) VALUES('My first blog post!!', 5)

更新

要更新,您只需要先找到一条记录,然后更改其属性之一。

$post = Post::find(1);
echo $post->title; # 'My first blog post!!'
$post->title = 'Some real title';
$post->save();
# UPDATE `posts` SET title='Some real title' WHERE id=1

$post->title = 'New real title';
$post->author_id = 1;
$post->save();
# UPDATE `posts` SET title='New real title', author_id=1 WHERE id=1

删除

删除记录不会 销毁 对象。这意味着它将调用 SQL 删除您数据库中的记录,但您仍然可以使用该对象。

$post = Post::find(1);
$post->destroy();
# DELETE FROM `posts` WHERE id=1
echo $post->title; # 'New real title'

许可证

MIT 许可证。