PHP code example of ofmadsen / livid

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

    

ofmadsen / livid example snippets




$config = new Livid\Config\SQLite3Config();
$database = new Livid\Database($config);

Livid\Mapper::setDatabase($database);



class ExampleMapper extends Livid\Mapper
{
    public function get($id)
    {
        $sql = 'SELECT *
                FROM examples
                WHERE id = :id';

        $parameters = ['id' => $id];

        return $this->query($sql, $parameters)->get()
    }
}

// Assuming the examples table contain a row with id = 1
$mapper = new ExampleMapper();
$entity = $mapper->get(1);

// Will echo Livid\Entity as no entity object was specified in the Query::get method
echo get_class($entity);



class Example
{
    private $id;
    private $exampleText;

    public function getExampleText()
    {
        return $this->exampleText;
    }
}



class ExampleMapper extends Livid\Mapper
{
    public function get($id)
    {
        // Note that the database column name is in snake case form
        $sql = 'SELECT id, example_text
                FROM examples
                WHERE id = :id';

        $parameters = ['id' => $id];

        // Note that the get method on the query object is called with a reference to the Example class
        return $this->query($sql, $parameters)->get(Example::class)
    }
}

$mapper = new ExampleMapper();
$example = $mapper->get(1);

echo $example->getExampleText();



class ExampleMapper extends Livid\Mapper
{
    // Uses the default database - in this case the MySQL at 127.0.0.1 with database 'livid' - see below
}

class ExampleLoggerMapper extends Livid\Mapper
{
    const DATABASE = 'logger';
}



// Default setup
$config = new Livid\Config\MySQLConfig();
$config->setHost('127.0.0.1');
$config->setDatabase('livid');
$defaultDatabase = new Livid\Database($config);

Livid\Mapper::setDatabase($defaultDatabase);

// Logger setup
$config = new Livid\Config\SQLite3Config();
$loggerDatabase = new Livid\Database($config);

Livid\Mapper::setDatabase($loggerDatabase, ExampleLoggerMapper::DATABASE);



/**
 * Query the database.
 *
 * @param string $sql
 * @param mixed[] $parameters
 *
 * @return Livid\Query
 */
public function query($sql, array $parameters = []);

/**
 * Execute a sql query on the database.
 *
 * @param string $sql
 *
 * @return int
 */
public function execute($sql);



/**
 * Get a single entity.
 *
 * If the result contains more than one entity an exception is thrown.
 *
 * @param string $entity
 *
 * @throws InvalidFetchedEntityCount
 *
 * @return mixed
 */
public function get($entity = Entity::class);

/**
 * Get multiple entities.
 *
 * @param string $entity
 *
 * @return mixed[]
 */
public function all($entity = Entity::class);

/**
 * Get a scalar value.
 *
 * @return mixed
 */
public function scalar();

/**
 * Bind parameters and execute the statement.
 *
 * @throws DatabaseQueryFailed
 *
 * @return bool
 */
public function execute();