PHP code example of alexsasharegan / model-framework
1. Go to this page and download the library: Download alexsasharegan/model-framework 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/ */
alexsasharegan / model-framework example snippets
// in your main entry point file, load the environment
// this technique avoids any global variable pollution
call_user_func( function () {
// constructed with the directory containing the `.env` file
$environment = new \Dotenv\Dotenv( __DIR__ );
$environment->load();
// also a nice place to set things like timezone & session
date_default_timezone_set('America/Phoenix');
session_start();
} );
use Framework\Model;
class Character extends Model {
/*
* This is the name of the database table
*/
const TABLE = 'characters';
/*
* Calling Model::delete() when this is set to true
* will only set the softDeleteFieldName to TRUE and update the db model
*/
protected $useSoftDeletes = TRUE;
protected $softDeleteFieldName = 'deleted';
/*
* Use this to handle time-stamping model creation
* (since MySQL handles updates automatically,
* but not creation automatically)
*/
protected $timestamp = TRUE;
protected $timestampFieldName = 'created_at';
protected $casts = [
'default_value' => self::CAST_TO_INT,
'deleted' => self::CAST_TO_BOOL,
'float' => self::CAST_TO_FLOAT,
'price' => self::CAST_TO_PRICE,
'special' => self::CAST_FROM_JSON_TO_ARRAY,
'specialObject' => self::CAST_FROM_JSON_TO_OBJECT,
];
}
// Convenient static methods return instances
// this returns a model instance with the data from this id
// already parsed, or an empty model
$character = Character::fetch( 65 );
// Models implement ArrayAccess interface,
// but they still enforce the internal cast instructions
// as well as returning NULL when an index isn't defined
// instead of throwing an exception
$character['special'] = [
'key' => [
'nested' => 'value',
],
'prop' => TRUE,
];
echo $character->toJson( JSON_PRETTY_PRINT );
$newCharacter = Character::fetch( 655 );
$newCharacter->mergeData( [
'first_name' => 'Jamie',
'last_name' => 'Lanaster',
'special' => [
'house' => [
'name' => 'Lanaster',
'words' => 'A Lanaster always pays his debts.',
'sigil' => 'Lion',
],
],
'price' => 12.3493046,
'deleted' => FALSE,
'fakeField' => "I'm not in your database...",
] );
// a handy dot notation syntax is available from the model as a __get magic method
echo $newCharacter['special.house.name'];
// Lanaster
// The benefits of the Traversable interface
foreach ( $newCharacter as $key => $value )
{
$type = gettype( $value );
echo "Prop: $key ($type): " . print_r( $value, TRUE ) . PHP_EOL;
}
echo PHP_EOL;
// Retrieves the fields from the database,
// then filters the properties on the object
// so you can further prevent injection
// (prepared queries are also used for data binding)
$newCharacter->removePropsNotInDatabase();
foreach ( $newUser as $key => $value )
{
$type = gettype( $value );
echo "Prop: $key ($type): " . print_r( $value, TRUE ) . PHP_EOL;
}
echo PHP_EOL;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.