PHP code example of primal / record

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

    

primal / record example snippets


class ExampleRecord extends \Primal\Database\MySQL\Record {
	protected $tablename = 'example_table';
}

$pdo = new PDO("mysql:host=localhost;dbname=test", 'username', 'password');

$row = new ExampleRecord($pdo);
$row['column_1'] = "Foo";

$row = new ExampleRecord($pdo);
$row->column_1 = "Foo";

$row = new ExampleRecord($pdo);
$row['column_1'] = "Foo";
$row['column_2'] = 16;
$row['date_column'] = new DateTime('yesterday');
$row->save();

//$row['id'] now contains the auto-incremented primary key value.

$row = new ExampleRecord($pdo);
$row['id'] = 2;
$row['column_2'] = 16;
$row['date_column'] = 'now'; //Record will automatically parse strings into DateTime values for date column types (`DATE`, `DATETIME`, `TIMESTAMP`).
$row->save();

// only `column_2` and `date_column` on the row keyed with an id of 2 will be updated, all other columns will remain unchanged.

$row = new ExampleRecord($pdo);
if ($row->load(5)) {
    $row->set('column_2', $new_value);
}

$row = new ExampleRecord($pdo);
if ($row->load(2)) {
    //$row found a table row where the `id` column (the primary key) contained 2.
} else {
    //No row was found that matched that primary key value.
}

$user = new User($pdo, array('email'=>'[email protected]'));
if ($user->found) {
    // user code
}

class Member extends \Primal\Database\MySQL\Record {
	protected $tablename = 'members';
	protected $schema = array(
    	'primaries' => array('member_id'),
    	'auto_increment' => 'member_id',
    	'columns' => array(
    		'member_id' => array(
    			'null' => false,
    			'unsigned' => 1,
    			'format' => 'number',
    			'precision' => 0,
    			'type' => 'int',
    		),
    		'email' => array(
    			'null' => false,
    			'unsigned' => 0,
    			'format' => 'string',
    			'precision' => 0,
    			'type' => 'varchar',
    			'length' => '255',
    		),
    		'username' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'string',
    			'precision' => 0,
    			'type' => 'varchar',
    			'length' => '200',
    		),
    		'firstname' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'string',
    			'precision' => 0,
    			'type' => 'tinytext',
    		),
    		'middlename' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'string',
    			'precision' => 0,
    			'type' => 'tinytext',
    		),
    		'lastname' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'string',
    			'precision' => 0,
    			'type' => 'tinytext',
    		),
    		'website' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'string',
    			'precision' => 0,
    			'type' => 'text',
    		),
    		'industry' => array(
    			'null' => false,
    			'unsigned' => 1,
    			'format' => 'number',
    			'precision' => 0,
    			'type' => 'int',
    		),
    		'last_updated' => array(
    			'null' => false,
    			'unsigned' => 0,
    			'format' => 'datetime',
    			'precision' => 0,
    			'type' => 'timestamp',
    		),
    		'last_login' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'datetime',
    			'precision' => 0,
    			'type' => 'datetime',
    		),
    		'validated' => array(
    			'null' => false,
    			'unsigned' => 0,
    			'format' => 'number',
    			'precision' => 0,
    			'type' => 'tinyint',
    		),
    		'review_rating' => array(
    			'null' => true,
    			'unsigned' => 1,
    			'format' => 'number',
    			'precision' => 0,
    			'type' => 'int',
    		),
    		'membership_type' => array(
    			'null' => false,
    			'unsigned' => 0,
    			'format' => 'enum',
    			'precision' => 0,
    			'type' => 'enum',
    			'options' => array (
    				0 => 'Free',
    				1 => 'None',
    				2 => 'Credited',
    				3 => 'Monthly',
    				4 => 'Yearly',
    			),
    		),
    		'membership_expires' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'date',
    			'precision' => 0,
    			'type' => 'date',
    		),
    		'balance' => array(
    			'null' => false,
    			'unsigned' => 0,
    			'format' => 'number',
    			'precision' => '2',
    			'type' => 'decimal',
    		),
    	)
    );
}

$record = new Member($this->mypdo);
var_export($record->buildTableSchema());

//output is the above array structure