Question

I am new to cakephp and just created a basic blog using scaffolding. There are two models: User and Post. When the posts are listed, it lists the user's name, which is not the primary key. I am not sure why it is listing the user's name. I want to make it list the primary key of the user. How do I do this? I apologize if this question is basic, but I cannot find an answer to it.

Here are the models:

class User extends AppModel{
    public $hasMany = array('Post');
}

class Post extends AppModel{
    public $belongsTo = array('User');
}

Here are the controllers:

class UsersController extends AppController{
    public $scaffold;
}

class PostsController extends AppController{
    public $scaffold;
}

This might be helpful. Here is the users table:

CREATE TABLE `cakephp_blog`.`users` (
  `id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `name` VARCHAR( 100 ) NULL DEFAULT NULL ,
  `email` VARCHAR( 150 ) NULL DEFAULT NULL ,
  `firstname` VARCHAR( 60 ) NULL DEFAULT NULL ,
  `lastname` VARCHAR( 60 ) NULL DEFAULT NULL
)

Thanks.

Was it helpful?

Solution

If you read the docs you'll see that cake is assuming that any field called name or title will be used as the displayField (99% of use cases).

If you want to display the primary key you can set:

public $displayField = 'id';

in your model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top