PHP code example of bingcool / swoolefy-orm
1. Go to this page and download the library: Download bingcool/swoolefy-orm library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bingcool / swoolefy-orm example snippets
Db::table('user')
->insert([
'name' => 'thinkphp',
'email' => '[email protected] ',
]);
$user = new User;
$user->name = 'thinkphp';
$user->email = '[email protected] ';
$user->save();
$user = new User;
$user->save([
'name' => 'thinkphp',
'email' => '[email protected] ',
]);
$user = Db::table('user')
->where('id', 1)
->find();
// 或者
$user = Db::table('user')
->find(1);
echo $user['id'];
echo $user['name'];
$user = User::get(1);
echo $user->id;
echo $user->name;
// 查询用户数据集
$users = User::where('id', '>', 1)
->limit(5)
->select();
// 遍历读取用户数据
foreach ($users as $user) {
echo $user->id;
echo $user->name;
}
Db::table('user')
->where('id', 1)
->update([
'name' => 'topthink',
'email' => '[email protected] ',
]);
$user = User::get(1);
$user->name = 'topthink';
$user->email = '[email protected] ';
$user->save();
$user = User::get(1);
$user->save([
'name' => 'topthink',
'email' => '[email protected] ',
]);
User::update([
'name' => 'topthink',
'email' => '[email protected] ',
], ['id' => 1]);
Db::table('user')->delete(1);
$user = User::get(1);
$user->delete();
User::destroy(1);
User::update([
'name' => 'topthink',
'email' => '[email protected] ',
], ['id' => 1]);
// 根据主键删除多个数据
User::destroy([1, 2, 3]);
// 指定条件删除数据
User::destroy([
'status' => 0,
]);
// 使用闭包条件
User::destroy(function ($query) {
$query->where('id', '>', 0)
->where('status', 0);
});