Search code examples
formsgridcrudatk4agiletoolkit

Reference Table Lookup Ability in Agile Toolkit ATK4 CRUD


I need help in Agile Toolkit CRUD Grid/Form.

I made a CRUD Grid/Form in Agile Toolkit for Employee, Position and Department. It was very easy. But i am now having difficulty trying to set the Position and Department columns. Instead of showing 'id' value, i'd like to show the referenced text in 'pos_desc' and 'dept_desc' respectively.

Also in CRUD's Add/Edit Form, it only shows 'id' and not the corresponding text. Is there a way to set this using the description text but save the 'id' instead on commit?

Thanks!

Here is the project's directory structure and some code snippets:

    + atk4
    + atk4-addons
    + empmaster
      + admin
        + lib
        + page
      + doc
      + lib
        + Model
      + page
      + templates

admin/lib/Admin.php

    class Admin extends ApiFrontend {
    :
    :   
       function init(){
    :
    :
          $this->addLocation('..',array(
                      'php'=>array(
                            'lib',
                            )
                      ));
          $this->addLocation('../..',array(
                      'php'=>array(
                            'atk4-addons/mvc',
                            'atk4-addons/misc/lib',
                            )
                      ))
                ->setParent($this->pathfinder->base_location);
    :
    :

lib/Model/Employee.php

    class Model_Employee extends Model_Table {
       public $entity_code = 'emp';

       function init() {
          parent::init();

          $this->addField('eeid')->caption('Emp ID');
          $this->addField('fnm')->caption('First Name');
          $this->addField('mnm')->caption('Middle Name');
          $this->addField('lnm')->caption('Last Name');

          $pos=$this->addField('pos_id')->caption('Position');
          $pos->refModel('Model_Postition');

          $dep=$this->addField('dept_id')->caption('Department');
          $dep->refModel('Model_Department');

          // #1 refModel gives error if declared w/out 'Model_' prefix
          // #2 Position & Department caption not on grid, but only on form
       }
    }

lib/Model/Position.php

    class Model_Position extends Model_Table {
       public $entity_code = 'pos';

       function init() {
          parent::init();

          $this->addField('pos_desc');
       }
    }

lib/Model/Department.php

    class Model_Department extends Model_Table {
       public $entity_code = 'dept';

       function init() {
          parent::init();

          $this->addField('dept_desc');
       }
    }

admin/page/index.php

    $crud = $tabs->addTab('Employee Master')->add('CRUD')->setModel('Employee');

Solution

  • By default it looks for the field named "name" on the model. If you don't have this field, you will need to override toStringSQL function in the Model_Position.

    See this answer for more information:

    reference field on form