Jump to content

ExampleNode

From NusaATK

A typical example of a node file looks like this:

<?php

class employee extends atkNode
{
    public function __construct()
    {
        parent::__construct("employee");

        $this->add(new atkAttribute("id", AF_AUTOKEY));
        $this->add(new atkAttribute("firstname"));
        $this->add(new atkAttribute("lastname", AF_SEARCHABLE|AF_OBLIGATORY));
        $this->add(new atkDateAttribute("hiredate"));

        $this->setTable("employee");
    }
}

While this is an acceptable use of Nodes, it is more common to use atkMetaNode syntax, which, for the above example, would look like this:

class employee extends atkMetaNode
{
    protected $table = "employee"; // this is optional if the tablename is equal to the class name of the node

    public static function meta(atkMetaPolicy $policy)
    {
        $policy->get("lastname")->addFlag(AF_SEARCHABLE);
    }
}

In the latter example, all attributes are derived from the database metadata. This includes flags such as AF_OBLIGATORY. You only need to specify behaviour that it can't derive from the db, such as AF_SEARCHABLE.