taeluf/rdb

在 beans 上自定义属性获取器,更好的错误报告,将 sql-verbs 作为 API。还有一些其他的生活质量提升功能。

0.2.1 2021-02-06 17:56 UTC

This package is auto-updated.

Last update: 2024-09-09 05:40:44 UTC


README

在 beans 上自定义属性获取器,更好的错误报告,将 sql-verbs 作为 API。还提供了一些额外的便利函数。

入门指南

安装: composer require taeluf/rdb 0.2.*v0.2.x-dev{"require":{"taeluf/rdb": "0.2.*"}}

我们使用 \RDB:: 而不是 \R::。关于这里未涵盖的内容,请参阅 Redbean 文档

1. 设置您的连接

// \RDB::setup('mysql:host=localhost;dbname=mydatabase', 'user', 'password' );  
\RDB::setup('sqlite:'.__DIR__.'/testdb.sqlite');  
//empty our recipe table at start of each test  
\RDB::delete('recipe',[]);  

2. 写入一些数据

\RDB::insert('recipe', ['name'=>'Tofu Scramble']);// <- convenience method in RDB  
\RDB::insert('recipe', ['name'=>'Vege Stirfry']);   
$loMein = \RDB::insert('recipe', ['name'=>'Lo Mein']);//also returns a bean  
$loMein->description = "A non-authentic Chinese noodle dish";  
// \RDB::store($loMein); // <- The redbean way  
$loMein->save();   // <- Convenience method in RDB  
\RDB::update('recipe', ['id'=>$loMein->id,'name'=>'Veggie Lo Mein']); //<-- Convenience method in RDB. Updates based on the id  
\RDB::update('recipe', ['name'=>'Veggie Stirfry'], ['name'=>'Vege Stirfry']); //<-- 2nd array is used as WHERE params for the update   
  
$list = \RDB::select('recipe', ['name'=>'Veggie Lo Mein']);  
$loadedLoMein = $list[0];  
  
$list2 = \RDB::select('recipe', ['name'=>'Veggie Stirfry']);  
$loadedStirfry = $list2[0];  

3. 创建一个模型(如果需要的话)

自定义 Redbean 模型 允许您向 redbean 添加功能。RDB 简化了某些事情。

// RDBModel namespace is required  
namespace RDBModel;  
  
class Recipe extends \RedBeanPHP\SimpleModel {  
  
    //Custom property getters MUST start with &get  
    public function &getSlug(){  
        $name = strtolower($this->name);  
        $parts = explode(' ', $name);  
        $slug = implode('-', $parts);  
        return $slug;  
    }  
}  

& 使用该模型

$tofu  = \RDB::select('recipe', ['name'=>'Tofu Scramble'])[0];  
$slug = $tofu->slug; // <- not stored in db  
$url = '/recipe/'.$slug.'/';  
  
return true  
    && $this->compare('tofu-scramble', $slug)  
;  

其他内容

  • 使用 $bean->props() 获取所有属性(仅数据库中的属性)
  • 使用 $bean->export()_type 添加到导出的数组中。
    • (未测试)调用 $bean->import($exported) 并将导出的数组加载到数据库中,多亏了 _type 的添加
  • 表名(类型)将被转换为小写,并移除非字母非下划线的字符,所以您不必过于小心传递给 RDB::find() 的类型。