1. Go to this page and download the library: Download gajus/moa 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/ */
gajus / moa example snippets
/**
* This class is generated using https://github.com/gajus/moa.
* Do not edit this file; it will be overwritten.
*/
abstract class Person extends \Gajus\MOA\Mother {
const TABLE_NAME = 'person';
const PRIMARY_KEY_NAME = 'id';
static protected
$columns = [
'id' => [
'column_type' => 'int(10) unsigned',
'column_key' => 'PRI',
'column_default' => NULL,
'data_type' => 'int',
'is_nullable' => false,
'extra' => 'auto_increment',
'character_maximum_length' => NULL,
],
'name' => [
'column_type' => 'varchar(100)',
'column_key' => '',
'column_default' => '',
'data_type' => 'varchar',
'is_nullable' => false,
'extra' => '',
'character_maximum_length' => 100,
],
'language' => [
'column_type' => 'varchar(100)',
'column_key' => '',
'column_default' => 'English',
'data_type' => 'varchar',
'is_nullable' => false,
'extra' => '',
'character_maximum_length' => 100,
]
];
}
/**
* @param PDO $db
* @param int $id
*/
$person = new \My\App\Model\Person($db);
// Set property
$person['name'] = 'Foo';
// Insert object to the database
$person->save();
# $person['id'] 1
// Note that "language" property was not set,
// though it had default value in the table schema.
# $person['language'] English
// Update property
$person['name'] = 'Bar';
// Save object state to the database
$person->save();
# $person['id'] 1
$person->delete();
# $person['id'] null
# $person['name'] Bar
$person->save();
# $person['id'] 2
$person = new \My\App\Model\Person($db, 2);
$person = new \My\App\Model\Person($db);
$person['name'] = 'Baz';
$person->save();
/**
* Shorthand method to pass each array key, value pair to the setter.
*
* @param array $data
* @return Gajus\MOA\Mother
*/
$person->populate(['name' => 'Qux', 'language' => 'Lithuanian']);
namespace My\App\Model;
class Person extends \Dynamically\Generated\Person {
static public function get[Where][..] (\PDO $db) {
$person_id = $db
->query("SELECT `" . static::$properties['primary_key_name'] . "` FROM `" . static::$properties['table_name'] . "` ORDER BY `[..]` DESC LIMIT 1")
->fetch(\PDO::FETCH_COLUMN);
if (!$person_id) {
throw new \Gajus\MOA\Exception\RecordNotFoundException('[..]');
}
return new static::__construct($db, $person_id);
}
static public function getMany[Where][..] (\PDO $db) {
$sth = $db->prepare("SELECT * FROM `" . static::$properties['table_name'] . "` WHERE `[..]` = ?");
$sth->execute(/* [..] */);
return $sth->fetchAll(\PDO::FETCH_ASSOC);
}
}
/**
* Triggered after INSERT query but before the transaction is committed.
*
* @return void
*/
protected function afterInsert () {}
/**
* Triggered after UPDATE query but before the transaction is committed.
*
* @return void
*/
protected function afterUpdate () {}
/**
* Triggered after DELETE query but before the transaction is committed.
*
* @return void
*/
protected function afterDelete () {}
/**
* Triggered when an attempt is made to change object property.
* Returning an error message will discard the transaction and throw Gajus\MOA\Exception\ValidationException exception.
*
* @param string $name
* @param mixed $value
* @return null|string
*/
protected function validateSet ($name, $value) {}
/**
* Triggered when an attempt is made to save object state.
* Returning an error message will discard the transaction and throw Gajus\MOA\Exception\ValidationException exception.
*
* @return null|mixed
*/
protected function validateSave () {}