Basic question on displaying name instead of id in a CRUD


I’m a new user to the ATK/UI suite, so this is probably a pretty basic question; hopefully the answer is equally as basic. I’m writing an application where there is a simple relationship between two tables, for example:

class User extends \atk4\data\Model {
public $table = ‘user’;

function init()
    $this->hasOne('city', new City());


class City extends \atk4\data\Model {
public $table = ‘city’;

function init()


This is a simple example to illustrate what I’m seeing; I realize it’s pretty trivial.

If I create a CRUD to manage the user table, the default popup to add or edit a user works nicely – I get a dropdown menu with the names of the available cities. However, the main CRUD table itself displays the city ID. How do I change this to get it to display the city name instead of the ID?

Thanks in advance for your help.


hey there,
don’t hesitate to ask, even it’s a basic question. That’s how we all learn stuff…
I am also quite new to atk-ui/-data so I may be wrong but I use a simliar structure.

First of all I set three properties on the model class:

public $id_field = 'id'; // I think this is the standard value already but it’s nice to know though
public $table = 'city'; // you use that already
public $title_field = 'name'; // this defines which column is used in the method “getTitle()” I think. The method is used in different built-in functions of atk for getting a name and stuff. This may already help in some situations

The second method may help you, too. You have to define which fields of your “hasOne”-city you want to further use in order to have the CRUD show it. You can do it like that:

$this->hasOne('city', [new City($db), 'their_field'=>'id', 'our_field'=>'user_id', 'caption' => 'City-ID'])
			->addFields(['city_name' => ['name', 'type' => 'string', 'caption' => 'City/Town'], 'city_population' => ['population', 'type' => 'integer', 'caption' => 'Roundabout population']]);

So with addFields you can load the values of the defined fields right into your model, I think using a single join query IIRC. You can then use them directly in your CRUD:

$crud = $app->add([new CRUD(),
		'fieldsRead' => ['city_name', 'city_population'],
		'fieldsUpdate' => ['city_population']

Hope this will help you a bit. And I hope everything’s correct. Maybe some more experienced user has an even better solution?


Hello and welcome,

You should change City class property title_field to ‘name’ in your case. This should do the trick.