Jump to content

ExampleNode: Difference between revisions

From NusaATK
No edit summary
 
No edit summary
 
Line 1: Line 1:
A typical example of a node file looks like this:
A typical example of a node file looks like this:
<pre>
<syntaxhighlight lang="php">
<?php
<?php


class employee extends atkNode
class employee extends atkNode
{
{
  function employee()
    public function __construct()
  {
    {
    $this->atkNode("employee");
        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");
        $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");
    }
}
}


?>
</syntaxhighlight>
</pre>
 
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:
 
<syntaxhighlight lang="php">
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);
    }
}
</syntaxhighlight>


Original Poster: Ivo
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.

Latest revision as of 13:35, 12 February 2010

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.