PHP code example of topsyx6 / syx6-orm

1. Go to this page and download the library: Download topsyx6/syx6-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/ */

    

topsyx6 / syx6-orm example snippets


    Db::table('user')
        ->insert([
            'name'  => 'syx6php',
            'email' => '[email protected]',
        ]);
    

   $user        = new User;
   $user->name  = 'syx6php';
   $user->email = '[email protected]';
   $user->save();
    

    $user = new User;
    $user->save([
        'name'  => 'syx6php',
        '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::find(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'  => 'topsyx6',
            'email' => '[email protected]',
        ]);
    

    $user        = User::find(1);
    $user->name  = 'topsyx6';
    $user->email = '[email protected]';
    $user->save();
    

    $user = User::find(1);
    $user->save([
        'name'  => 'topsyx6',
        'email' => '[email protected]',
    ]);
    

    User::update([
        'name'  => 'topsyx6',
        'email' => '[email protected]',
    ], ['id' => 1]);
    

    Db::table('user')->delete(1);
    

    $user = User::find(1);
    $user->delete();
    

   User::destroy(1);
    

    // 根据主键删除多个数据
    User::destroy([1, 2, 3]);
    // 指定条件删除数据
    User::destroy([
        'status' => 0,
    ]);
    // 使用闭包条件
    User::destroy(function ($query) {
        $query->where('id', '>', 0)
            ->where('status', 0);
    });