Developer Blog

  • Blog
  • /
  • Extensions and Nested Sets
By Dracony on 30 May 2013

ORM has been updated with support for Extensions, which are modules for the ORM Model. They can help you a lot with encapsulating pieces of model logic into classes and reusing them.
And the first Extension to arrive is the support for Nested Sets, which can be added to your Model like this:

1
2
3
4
5
6
7
namespace App\Model;

class Animal extends \PHPixie\ORM\Model{
    protected $extensions = array(
        'nested'=>'\PHPixie\ORM\Extension\Nested'
    );
}

and used like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$birds = $pixie->orm->get('animal');
$birds->name = 'Birds';
$birds->nested->prepare_append();
$birds->save();

$sparrow = $pixie->orm->get('animal');
$sparrow->name = 'Sparrow';
$sparrow->nested->prepare_append($birds);
$sparrow->save();

//Refresh the element after tree has been altered
$birds = $pixie->orm->get('animal',$birds->id);

$cuckoo = $pixie->orm->get('animal');
$cuckoo->name = 'Cuckoo';
$cuckoo->nested->prepare_append($birds);
$cuckoo->save();

$birds = $pixie->orm->get('animal',$birds->id);
//Get all birds
$birds->nested->children()->find_all();

A more detailed explanation can be found in the updated ORM Tutorial.
Hope you like it =)

comments powered by Disqus