PHP code example of planetbiru / magic-object
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()
namespace MusicProductionManager\Data\Entity;
use MagicObject\MagicObject;
/**
* @Entity
* @JSON(property-naming-strategy=SNAKE_CASE, prettify=true)
* @Table(name="album")
* @Cache(enable="true")
* @package MusicProductionManager\Data\Entity
*/
class Album extends MagicObject
{
/**
* Album ID
*
* @Id
* @GeneratedValue(strategy=GenerationType.UUID)
* @NotNull
* @Column(name="album_id", type="varchar(50)", length=50, nullable=false)
* @Label(content="Album ID")
* @var string
*/
protected $albumId;
/**
* Name
*
* @Column(name="name", type="varchar(50)", length=50, nullable=true)
* @Label(content="Name")
* @var string
*/
protected $name;
/**
* Title
*
* @Column(name="title", type="text", nullable=true)
* @Label(content="Title")
* @var string
*/
protected $title;
/**
* Description
*
* @Column(name="description", type="longtext", nullable=true)
* @Label(content="Description")
* @var string
*/
protected $description;
/**
* Producer ID
*
* @Column(name="producer_id", type="varchar(40)", length=40, nullable=true)
* @Label(content="Producer ID")
* @var string
*/
protected $producerId;
/**
* Release Date
*
* @Column(name="release_date", type="date", nullable=true)
* @Label(content="Release Date")
* @var string
*/
protected $releaseDate;
/**
* Number Of Song
*
* @Column(name="number_of_song", type="int(11)", length=11, nullable=true)
* @Label(content="Number Of Song")
* @var int
*/
protected $numberOfSong;
/**
* Duration
*
* @Column(name="duration", type="float", nullable=true)
* @Label(content="Duration")
* @var float
*/
protected $duration;
/**
* Image Path
*
* @Column(name="image_path", type="text", nullable=true)
* @Label(content="Image Path")
* @var string
*/
protected $imagePath;
/**
* Sort Order
*
* @Column(name="sort_order", type="int(11)", length=11, nullable=true)
* @Label(content="Sort Order")
* @var int
*/
protected $sortOrder;
/**
* Time Create
*
* @Column(name="time_create", type="timestamp", length=19, nullable=true, updatable=false)
* @Label(content="Time Create")
* @var string
*/
protected $timeCreate;
/**
* Time Edit
*
* @Column(name="time_edit", type="timestamp", length=19, nullable=true)
* @Label(content="Time Edit")
* @var string
*/
protected $timeEdit;
/**
* Admin Create
*
* @Column(name="admin_create", type="varchar(40)", length=40, nullable=true, updatable=false)
* @Label(content="Admin Create")
* @var string
*/
protected $adminCreate;
/**
* Admin Edit
*
* @Column(name="admin_edit", type="varchar(40)", length=40, nullable=true)
* @Label(content="Admin Edit")
* @var string
*/
protected $adminEdit;
/**
* IP Create
*
* @Column(name="ip_create", type="varchar(50)", length=50, nullable=true, updatable=false)
* @Label(content="IP Create")
* @var string
*/
protected $ipCreate;
/**
* IP Edit
*
* @Column(name="ip_edit", type="varchar(50)", length=50, nullable=true)
* @Label(content="IP Edit")
* @var string
*/
protected $ipEdit;
/**
* Active
*
* @Column(name="active", type="tinyint(1)", length=1, defaultValue="1", nullable=true)
* @DefaultColumn(value="1")
* @var bool
*/
protected $active;
/**
* As Draft
*
* @Column(name="as_draft", type="tinyint(1)", length=1, defaultValue="1", nullable=true)
* @DefaultColumn(value="1")
* @var bool
*/
protected $asDraft;
}
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