Common Behaviors

Strata Models should inherit either one of App\Model\AppModel or App\Model\AppCustomPostType. Instead of passing behavior between model classes through inheritance, it should be applied using PHP Traits.

Start by building your behavior as a Trait and place it under ~/src/Model/Behavior/. Here we add more flexible query methods under the QueriablePostTrait:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php

namespace App\Model\Behavior;

trait QueriablePostTrait {

    public function byName()
    {
        return $this
            ->orderby("post_title")
            ->direction("ASC");
    }

    public function published()
    {
        return $this->status("publish");
    }

    public function byRecency()
    {
        return $this
            ->orderby("creation_date")
            ->direction("DESC");
    }

    public function byMenuOrder()
    {
        return $this
            ->orderby("menu_order")
            ->direction("ASC");
    }
}

By adding the Trait to either one of your project’s model classes or the common AppModel class it will gain the prepared behavior.

1
2
3
4
5
6
7
8
9
10
11
12
<?php
namespace App\Model;

use App\Model\Behavior\QueriablePostTrait;
use Strata\Model\Model as StrataModel;

class AppModel extends StrataModel
{
    use QueriablePostTrait;

}
?>