ganga / potato-orm
potato-orm 是一个 SQLite ORM 包。
该包的官方仓库似乎已消失,因此该包已被冻结。
v1.0.1
2016-01-07 10:41 UTC
Requires
- php: >=5.6.0
- vlucas/phpdotenv: ^2.2
Requires (Dev)
- phpunit/phpunit: ^5.1
- satooshi/php-coveralls: ^0.7.0
- scrutinizer/ocular: ^1.3
This package is not auto-updated.
Last update: 2019-05-21 16:54:06 UTC
README
POTATO ORM 是一个 SQL 数据库 ORM,能够快速且无缝地与 SQL 数据库交互。已在 SQLite、MySQL 和 PgSQL 上进行了测试。
安装
通过 Composer
$ composer require ganga/potato-orm
用法
SQLite 配置
注意:在 SQLite 中,如果不存在,则数据库将在根目录中创建。
$sqliteConfig = [ "type" => "sqlite", //case insensitive "database" = "test.db" //path to db file ];
其他 SQL 数据库配置
注意:建议从 .env
文件中加载配置设置。如果您根目录中没有 .env
文件或不知道它,请阅读此 phpdotenv 项目。
在 SQL 数据库配置中提供 数据库类型
、数据库名
、数据库用户
、数据库密码
和 主机
。
// Load the `.env` variables for the project // Check the `.env.example` file in the root of the project to see the environment variables needed. $dotenv = new Dotenv\Dotenv(__DIR__); $dotenv->load();
创建配置并连接到数据库
$mysqlConfig = [ "type" => getenv('DB_TYPE'), "database" => getenv('DB_NAME'), "user" => getenv('DB_USER'), "password" => getenv('DB_PASS'), "host" => getenv('DB_HOST') ];
一旦您有了一个配置,无论是 SQLite 还是任何其他 SQL 数据库,请建立连接。
$conn = new Connection($mysqlConfig);
一旦我们有了连接,我们创建模型
// By declaring the class name, we check for // a plural name of the class as the table name // in this case the table name is users class User extends Potato { }
数据库交互
// create a new user $user = new User; $user->name = "Ganga Christopher"; $user->age = 23; $user->address = "Kindaruma 525"; $user->company = "Andela"; $user->save();
从用户表获取所有用户
$users = User::getAll(); // Returns an array of the users found in the db $users = User::getAll(['name', 'age']); // Selects only the name and age fields from the database table
从用户表获取单个用户
$user = User::getOne(2); // 2 represents the id of the user to be retrieved $user = User::getOne(2, ['name', 'age']); // Selects only the name and age fields from the database table
编辑现有用户
注意:严格使用 User::find($id);
来查找要更新的用户。要查找并返回用户,请使用 User::getOne($id);
$user = User::find(2); $user->name = "Devy Kerr"; $user->address = "466 Video Productions"; $user->save();
删除用户
//returns true or false based on success of the query User::destroy(2); // 2 represents the id of the user to be deleted
测试
$ composer test
致谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。