taq / torm
基于 ActiveRecord 的 ORM
1.3.4
2020-01-24 19:37 UTC
Requires
- php: >=5.3.0
- taq/pdooci: ^1.0
This package is not auto-updated.
Last update: 2024-09-24 09:31:06 UTC
README
这只是另一个简单的 PHP ORM。你可以使用它,但不要问我为什么我做了它。对吧?:-)
用法
请查看Github Wiki以获取文档,但让我在这里展示基础知识
<?php // open the PDO connection and set it $con = new PDO("sqlite:database.sqlite3"); TORM\Connection::setConnection($con); TORM\Connection::setDriver("sqlite"); // define your models - an User class will connect to an users table class User extends TORM\Model {}; class Ticket extends TORM\Model {}; // create some validations User::validates("name", ["presence" => true]); User::validates("email", ["presence" => true]); User::validates("email", ["uniqueness" => true]); User::validates("id", ["numericality" => true]); // create some relations User::hasMany("tickets"); User::hasOne("account"); Ticket::belongsTo("user"); // this will create a new user $user = new User(); $user->name = "John Doe"; $user->email = "john@doe.com"; $user->level = 1; $user->save(); // this will find the user using its primary key $user = User::find(1); // find some users $users = User::where(["level" => 1]); // find some users, using more complex expressions // the array first element is the query, the rest are values $users = User::where(["level >= ?", 1]); // updating users User::where(["level" => 1])->updateAttributes(["level" => 3]); // using fluent queries $users = User::where(["level" => 1])->limit(5)->order("name desc"); // listing the user tickets foreach($user->tickets as $ticket) { echo $ticket->description; } // show user account info echo $user->account->number; ?>
测试
SQLite
首先,使用 composer update
确保所有包都正常。然后,进入 test
目录并运行 run
。它需要 SQLite 驱动程序,所以请确保它可用。如果不可用,请检查使用以下命令找到的 php.ini
目录:
$ php -r 'phpinfo();' | grep 'php.ini' Configuration File (php.ini) Path => /etc/php/7.1/cli Loaded Configuration File => /etc/php/7.1/cli/php.ini
如果在该目录或与 php.ini
文件相同的 conf.d
目录中找不到,则可以使用以下命令在 Ubuntu 上安装:
$ sudo apt install php-sqlite3
多字节字符串、区域设置和 YAML
$ sudo apt install php-mbstring php-intl php-yaml