0

I'm trying to learn how to use addRef.

I think I need a way to tell addRef which field should link with 'id' in Master?

To test, I have a 'master' table:

<?php
class Model_TestMaster extends Model_Table {
   public $table='testmaster';
       function init(){
          parent::init();

      $this->addField('Description');
      $this->hasMany('testslave');
       }
}

and a 'slave' table:

<?php
class Model_TestSlave extends Model_Table {
   public $table='testslave';
       function init(){
          parent::init();

      $this->addField('MastersID');
      $this->addField('SubDescription');
       }
}

and then I set up the 'page' like this:

<?php
class page_test extends Page {
function init(){
    parent::init();

$page=$this;
$tabs = $this->add('Tabs');

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

if (! $crud->isEditing()) {
        // add subCRUD
        $sub_crud = $crud->addRef('testslave', array(
            'extra_fields' => array('MastersID','SubDescription')));
}
}

I think I need a way to tell addRef which field should link with 'id' in Master?

It displays Ok, but when I press the button to expand the slave I get:

Application Error: Child element not found

BaseException, code: 0

Additional information:

Raised by object: Object Model_TestSlave(51cf4a73__ter_testslave_model_testslave)
element: testmaster_id
:

Stack trace:
File        Object Name Stack Trace
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\atk4\lib/BaseException.php  :63     BaseException   BaseException->collectBasicData(Null)
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\atk4\lib/AbstractObject.php :545    BaseException   BaseException->__construct("Child element not found", Null)
/   :   51cf4a73__ter_testslave_model_testslave Model_TestSlave->exception("Child element not found")
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\atk4\lib\SQL/Model.php  :107        Loggercall_user_func_array(Array(2), Array(1))
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\atk4\lib/AbstractObject.php :331    51cf4a73__ter_testslave_model_testslave Model_TestSlave->exception("Child element not found")
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\atk4\lib\SQL/Model.php  :275    51cf4a73__ter_testslave_model_testslave Model_TestSlave->getElement("testmaster_id")
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\atk4\lib\SQL/Many.php   :79     51cf4a73__ter_testslave_model_testslave Model_TestSlave->addCondition("testmaster_id", "0000000001")
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\atk4\lib\SQL/Model.php  :248    51cf4a73__ter_testslave SQL_Many->ref(Null)
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\atk4\lib\View/CRUD.php  :316    asol_Test_tabs_view_htmlelement_crud_model_testmaster   Model_TestMaster->ref("testslave")
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\page/test.php   :15     asol_Test_tabs_view_htmlelement_crud    CRUD->addRef("testslave", Array(1))
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\atk4\lib/AbstractObject.php :306    asol_Test   page_test->init()
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\atk4\lib/ApiFrontend.php    :130    asol    Admin->add("page_Test", "Test", "Content")
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\atk4\lib/ApiWeb.php :428    asol    Admin->layout_Content()
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\atk4\lib/ApiFrontend.php    :39     asol    Admin->addLayout("Content")
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb\atk4\lib/ApiWeb.php :275    asol    Admin->initLayout()
C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb/index.php   :15     asol    Admin->main()

Thank you,

Mark

Jason Heo
  • 9,956
  • 2
  • 36
  • 64
Mark
  • 27
  • 5

1 Answers1

0
  1. Should be $this->hasMany('TestSlave'); not testslave. Otherwise there will be problems on Linux.

  2. Don't use $this->addField('MastersID'); Instead use $this->hasOne('TestMaster');

  3. Should be $crud->setModel('TestMaster'); not testmaster. Otherwise there will be problems on Linux.

  4. Instead

$sub_crud = $crud->addRef('testslave', array( 'extra_fields' => array('MastersID','SubDescription')));

use

$sub_crud = $crud->addRef('TestSlave', array( 'extra_fields' => array('testmaster_id','SubDescription')));

Also you can use field "testmaster" in extra_fields. That'll be title field of TestMaster model.

Idea here is that when you put hasOne('ModelName') in your model, then 2 fields are created in model. First one will have name "modelname_id" and will consist of ID of related model. Second will have name "modelname" and will consist of title field of related model.

DarkSide
  • 3,670
  • 1
  • 26
  • 34
  • You're welcome! It's almost always advisable to use both - hasMany and it's opposite - hasOne in related model. If you have both of them, then you can navigate back and forth using this relation. Otherwise you can navigate only in one direction and ... also it's possible that you'll have some issues doing that. Better always use both and you'll be fine :) – DarkSide Nov 29 '13 at 12:52