Medoo 微框架的细节小坑

1. insert() 返回值

结论:insert() 、update()、 delete() 三个函数都将会返回一个pdo对象。

使用 Medoo 1.4.5 微框架时,根据中文文档,调用 $database->insert() 函数时,返回值如下:

Return: [number] 返回插入的id

该返回似乎与 1.2 的旧版本一致,但实际返回值不是id,

而是一个PDOStatement对象

,在文档下方有详细说明:

PDOStatement
insert()返回的是PDOStatement对象,你可以通过它获取更多信息

$data = $database->insert("account", [
    "user_name" => "foo",
    "email" => "foo@bar.com",
    "age" => 25
]);
// Returns the number of rows affected by the last SQL statement
echo $data->rowCount();
// Read more: http://php.net/manual/en/class.pdostatement.php

中文文档地址:http://medoo.lvtao.net/1.2/doc.insert.php

而进入Medoo官网,在新文档的首页,官网有明显提示新版本与旧版本不同的地方,其中insert()update()delete() 三个函数都将会返回一个pdo对象。

Update, Insert And Delete Return Result
update(), insert() and delete() method will return the PDO::Statement object instead of number of affected row since 1.4. You can call its method for more special usage.

对比官方的 英文文档,调用 $database->insert() 函数时,返回值如下:

Return: [PDOStatement] The PDOStatement object.

而如果想要得到插入的id,则需要调用 medoo对象的id()函数.

For old version, the API insert() will return the last inserted row ID by default. But for Medoo 1.2, you have to call the new API id() alone to get the ID.
$database->insert("account", [
    "user_name" => "foo",
    "email" => "foo@bar.com",
    "age" => 25
]);

//插入的id
$account_id = $database->id();
$pdo = $database->insert("account", [
    "user_name" => "foo",
    "email" => "foo@bar.com",
    "age" => 25
]);

 //影响的行数
$effectedRow = $pdo->rowCount()
//插入的id
$insert_id = $database->id();

这非常显然是中文文档翻译时的疏忽了,参考文档还是尽量找英文原版吧。


2. get() 返回值

结论:字段参数直接一个string填入,则直接返回字段值;字段参数以数组填入,则返回关联数组

根据 Medoo 1.4.5 微框架文档,$database->get() 返回值为

Return: [string/array/int/object]
Return the data of the column.

get()的返回值会根据字段参数进行调整。

如果直接填一个字段参数,返回的结果类型取决于该字段在表中的类型。例如:

//直接填入一个参数
$email = $database->get("account", "email", [
    "user_id" => 1234
]);
// $email = "foo@bar.com"  
// 类型: string


$point= $database->get("account", "point", [
    "user_id" => 1234
]);
// $point= 100  
// 类型: int

如果以['字段1','字段2']填写1个或多个参数,返回的结果为数组。
特别要注意的是,只要以中括号形式填写参数,即使只填入一个参数,也会返回数组。例如:

//数组形式 一个参数
$data = $database->get("account", [
    "email"
], [
    "user_id" => 1234
]);
// $data = array("email"=> "foo@bar.com"); 
// 类型: array

//数组形式 多个参数
$profile = $database->get("account", [
    "email",
    "gender",
    "location"
], [
    "user_id" => 1234
]); 
// $profile = array(
//  "email" => "foo@bar.com",
//  "gender" => "female",
//  "location" => "earth"
// );

3. 连表查询别名问题

如果在表中指定了别名,而两个表存在重复的字段名,那么连表的时候是一定要加上别名前缀的。


$data = $database->select($table.'(p)',//第一个表此处指定了别名
            [
                "[>]$table_h(h)" => [
                    "p.id" => "pid"
                    /** 指定别名后,join此处第一个表需要用别名
                     *  而第二个表,不能用别名
                     *  p表中和h表中都有id字段
                     *  如果这里写 
                     *  "id" => "pid" 
                     * 或 "p.id"=>"h.id" 都会报错
                    **/
                ]
            ],
            [
                'p.id',
                'p.title_text',
                'p.title_pic',
                'p.answers',
                'p.language',
                'p.classification',
                'p.pro_type',
                'p.pro_source',
                'h.hint'
                //同理,避免部分相同字段名出现问题,都需要加上别名

            ], [
                'AND' => [
                    'p.id' => $pid,
                    'p.visible[!]' => 0
                ]
            ]);

参考

Medoo中文网站:【已过时】
http://medoo.lvtao.net/

Medoo英文官方网站:
https://medoo.in/

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值