PHPackages                             socialengine/unum - 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. [Database &amp; ORM](/categories/database)
4. /
5. socialengine/unum

ActiveLibrary[Database &amp; ORM](/categories/database)

socialengine/unum
=================

Unum: A simple entity implementation

2.0.1(10y ago)23.6k1[1 issues](https://github.com/SocialEngine/Unum/issues)MITPHPPHP &gt;=5.5.0

Since Jul 24Pushed 10y ago7 watchersCompare

[ Source](https://github.com/SocialEngine/Unum)[ Packagist](https://packagist.org/packages/socialengine/unum)[ RSS](/packages/socialengine-unum/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (2)Versions (5)Used By (0)

Unum: A simple entity implementation
====================================

[](#unum-a-simple-entity-implementation)

[![Build Status](https://camo.githubusercontent.com/0c6f0e475318d62458430d29abb8808777d36c6786d6677bfe7c7150f5390f75/68747470733a2f2f7472617669732d63692e6f72672f536f6369616c456e67696e652f556e756d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/SocialEngine/Unum)[![Latest Stable Version](https://camo.githubusercontent.com/ee0f47045085aae15948b3285d1f4d2f79628f3ce48b0824e888c8445b7a9d77/68747470733a2f2f706f7365722e707567782e6f72672f736f6369616c656e67696e652f756e756d2f76657273696f6e2e706e67)](https://packagist.org/packages/socialengine/Unum) [![License](https://camo.githubusercontent.com/238b8a28c90c53b0720ce2cd387e0e5cd1eca631c2c633203d78b50ee8201b3b/68747470733a2f2f706f7365722e707567782e6f72672f736f6369616c656e67696e652f756e756d2f6c6963656e73652e737667)](https://packagist.org/packages/socialengine/unum)

Installation
------------

[](#installation)

Require this package with composer:

```
$ composer require socialengine/setup-test-db

```

Usage
-----

[](#usage)

Create a class with protected attributes that extends `SocialEngine\Unum\Entity`:

```
class MyEntity extends \SocialEngine\Unum\Entity
{
  protected $name;
  protected $email;
}
```

From there, new entities can be new-ed with an array of attributes:

```
$entity = new MyEntity(['name' => 'Duke', 'email' => 'support@socialengine.com']);
```

### Accessing Attributes

[](#accessing-attributes)

Attributes can be accessed with either array or object syntax:

```
$entity = new MyEntity(['name' => 'Duke', 'email' => 'support@socialengine.com']);
echo $entity->name; // Duke
echo $entity['email']; // support@socialengine.com
```

Accessing an attribute which has not been "loaded" with data will throw an `InvalidArgumentException`.

By defining a method of the form `get{AttributeName}`, the entity will use that method to return a property's value.

Note: snake\_case attributes will be converted to PascalCase for methods, so `test_prop` becomes `getTestProp`

```
class GetMethodEntity extends \SocialEngine\Unum\Entity
{
    protected $name;

    protected function getName()
    {
        return $name . ' From Method!';
    }
}

$entity = new GetMethodEntity(['name' => 'Hello']);
var_dump($entity->name); // string(18) "Hello From Method!"
```

### Assigning Attributes

[](#assigning-attributes)

Attributes can be assigned with either array or object syntax:

```
$entity = new MyEntity();
$entity->name = 'My Name';
$entity['email'] = 'fake@example.com';
var_dump($entity->toArray());
/*
array(2) {
  'name' =>
  string(4) "Duke"
  'email' =>
  string(24) "support@socialengine.com"
}
*/
```

Assigning to an attribute which does not exist will throw an `InvalidArgumentException`.

By defining a method of the form `set{AttributeName}`, the entity will use that method to set a property's value.

Note: snake\_case attributes will be converted to PascalCase for methods, so `test_prop` becomes `setTestProp`

```
class SetMethodEntity extends \SocialEngine\Unum\Entity
{
    protected $name;

    protected function setName($value)
    {
        $this->name = $name . ' Set By Method!';
    }
}

$entity = new SetMethodEntity(['name' => 'Hello']);
var_dump($entity->name); // string(20) "Hello Set By Method!"
```

### Dirty Attributes

[](#dirty-attributes)

Assigning an entity's attribute a value which differs than its current value will mark that attribute as "dirty". To retrieve an entity's dirty attributes, pass `true` as the `$dirty` flag to `toArray`

```
$entity = new MyEntity();
$entity->name = 'My Name';
var_dump($entity->toArray(true));
/*
array(1) {
  'name' =>
  string(7) "My Name"
}
*/
```

Entities can be "cleaned" with the `clean` method, which will remove the "dirty" mark from all attributes.

```
$entity = new MyEntity();
$entity->name = 'My Name';
$entity->email = 'whatever';
$entity->clean();
var_dump($entity->toArray(true));
/*
array(0) {
}
*/
```

### Meta Attributes

[](#meta-attributes)

You can set and get "meta" attributes - ones that do not have attributes directly attached to them.

This is good if you have an entity that stores a `category_id` but does not store the category name

```
