PHP code example of reallywang / dynamo-db

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

    

reallywang / dynamo-db example snippets




use ReallyWang\DynamoDB\DB;

// 整理配置
$config = [
    'default' => [
        'region' => '',
        'version' => '',
        'credentials' => [
            'key' => '',
            'secret' => ''
        ]
    ],
    'test' => [
        'region' => '',
        'version' => '',
        'credentials' => [
            'key' => '',
            'secret' => ''
        ]
    ]
];

// 获取连接对象
DB::config($config); // 默认为default
// DB::config($config, 'test'); // 使用test数据库配置

// 向wrtest表中新增数据
$result = DB::table('wrtest')->insert(['id' => 1, 'detail' => '23123']);

// 修改wrtest表中 id = 123 且detail 大于 2的行 detail 为 23123
$result = DB::table('wrtest')->key(['id' => '123'])->condition(['detail' => ['>', 2]])->update(['detail' => '23123']);

// 查询wrtest表中id = 123的数据(id 必须是主键,find方法必须与key方法同时使用)
$result = DB::table('wrtest')->key(['id' => '123'])->find();

// 查询wrtest表中id = 1 且 detail 大于 2 的数据中的detail属性,condition 中必须包括主键
$result = DB::table('wrtest')->condition(['id' => 1, 'detail' => ['>', 2]])->field(['detail'])->get();

// 查询wrtest表中detail 小于 0 或 detail 在 2 和 3 之间的数据
$result = DB::table('wrtest')->condition(['detail' => ['<', 0]])->orCondition(['detail' => ['between', 2, 3]])->scan();

// 删除wrtest表中 id = 123 且detail 大于 2的行
$result = DB::table('wrtest')->key(['id' => '123'])->condition(['detail' => ['>', 2]])->delete();

// 获取wrtest表中 id = 123 且detail 大于 2的数据条数
$result = DB::table('wrtest')->key(['id' => '123'])->condition(['detail' => ['>', 2]])->count();

// 修改wrtest表中 id = 123 且detail 大于 2的行 detail 为 23123
$result = DB::table('wrtest')->key(['id' => '123'])->condition(['detail' => ['>', 2]])->remove(['detail.author[0]']);

// 将wrtest表中 id = 123 且detail.author 中有3个元素 的行 detail.author中的第一个元素删掉
$result = DB::table('wrtest')->key(['id' => '123'])->condition(['detail.author' => ['size', 3]])->remove(['detail.author[0]']);

// 将wrtest表中 id = 123 且detail.author 中元素数量大于等于3 的行 detail.author中的第一个元素删掉
$result = DB::table('wrtest')->key(['id' => '123'])->condition(['detail.author' => ['size', '>=', 3]])->remove(['detail.author[0]']);

// 将wrtest表中 id = 123 且detail.author 中元素个数在3和10之间 的行 detail.author中的第一个元素删掉
$result = DB::table('wrtest')->key(['id' => '123'])->condition(['detail.author' => ['size', 'between', 3, 10]])->remove(['detail.author[0]']);

// wrtest表 符合 id 为123 的行 num属性会递增 1
$result = DB::table('wrtest')->key(['id' => '123'])->condition(['detail' => '123'])->step('num');

// wrtest表 符合 id 为123 的行 num属性会递减 2
$result = DB::table('wrtest')->key(['id' => '123'])->condition(['detail' => '123'])->step('num', false, 2);