-
Notifications
You must be signed in to change notification settings - Fork 5
Entity Class
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.
$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.
$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.
$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 usesis
to compare, and if it's true, the function usesequals
. -
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 usesis
to compare, and if it's true, the function usesequals
. This method may return 0, which evaluates to false, so you should useinArray
if you are only checking whether the entity is in the array.
$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.
$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);
});
Home | Setup Guide | API Docs | Full Code API Docs
© Copyright 2014-2019 SciActive.com