0

I've found a problem with the CRUD and hasOne() where the $display_field is ignored in the "add" form. If the referenced model has a 'name' field, then the drop down menu in the Add form of the CRUD is populated correctly. However, if the hasOne() model uses the $display_field parameter I'd expect the Add form to display the field specified, however it does not. It will only display the 'name' field or the 'id' of the referenced model rather than the field specified by $display. However, after the entry is added, the correct $display_field is used to display the entries. So the problem is somewhere in the Add operation.

For example:

Author

class Model_Author extends Model_Table {

public $table='author';

function init(){
  parent::init();
  $this->addField('long_name');
  $this->addField('DOB');

  }
}

Book

class Model_Book extends Model_Table {

public $table='book';

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

 $this->addField('title');
 $this->hasOne('Author', 'author_id', 'long_name');
 }

}

CRUD page.

class page_manager extends Page {

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

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

$crud=$tabs->addTab('Book')->add('CRUD');
   } 
 }

When you select the Add button, the Author field will display the id number of the authors in the author table. However, after you add a Book entry, the correct fields (i.e. long_name) is used to display the existing entries.

Cheers

LostInTheWeb
  • 187
  • 1
  • 11

1 Answers1

0

I indeed experienced the same behaviour and agree to expect that the third argument of hasOne() which is the display field should override the model $title_field.

I isolated the cause and found a way to improve. Open the file atk4/lib/Field/Reference.php and in the function getModel() add the line to set the display field like this:

  function getModel(){
        if(!$this->model)$this->model=$this->add($this->model_name);
        if($this->display_field) $this->model->title_field=$this->display_field;
        return $this->model;
    }

Here it is working as you would expect, which is that hasOne() will decide which field to display in the ADD form of the CRUD. However I cannot oversee any side affects or maybe other places to add this display_field.

Bob Siefkes
  • 1,133
  • 9
  • 11