2017 © Pedro Peláez
 

windwalker-package record

Windwalker Record package

image

windwalker/record

Windwalker Record package

  • Sunday, July 8, 2018
  • by asika32764
  • Repository
  • 3 Watchers
  • 1 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 52 Versions
  • 0 % Grown

The README.md

Windwalker Record

Windwalker Record is a simple ActiveRecord to operate database row., (*1)

Installation via Composer

Add this to the require block in your composer.json., (*2)

``` json { "require": { "windwalker/record": "~3.0" } }, (*3)


## Use Record New a instance. ``` php use Windwalker\Record\Record; // Record object for users table $user = new Record('users');

Or create a class:, (*4)

``` php use Windwalker\Record\Record;, (*5)

class UserRecord extends Record { protected $table = 'users';, (*6)

protected $keys = 'id';

}, (*7)

$user = new UserRecord;, (*8)


### Load A Record ``` php $user->load(25); // Load by primary key $user->load(array('alias' => $alias)); // Load by field name.

Check row exists, (*9)

``` php try { $record->load(25); } catch (NoResultException $e) { // Handle error }, (*10)


### Bind Data ``` php $data = array( 'name' => 'Sakura', 'username' => 'sakura', 'alias' => 'sakura', 'password' => '1234', 'desc' => 'foo bar.' ); $user->bind($data); $user->name; // Sakura

If we have a table with only 3 columns:, (*11)

Name
name
username
password

The fields which not in this table will be remove after binding data., (*12)

``` php $user->alias; // null, (*13)


That makes our fields in Record will always same as DB table. ### Store #### Create A New Row If primary not exists, Record will create a new row in table. ``` php $data = array( 'name' => 'Sakura', 'username' => 'sakura', 'password' => '1234' ); $user->bind($data); $user->store(); echo $user->id; // Auto generated id

Update A Existing Row

If primary key exists, Record will update it., (*14)

