Display two fields in dropdown


#1

Hi,
I’m new in agile toolkit and I don’t understand how to make a dropdown with two fields from database.
ID description
Example;
1 - the first record on the database
2- the second record on the database …

I think that I should use “Expression” but when I write this code :
class LOCATION extends \atk4\data\Model
{
public $table = ‘location’;
function init()
{
parent::init();
$this->addField(‘lid’);
$this->addField(‘description’);
$this->addField(‘phonenumber’);
$this->addExpression(‘twofields’)->set(‘locid +" - " + description’);
$this->hasMany('NUC,new NUC());
}
}
My web page is not showing…
Thank you for you help.


#2

Hi and welcome to ATK. You can only have one field in a drop-down, but you can create Expression that contains expression which is made up from multiple fields:

to your model add:

$this->addField('name', '[id] - [description']);

Then your dropdown (which shows name as a default $model->title_field) will show whaat you expect.


#3

Hi,
Thank you very much for your help. But when I add this line in the class location , the website does not work (I do not see anything on the page).

This is my first class :
class NUKE extends \atk4\data\Model
{
public $table = ‘nuke’;
function init()
{
parent::init();
$location = new LOCATION();
$this->hasOne(‘locid’,$location)->addField(‘description’);
$this->addField(‘hostname’,[‘required’=>true]);
$this->addField(‘animid’,[‘required’=>true]);
$this->addField(‘animparam’,[‘values’=>[‘N’=>‘N’,‘R’=>‘R’,‘L’=>‘L’],‘required’=>true]);
$this->addField(‘sheduleid’);
}
}

This is my second class :
class LOCATION extends \atk4\data\Model
{
public $table = ‘location’;
function init()
{
parent::init();
$this->addField(‘locid’);
$this->addField(‘description’);
$this->addField(‘tel’);
//I do not see anything on the web page when I add this line
// $this->addField(‘name’,’[locid]-[description]’);
$this->hasMany(‘NUKE’,new NUKE());
}
}

And then I do that :

$app = new \atk4\ui\App();
$app->title = ‘Gestion de la table NUKE’;
$app->initLayout($app->stickyGET(‘layout’) ?: ‘Admin’);

$db = new \atk4\data\Persistence_SQL(
‘pgsql:host=XXX.XX.XX.XXX;dbname=XXXXX’,
‘XXXXX’,
‘XXXXX’
);
$app->layout->add(‘CRUD’)->setModel(new NUKE($db));

Each nuke has a location. Actually when I update a nuke I have a dropdown but only with the id of all location. I want to have a dropdown with id + description .
Thank you very much.


#4

my guess is that you cannot subtract “description” which would be a varchar field in the database. You should also enable PHP error output to see what went wrong.