PHP code example of basttyy / reactphp-orm

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

    

basttyy / reactphp-orm example snippets



//import
eactphpOrm\QueryBuilderWrapper;
use Basttyy\ReactphpOrm\QueryBuilder;
use React\MySQL\Factory;
use React\MySQL\QueryResult;

#create react/mysql factory
$factory = new Factory();

#create querybuilder connection object
$connection = (new QueryBuilderWrapper($factory))->createLazyConnection('root:123456789@localhost/react-database');

#run an insert query
$values = [
    'username' => 'johndoe',
    'firstname' => 'john',
    'lastname' => 'doe',
    'email' => '[email protected]'
];

$connection->from('users')->insert($values)->then(
    function (bool $status) {
        echo "inserted successfully ".PHP_EOL;
    },
    function (Exception $ex) {
        echo $ex->getMessage().PHP_EOL;
    }
);

#run a select where query
$connection->from('users')->where('status', 'active')->query()->then(
    function (QueryResult $command) {
        print_r($command->resultRows);
        echo count($command->resultRows) . ' row(s) in set' . PHP_EOL;
    },
    function (Exception $error) {
        echo 'Error: ' . $error->getMessage() . PHP_EOL;
    }
);

#run a get query
$connection->from('users')->where('status', 'active')->get()->then(
    function(Collection $data) {
        print_r($data->all());
        echo $data->count() . ' row(s) in set' . PHP_EOL;
    },
    function (Exception $error) {
        echo 'Error: ' . $error->getMessage() . PHP_EOL;
    }
);