``` php $data = array( 'id' => 30, 'name' => 'Sakura', 'username' => 'sakura', 'password' => '1234' );, (*15)

$user->bind($data);, (*16)

$user->store();, (*17)


### Validate Check method help you validate data. ``` php class UserRecord extends Record { // ... public function validate() { if (!$this['name']) { throw new InvalidArgumentException('Name empty.'); } return true; } }

Then we call validate() before store()., (*18)

``` php $user->bind($data) ->validate() ->store();, (*19)


### Delete ``` php $user->load(30); $result = $user->delete(); // boolean // OR delete by conditions $result = $user->delete(30); // boolean $result = $user->delete(array('username' => $username)); // boolean

Mutator and Accessor

Mutator and accessor is a setter and getter to do some extra modification when you access value via magic methods., (*20)

This is an example of mutator:, (*21)

``` php class ArticleRecord extends Record { protected function setCreatedDateValue($value) { if ($value instanceof \DateTime) { $value = $value->format('Y-m-d H:i:s'); }, (*22)

    $this->data['created_date'] = $value;
}

}, (*23)


Use camel-case style to define a method, then when you access the `created_date` field, this method will be auto executed. ``` php $articleRecord->created_date = new \DateTime('now'); echo $articleRecord->created_date; // 2016-03-02 12:30:29

And an example of accessor:, (*24)

``` php class ArticleRecord extends Record { protected function getCreatedDateValue($value) { return new \DateTime($value); } }, (*25)


And now you can get `DateTime` object back: ``` php echo $articleRecord->created_date->format('Y-m-d H:i:s'); // 2016-03-02 12:30:29

Casts

Add casts to auto convert value type after read from DB:, (*26)

<?
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

Supports casts:, (*27)

  • int | integer
  • real | float | double
  • string
  • bool | boolean
  • object
  • array | json
  • date | datetime
  • timestamp
  • (Class name)
  • (Callback array)

NestedRecord

NestedRecord is a tool help us handle Nested Set Model., (*28)

Create Table

Name: categories, (*29)

Name Type Description Need For NestedRecord
id int Primary Key
parent_id int ID of Parent Node V
title varchar Category title
path varchar Node path V
lft int Left key V
rgt int Right key V
level int Node level V

Initialise

Every nested set should have a root node., (*30)

``` php $cat = new NestedRecord('categories');, (*31)

$cat->createRoot();, (*32)


NOTE: The root node id is `1`. ### Create Node Set as first child of ROOT ``` php $cat->bind($data) ->setLocation(1, NestedRecord::LOCATION_FIRST_CHILD) ->store();

Now we will have a new node and it id is 2. Create a new node as last child of 2., (*33)

``` php $cat->bind($data) ->setLocation(2, NestedRecord::LOCATION_LAST_CHILD) ->store();, (*34)


Available positions: - LOCATION_FIRST_CHILD - LOCATION_LAST_CHILD - LOCATION_BEFORE - LOCATION_AFTER ### Move Node #### Re Ordering ``` php $cat->move(1); // move up $cat->move(-1); // Move down

Move To Other Node

Move to node 3 as last child., (*35)

``` php $cat->moveByReference(3, NestedRecord::LOCATION_LAST_CHILD);, (*36)


### Rebuild If a tree was not correct, using rebuild to reset all `lft`, `rgt` of this branch. ``` php $cat->load(5); $cat->rebuild(); // Rebuild node: 5 and it's children.

getPath

Method to get an array of nodes from a given node to its root., (*37)

``` php $path = $cat->getPath();, (*38)


### getTree Method to get a node and all its child nodes. ``` php $records = $cat->getTree();

Event

Record has an event system that we can process logic before & after every DB operation., (*39)

Add event methods in your Record class., (*40)

``` php class UserRecord extends Record { public function onAfterLoad(Event $event) { $this->foo = array('a', 'b', 'c'); } }, (*41)


Or add listeners to Dispatcher (You must install `windwalker/event` first). ``` php // Use listener object $record->getDispatcher()->addListener(new MyRecordListener); // Use callback $record->getDispatcher()->listen('onAfterStore', function (Event $event) { // Process your logic });

Available events:, (*42)

  • onBeforeLoad
  • onAfterLoad
  • onBeforeStore
  • onAfterStore
  • onBeforeDelete
  • onAfterDelete
  • onBeforeBind
  • onAfterBind
  • onBeforeCreate
  • onAfterCreate
  • onBeforeUpdate
  • onAfterUpdate

The Versions

08/07 2018

dev-master

9999999-dev https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+ LGPL-2.0-or-later

The Requires

 

The Development Requires

framework record windwalker

08/07 2018

3.4.3

3.4.3.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework record windwalker

08/07 2018

3.4.4

3.4.4.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework record windwalker

16/06 2018

3.4.2

3.4.2.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework record windwalker

15/06 2018

3.4.1

3.4.1.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework record windwalker

01/03 2018

dev-test

dev-test https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+ LGPL-2.0-or-later

The Requires

 

The Development Requires

framework record windwalker

01/03 2018

3.3.1

3.3.1.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework record windwalker

01/03 2018

3.3.2

3.3.2.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework record windwalker

01/03 2018

3.4

3.4.0.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework record windwalker

20/02 2018

3.3

3.3.0.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0-or-later

The Requires

 

The Development Requires

framework record windwalker

30/11 2017

3.2.8

3.2.8.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

04/09 2017

3.2.6

3.2.6.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

04/09 2017

3.2.7

3.2.7.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

03/08 2017

3.2.5

3.2.5.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

25/06 2017

3.2.4

3.2.4.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

18/06 2017

3.2.3

3.2.3.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

10/06 2017

3.2.2

3.2.2.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

05/06 2017

3.2.1

3.2.1.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

23/03 2017

3.2

3.2.0.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

20/03 2017

3.1.4

3.1.4.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

20/03 2017

3.1.5

3.1.5.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

20/03 2017

3.1.6

3.1.6.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

19/10 2016

3.1.2

3.1.2.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

19/10 2016

3.1.3

3.1.3.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

11/10 2016

3.1.1

3.1.1.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

11/10 2016

3.1

3.1.0.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

10/10 2016

3.0.1

3.0.1.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

18/07 2016

3.0

3.0.0.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

06/07 2016

3.0-beta2

3.0.0.0-beta2 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

04/07 2016

3.0-beta

3.0.0.0-beta https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

01/04 2016

2.1.9

2.1.9.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

21/03 2016

2.1.8

2.1.8.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

12/02 2016

2.1.7

2.1.7.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

26/10 2015

2.1.4

2.1.4.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

26/10 2015

2.1.5

2.1.5.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

26/10 2015

2.1.6

2.1.6.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

07/09 2015

2.1.2

2.1.2.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

11/08 2015

2.1.1

2.1.1.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

11/08 2015

2.1

2.1.0.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

27/07 2015

2.0.9

2.0.9.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

13/03 2015

2.0.7

2.0.7.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

13/03 2015

2.0.8

2.0.8.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

24/02 2015

2.0.6

2.0.6.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

17/02 2015

2.0.5

2.0.5.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

03/01 2015

2.0.4

2.0.4.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

16/12 2014

2.0.0

2.0.0.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

16/12 2014

2.0.1

2.0.1.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

16/12 2014

2.0.2

2.0.2.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

16/12 2014

2.0.3

2.0.3.0 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

24/11 2014

2.0.0-beta2

2.0.0.0-beta2 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

LGPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

05/10 2014

2.0.0-beta1

2.0.0.0-beta1 https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework record windwalker

05/10 2014

2.0.0-alpha

2.0.0.0-alpha https://github.com/ventoviro/windwalker-record

Windwalker Record package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework record windwalker