PHP code example of windwalker / record

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

    

windwalker / record example snippets


<?
class SakuraRecord extends Record
{
    protected $casts = [
        'id' => 'int',
        'price' => 'string',
        'created' => 'datetime',
        'modified' => 'timestamp',
        'images' => 'object', // or array will be json decoded
        'params' => \Windwalker\Structure\Structure::class,
        'other' => ['SomeClass', 'handle'] // Use callback
    ];
}

$sakuraRecord->load(3);

$sakuraRecord->id; // 3
$sakuraRecord->price; // '1200.00'
$sakuraRecord->created->format('Y/d/m'); // Auto convert to DateTime object
$sakuraRecord->modified; // 1497067876
$sakuraRecord->images[0]->url; // Store json in DB, can will auto decode to object.
$sakuraRecord->params->get('foo.bar'); // Use class name to store value to object
 php
use Windwalker\Record\Record;

// Record object for users table
$user = new Record('users');
 php
$user->load(25); // Load by primary key

$user->load(array('alias' => $alias)); // Load by field name.
 php
$data = array(
    'name'     => 'Sakura',
    'username' => 'sakura',
    'alias'    => 'sakura',
    'password' => '1234',
    'desc'     => 'foo bar.'
);

$user->bind($data);

$user->name; // Sakura
 php
$user->alias; // null
 php
$data = array(
    'name'     => 'Sakura',
    'username' => 'sakura',
    'password' => '1234'
);

$user->bind($data);

$user->store();

echo $user->id; // Auto generated id
 php
$data = array(
    'id'       => 30,
    'name'     => 'Sakura',
    'username' => 'sakura',
    'password' => '1234'
);

$user->bind($data);

$user->store();
 php
$user->bind($data)
    ->validate()
    ->store();
 php
$user->load(30);
$result = $user->delete(); // boolean

// OR delete by conditions

$result = $user->delete(30); // boolean
$result = $user->delete(array('username' => $username)); // boolean
 php
class ArticleRecord extends Record
{
    protected function setCreatedDateValue($value)
    {
        if ($value instanceof \DateTime)
        {
            $value = $value->format('Y-m-d H:i:s');
        }

        $this->data['created_date'] = $value;
    }
}
 php
$articleRecord->created_date = new \DateTime('now');

echo $articleRecord->created_date; // 2016-03-02 12:30:29
 php
class ArticleRecord extends Record
{
    protected function getCreatedDateValue($value)
    {
        return new \DateTime($value);
    }
}
 php
echo $articleRecord->created_date->format('Y-m-d H:i:s'); // 2016-03-02 12:30:29
 php
$cat = new NestedRecord('categories');

$cat->createRoot();
 php
$cat->bind($data)
    ->setLocation(1, NestedRecord::LOCATION_FIRST_CHILD)
    ->store();
 php
$cat->bind($data)
    ->setLocation(2, NestedRecord::LOCATION_LAST_CHILD)
    ->store();
 php
$cat->move(1); // move up
$cat->move(-1); // Move down
 php
$cat->moveByReference(3, NestedRecord::LOCATION_LAST_CHILD);
 php
$path = $cat->getPath();
 php
$records = $cat->getTree();
 php
class UserRecord extends Record
{
	public function onAfterLoad(Event $event)
	{
		$this->foo = array('a', 'b', 'c');
	}
}
 php
// Use listener object
$record->getDispatcher()->addListener(new MyRecordListener);

// Use callback
$record->getDispatcher()->listen('onAfterStore', function (Event $event)
{
    // Process your logic
});