1. Go to this page and download the library: Download planetbiru/magic-object 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/ */
planetbiru / magic-object example snippets
use MagicObject\MagicObject;
$object = new MagicObject();
namespace MusicProductionManager\Config;
use MagicObject\MagicObject;
class ConfigApp extends MagicObject
{
/**
* Constructor
*
* @param mixed $data Initial data
* @param bool $readonly Readonly flag
*/
public function __construct($data = null, $readonly = false)
{
if($data != null)
{
parent::__construct($data);
}
$this->readOnly($readonly);
}
}
$cfg = new ConfigApp(null, true);
$cfg->loadYamlFile(dirname(__DIR__)."/.cfg/app.yml", true, true, true);
// to get database object,
$cfg->getDatabase()
//
// to get database.host
$cfg->getDatabase()->getHost()
// to get database.database_name
$cfg->getDatabase()->getDatabaseName()
use MagicObject\Database\PicoDatabase;
use MagicObject\Database\PicoDatabaseCredentials;
use MusicProductionManager\Config\ConfigApp;
use MusicProductionManager\Data\Entity\Album;
ase = new PicoDatabase($databaseCredentials);
try {
$database->connect();
// Create a new Album instance
$album1 = new Album(null, $database);
$album1->setAlbumId("123456");
$album1->setName("Album 1");
$album1->setAdminCreate("USER1");
$album1->setDuration(300);
// Another way to create an object
// Create an object from stdClass or another object with matching properties (snake_case or camelCase)
$data = new stdClass;
// Snake case
$data->album_id = "123456";
$data->name = "Album 1";
$data->admin_create = "USER1";
$data->duration = 300;
// Or camel case
$data->albumId = "123456";
$data->name = "Album 1";
$data->adminCreate = "USER1";
$data->duration = 300;
$album1 = new Album($data, $database);
// Another way to create an object
// Create an object from an associative array with matching properties (snake_case or camelCase)
$data = array();
// Snake case
$data["album_id"] = "123456";
$data["name"] = "Album 1";
$data["admin_create"] = "USER1";
$data["duration"] = 300;
// Or camel case
$data["albumId"] = "123456";
$data["name"] = "Album 1";
$data["adminCreate"] = "USER1";
$data["duration"] = 300;
$album1 = new Album($data, $database);
// Get value from the form
// This method is not safe
$album1 = new Album($_POST, $database);
// We can use another method
$inputPost = new InputPost();
// We can apply filters
$inputPost->filterName(PicoFilterConstant::FILTER_SANITIZE_SPECIAL_CHARS);
$inputPost->filterDescription(PicoFilterConstant::FILTER_SANITIZE_SPECIAL_CHARS);
// If a property is not present in $inputPost, we can set a default value
// Note that the user can modify the form and add/update unwanted properties
$inputPost->checkboxActive(false);
$inputPost->checkboxAsDraft(true);
// We can remove any property data from the $inputPost object before applying it to the entity
// This property will not be saved to the database
$inputPost->setSortOrder(null);
$album1 = new Album($inputPost, $database);
// Insert into the database
$album1->insert();
// Insert or update the record
$album1->save();
// Update the record
// A NoRecordFoundException will be thrown if the ID is not found
$album1->update();
// Convert the object to JSON
$json = $album1->toString();
// Alternatively:
$json = $album1 . "";
// Send to the output buffer
// It is automatically converted to a string
echo $album1;
// Find one record by ID
$album2 = new Album(null, $database);
$album2->findOneByAlbumId("123456");
// Find multiple records
$album2 = new Album(null, $database);
$albums = $album2->findByAdminCreate("USER1");
$rows = $albums->getResult();
foreach ($rows as $albumSaved) {
// $albumSaved is an instance of Album
// We can update the data
$albumSaved->setAdminEdit("USER1");
$albumSaved->setTimeEdit(date('Y-m-d H:i:s'));
// This value will not be saved to the database because it does not have a corresponding column
$albumSaved->setAnyValue("ANY VALUE");
$albumSaved->update();
}
} catch (Exception $e) {
// Handle the exception (currently doing nothing)
}
bash
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.