PHP code example of rnr1721 / le7-db-redbean

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

    

rnr1721 / le7-db-redbean example snippets




declare(strict_types=1);

namespace Model;

use Core\Entify\Interfaces\ModelInterface;
use Core\Database\Redbean\Model;

class Contact extends Model implements ModelInterface
{
    
    public function getRules(): array
    {
        return [
            'id' => [
                'label' => 'ID',
                'validate' => ''
            ],
            'name' => [
                'label' => 'Name',
                'validate' => ''
            ],
            'lastname' => [
                'label' => 'Lastname',
                'validate' => ''
            ],
            'another' => [
                'label' => 'Another field',
                'validate' => '

use Core\Database\Redbean\Db;
use Core\Database\Redbean\DbConn;
use Core\Entify\Entification;
use Core\Entify\RulesLoaderClass;
use Core\Database\Redbean\Drivers\DbSql;

// Create rules loader for classes, and set namespace for models
$loader = new RulesLoaderClass('\\Model\\');

// Create instance of Entify framework
$entification = new Entification($loader);

// Now, create array with parametres
// But you can configure DB driver with methods if need
$connectionArray = [
    'namespace' => '\\Model\\', // Importsnt! Namespace for models
    'driver' => 'mysql', // mysql, pgsql, curbid
    'host' => 'localhost', // Db host
    'port' => '3306', // Your Db port
    'user' => 'user',
    'name' => 'database',
    'pass' => '123'
];

// Now we create driver
$driver = new DbSql($connectionArray);

// Create object that connect, disconnect and switch between DBs
$connection = new DBConn($driver, $entification);

// Create database object wrapper
$db = new Db($connection);

// Code above you can run in DI container so it not scarry :)

// Now we can use non-static Redbean methods with $db object
$bean = $db->dispense('contact');

// See in model class above
$bean->name = 'John';
$bean->lastname = 'Doe';
$bean->another = '';

try {
    // Now this make invalidArgumentException because in rules another is
    // 

use Core\Database\Redbean\EntificationSql;
use Core\Database\Redbean\Db;
use Core\Database\Redbean\DbConn;
use Core\Entify\Entification;
use Core\Entify\RulesLoaderClass;
use Core\Database\Redbean\Drivers\DbSql;

$loader = new RulesLoaderClass('\\Model\\');

$entification = new Entification($loader);

$connectionArray = [
    'namespace' => '\\Model\\', // Importsnt! Namespace for models
    'driver' => 'mysql', // mysql, pgsql, curbid
    'host' => 'localhost', // Db host
    'port' => '3306', // Your Db port
    'user' => 'user',
    'name' => 'database',
    'pass' => '123'
];

$driver = new DbSql($connectionArray);

$connection = new DBConn($driver, $entification);

$db = new Db($connection);

$entificationSql = new EntificationSql($loader, $db);

// Now we end DI container part. And now we can use repository entities

// Get repository data provider
// You can also set bindings and custom query
$provider = $entificationSql->getDataProvider('contact');

// Case1 - get paginated result:
// Per page 5 items, and current page 1
$entity = $provider->paginate(5, 1)->getEntity();
$data = $entity->export(); // Get data as array
$info = $entity->getInfo(); // Get pagination data and rules info

// Case2 - get paginated custom result
$entity = $provider->paginate(5, 1)->select()->from()->where('id = 1')->getEntity();
$data = $entity->export(); // Get data as array
$info = $entity->getInfo(); // Get pagination data and rules info

$bindings = [1,2,3];
$provider = $entificationSql->getDataProvider('contact',$bindings);
$entity = $provider->paginate(5, 1)->select()->from()->where(' id = ? OR id = ? OR id = ? ')->getEntity();
$data = $entity->export(); // Get data as array
$info = $entity->getInfo(); // Get pagination data and rules info

// Unpaginated result
$entity = $provider->select()->from()->where('id = 1')->getEntity();
$data = $entity->export(); // Get data as array
$info = $entity->getInfo(); // Empty pagination data and rules info

// We have already created $db, see above

// Switch to another (in this case Sqlite) database
$driver = new DbSqlite(['path'=>'./db.sqlite']);
$db->getConntection()->switchDatabase($driver,'mydb2');
// Get connection status (after use it), bool
$status = $db->getConnection()->isConnected();
// Disconnect from Db
$db->getConnection()->disconnect();