Skip to content

Latest commit

 

History

History
146 lines (115 loc) · 2.53 KB

File metadata and controls

146 lines (115 loc) · 2.53 KB

JSON Helper Functions

Create and manipulate JSON data using PDOdb's JSON helpers.

Creating JSON

jsonObject() - Create Object

use tommyknocker\pdodb\helpers\Db;

$db->find()->table('users')->insert([
    'name' => 'Alice',
    'meta' => Db::jsonObject([
        'city' => 'NYC',
        'age' => 30,
        'verified' => true
    ])
]);

jsonArray() - Create Array

$db->find()->table('users')->insert([
    'name' => 'Alice',
    'tags' => Db::jsonArray('php', 'mysql', 'docker')
]);

JSON Queries

jsonGet() - Extract Value

$users = $db->find()
    ->from('users')
    ->select([
        'id',
        'name',
        'city' => Db::jsonGet('meta', ['city'])
    ])
    ->get();

jsonPath() - Path Comparison

$users = $db->find()
    ->from('users')
    ->where(Db::jsonPath('meta', ['age'], '>', 25))
    ->get();

jsonContains() - Check Contents

// Check if array contains value
$phpDevs = $db->find()
    ->from('users')
    ->where(Db::jsonContains('tags', 'php'))
    ->get();

jsonExists() - Check Path

$users = $db->find()
    ->from('users')
    ->where(Db::jsonExists('meta', ['city']))
    ->get();

JSON Functions

jsonLength() - Array/Object Length

$users = $db->find()
    ->from('users')
    ->select([
        'id',
        'tag_count' => Db::jsonLength('tags')
    ])
    ->get();

jsonKeys() - Object Keys

$users = $db->find()
    ->from('users')
    ->select([
        'id',
        'meta_keys' => Db::jsonKeys('meta')
    ])
    ->get();

jsonType() - Value Type

$users = $db->find()
    ->from('users')
    ->select([
        'id',
        'tags_type' => Db::jsonType('tags')
    ])
    ->get();

Common Patterns

User Profile

$db->find()->table('users')->insert([
    'username' => 'alice',
    'profile' => Db::jsonObject([
        'bio' => 'Software Developer',
        'location' => 'NYC',
        'skills' => Db::jsonArray('PHP', 'MySQL', 'Docker')
    ])
]);

Product Specs

$db->find()->table('products')->insert([
    'name' => 'Laptop',
    'specs' => Db::jsonObject([
        'cpu' => 'Intel i7',
        'ram' => '16GB',
        'storage' => '512GB SSD',
        'ports' => Db::jsonArray('USB-A', 'USB-C', 'HDMI')
    ])
]);

Next Steps