Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Entity Class

hperrin edited this page Oct 4, 2014 · 14 revisions

As mentioned before, entities are organized using tags. To add, remove, and check tags, the methods addTag, removeTag, and hasTag are used, respectively. Each takes any number of tags or an array of tags as arguments.

Manipulating Entity Tags
```php $entity = Entity::factory();

$entity->addTag('com_foobar', 'foo', 'bar'); $entity->hasTag('foo'); // True

$entity->removeTag('foo', 'bar'); $entity->hasTag('foo'); // False

```js
var entity = new Entity();

entity.addTag('com_foobar', 'foo', 'bar');
entity.hasTag('foo'); // True

entity.removeTag('foo', 'bar');
entity.hasTag('foo'); // False

To clear the cache of referenced entities, so that the next time one is accessed it will be pulled from the database, use the clearCache method in PHP and the refresh method in JavaScript.

Clearing the Reference Entity Cache
```php $entity = Entity::factory(); $entity->foo = Entity::factory(); $entity->foo->bar = 'Old value.'; $entity->foo->save(); $entity->save();

$entity = $nymph->getEntity($entity->guid);

$instOfFoo = $nymph->getEntity($entity->foo->guid); $instOfFoo->bar = 'New value.'; $instOfFoo->save();

echo $entity->foo->bar; // Outputs 'Old value.' $entity->clearCache(); echo $entity->foo->bar; // Outputs 'New value.'

```js
// Let's just pretend the entity and foo have both been saved.
alert(entity.get('foo').get('bar')); // Outputs 'Old value.'
entity.refresh().then(function(entity){
  entity.data.foo.ready(function(foo){
    alert(entity.get('foo').get('bar')); // Outputs 'New value.'
  });
});

Much like clearing the entity cache, you may need to refresh the entity's own datan in PHP. Use the refresh method for this.

Refreshing an Entity's Data
```php $entity = Entity::factory(); $entity->foo = 'Old value.'; $entity->save();

$instOfEnt = $nymph->getEntity($entity->guid); $instOfEnt->foo = 'New value.'; $instOfEnt->save();

echo $entity->foo; // Outputs 'Old value.' $entity->refresh(); echo $entity->foo; // Outputs 'New value.'


As you've already seen in the examples, to save an entity, use the `save` method. Likewise, to delete the entity, use the `delete` method. You can also call the `saveEntity`, `deleteEntity`, and `deleteEntityById` methods of Nymph. The `Entity` class uses these methods.

<div dir="rtl">Different Ways of Saving and Deleting Entities</div>
```php
$entity = Entity::factory();

// Save the entity.
$entity->save();
// or
$nymph->saveEntity($entity);

// Delete the entity.
$entity->delete();
// or
$nymph->deleteEntity($entity);
// or
$nymph->deleteEntityById($entity->guid);
var entity = new Entity();

// Save the entity.
entity.save().then(function(entity){});
// or
Nymph.saveEntity(entity).then(function(entity){});

// Delete the entity.
entity.delete().then(function(){}, function(){alert('uh oh')});
// or
Nymph.deleteEntity(entity).then(function(){}, function(){alert('uh oh')});
// or
Nymph.deleteEntities(arrayOfEntities).then(function(){}, function(){alert('uh oh')});

Several methods are provided by entities to find and compare them with other data. Entities cannot simply be checked using the == operator. It will almost always fail because of things like entity caching and sleeping references. Instead, you can use the following entity methods.

  • is - Perform a less strict comparison of two entities. To return true, the entity and the object passed must meet the following criteria.
  • They must be entities.
  • They must have equal GUIDs. (Or both can have no GUID.)
  • If they have no GUIDs, their data must be equal.
  • equals - Perform a more strict comparison of two entities. To return true, the entity and the object passed must meet the following criteria.
  • They must be entities.
  • They must have equal GUIDs. (Or both can have no GUID.)
  • They must be instances of the same class.
  • Their data must be equal.
  • inArray - Check whether the entity is in an array. Takes two arguments, the array and a boolean $strict. If $strict is false, the function uses is to compare, and if it's true, the function uses equals.
  • arraySearch - Search an array for the entity and return the corresponding key. Takes two arguments, the array and a boolean $strict. If $strict is false, the function uses is to compare, and if it's true, the function uses equals. This method may return 0, which evaluates to false, so you should use inArray if you are only checking whether the entity is in the array.
Comparing Entities
```php $entity = Entity::factory(35); // Assuming this entity exists. $entity2 = Entity::factory(35); $entity->is($entity2); // True

$array = array('foo' => null, 'bar' => $entity); echo $entity->arraySearch($array); // Outputs 'bar'

```js
var entityPromise = new Entity(35); // Assuming this entity exists.
var entityPromise2 = new Entity(35);
Promise.all([entityPromise, entityPromise2]).then(function(entities){
  var entity = entities[0],
      entity2 = entities[1];
  entity.is(entity2); // True

  var arr = [null, entity];
  alert(entity.arraySearch(arr)); // Outputs '1'
});

Nymph provides three methods to sort entities. Each one uses a different method of sorting.

  • hsort - Sort an array of entities hierarchically by a specified property's value.
  • psort - Sort an array of entities by parent and a specified property's value. If they have the same parent, their own value is used.
  • sort - Sort an array of entities by a specified property's value.
Sorting Entities
```php $cats = $nymph->getEntities( array('class' => FoobarCategory), array('&', 'tag' => array('foobar', 'category') ) );

$caseSensitive = false; $reverse = false;

// Sort categories hierarchically by their name. $nymph->hsort($cats, 'name', 'parent', $caseSensitive, $reverse); // Sort categories by their parent's name. $nymph->psort($cats, 'name', 'parent', $caseSensitive, $reverse); // Sort categories by their name. $nymph->sort($cats, 'name', $caseSensitive, $reverse);

```js
var catsPromise = Nymph.getEntities(
    {'class': 'FoobarCategory'},
    {'type': '&',
      'tag': ['foobar', 'category']
    }
  );

catsPromise.then(function(cats){
  var caseSensitive = false,
      reverse = false;

  // Sort categories hierarchically by their name.
  Nymph.hsort(cats, 'name', 'parent', caseSensitive, reverse);
  // Sort categories by their parent's name.
  Nymph.psort(cats, 'name', 'parent', caseSensitive, reverse);
  // Sort categories by their name.
  Nymph.sort(cats, 'name', caseSensitive, reverse);
});
Clone this wiki locally