PHP code example of ceus-media / database

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

    

ceus-media / database example snippets




$dbDriver	= 'mysql';
$dbName		= 'myDatabase';
$dbUsername	= 'myDatabaseUser';
$dbPassword	= 'myDatabasePassword';

$dbc	= new \CeusMedia\Database\PDO\Connection(
	new \CeusMedia\Database\PDO\DataSourceName( $dbDriver, $dbName ),
	$dbUsername, $dbPassword
);

class MyFirstTable extends \CeusMedia\Database\PDO\Table
{
	protected string $name			= "my_first_table";
	protected array $columns		= [
		'id',
		'maybeSomeForeignId',
		'content',
	];
	protected string $primaryKey	= 'id';
	protected array $indices		= [
		'maybeSomeForeignId',
	];
	protected int $fetchMode		= \PDO::FETCH_OBJ;
}

$table	= new MyFirstTable( $dbc );

$entry	= $table->get( 1 );

object stdObject(
	'id'					=> 1,
	'maybeSomeForeignId'	=> 123,
	'content'				=> 'Content of first entry.'
)

$someEntries	= $table->getAllByIndex( 'maybeSomeForeignId', 123 );

$indices		= [
	'maybeSomeForeignId'	=> 123,
	'notExistingKey'		=> 'will result in an exception',
];
$someEntries	= $table->getAllByIndices( $indices );

$allEntries	= $table->getAll();

$conditions	= ['content' => '%test%'];
$orders		= [];
$limits		= [$offset = 0, $limit = 10];

$allEntries	= $table->getAll( $conditions, $orders, $limits );

$orders	= [
	'maybeSomeForeignId'	=> 'DESC',
	'content'		=> 'ASC',
];

$number	= $table->countByIndex( 'maybeSomeForeignId', 123 );

$number	= $table->countByIndices( [
	'maybeSomeForeignId'	=> 123,
	'notExistingKey'		=> 'will result in an exception',
] );

$number	= $table->count();

$Conditions	= [
	'maybeSomeForeignId'	=> 123,
	'content'		=> '%test%',
];
$number	= $table->count( $conditions );

$data		= [
	'maybeSomeForeignId'	=> 123,
	'content'				=> 'Second entry.',
];
$entryId	= $table->add( $data );

$primaryKey	= 2;
$data		= [
	'maybeSomeForeignId'	=> 124,
	'content'				=> 'Second entry - changed.',
];
$result	= $table->edit( $primaryKey, $data );

$indices	= [
	'maybeSomeForeignId'	=> 123,
];
$data		= [
	'maybeSomeForeignId'	=> 124,
];
$result	= $table->editByIndices( $indices, $data );

$primaryKey	= 2;
$result	= $table->remove( $primaryKey );

$indices	= [
	'maybeSomeForeignId'	=> 123,
];
$result	= $table->removeByIndices( $indices );

	protected int $fetchMode		= \PDO::[YOUR_FETCH_MODE];

driver		= 'mysql';
host		= 'myHost';
port		= 'myPort';
database	= 'myDatabase';
username	= 'myDatabaseUser';
password	= 'myDatabasePassword';



use CeusMedia\Database\PDO\DataSourceName;
use CeusMedia\Database\OSQL\Client;
use CeusMedia\Database\OSQL\Connection;
use CeusMedia\Database\OSQL\Condition;
use CeusMedia\Database\OSQL\Table;
use CeusMedia\Database\OSQL\Query\Select;

$config	= (object) parse_ini_file( 'myConfigFile.ini' );

$client	= new Client( new Connection( DataSourceName::renderStatic(
	$config->driver,
	$config->database,
	$config->host,
	$config->port,
	$config->username,
	$config->password
), $config->username, $config->password ) );

$result	= Select::create( $client )
	->from( new Table( 'galleries', 'g' ) )
	->where( new Condition( 'galleryId', 1, Condition::OP_EQ ) )
	->execute();

new UI_DevOutput();
print_m( $result );

[O] 0 -> stdClass
   [S] galleryId => 1
   [S] status => 0
   [S] rank => 1
   [S] path => test
   [S] title => Test
   [S] description => Das ist ein Test.
   [S] timestamp => 1402008611