PHPackages                             windwalker/record - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. windwalker/record

ActiveWindwalker-package[Framework](/categories/framework)

windwalker/record
=================

Windwalker Record package

3.5.23(6y ago)115LGPL-2.0-or-laterPHPPHP &gt;=7.1.3

Since Oct 5Pushed 5y ago3 watchersCompare

[ Source](https://github.com/ventoviro/windwalker-record)[ Packagist](https://packagist.org/packages/windwalker/record)[ Docs](https://github.com/ventoviro/windwalker-record)[ RSS](/packages/windwalker-record/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (82)Used By (0)

Windwalker Record
=================

[](#windwalker-record)

Windwalker Record is a simple ActiveRecord to operate database row.

Installation via Composer
-------------------------

[](#installation-via-composer)

Add this to the require block in your `composer.json`.

```
{
    "require": {
        "windwalker/record": "~3.0"
    }
}
```

Use Record
----------

[](#use-record)

New a instance.

```
use Windwalker\Record\Record;

// Record object for users table
$user = new Record('users');
```

Or create a class:

```
use Windwalker\Record\Record;

class UserRecord extends Record
{
    protected $table = 'users';

    protected $keys = 'id';
}

$user = new UserRecord;
```

### Load A Record

[](#load-a-record)

```
$user->load(25); // Load by primary key

$user->load(array('alias' => $alias)); // Load by field name.
```

Check row exists

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

### Bind Data

[](#bind-data)

```
$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:

NamenameusernamepasswordThe fields which not in this table will be remove after binding data.

```
$user->alias; // null
```

That makes our fields in Record will always same as DB table.

### Store

[](#store)

#### Create A New Row

[](#create-a-new-row)

If primary not exists, Record will create a new row in table.

```
$data = array(
    'name'     => 'Sakura',
    'username' => 'sakura',
    'password' => '1234'
);

$user->bind($data);

$user->store();

echo $user->id; // Auto generated id
```

#### Update A Existing Row

[](#update-a-existing-row)

If primary key exists, Record will update it.

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

$user->bind($data);

$user->store();
```

### Validate

[](#validate)

Check method help you validate data.

```
class UserRecord extends Record
{
    // ...

    public function validate()
    {
        if (!$this['name'])
        {
            throw new InvalidArgumentException('Name empty.');
        }

        return true;
    }
}
```

Then we call `validate()` before `store()`.

```
$user->bind($data)
    ->validate()
    ->store();
```

### Delete

[](#delete)

```
$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)

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

This is an example of mutator:

```
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;
    }
}
```

Use camel-case style to define a method, then when you access the `created_date` field, this method will be auto executed.

```
$articleRecord->created_date = new \DateTime('now');

echo $articleRecord->created_date; // 2016-03-02 12:30:29
```

And an example of accessor:

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

And now you can get `DateTime` object back:

```
echo $articleRecord->created_date->format('Y-m-d H:i:s'); // 2016-03-02 12:30:29
```

### Casts

[](#casts)

Add casts to auto convert value type after read from DB:

```
