【问题标题】:How does this cakephp scaffold know to display the name and not the primary key id?这个 cakephp 脚手架如何知道显示名称而不是主键 ID?
【发布时间】:2013-09-06 21:57:41
【问题描述】:

我是 cakephp 新手,刚刚使用脚手架创建了一个基本博客。有两种模型:User 和 Post。列出帖子时,它会列出用户名,而不是主键。我不确定它为什么列出用户名。我想让它列出用户的主键。我该怎么做呢?如果这个问题是基本的,我很抱歉,但我找不到答案。

以下是模型:

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

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

这里是控制器:

class UsersController extends AppController{
    public $scaffold;
}

class PostsController extends AppController{
    public $scaffold;
}

这可能会有所帮助。这是用户表:

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
)

谢谢。

【问题讨论】:

    标签: cakephp has-many scaffolding belongs-to model-associations


    【解决方案1】:

    如果您阅读 docs,您会发现 cake 假设任何名为 nametitle 的字段都将用作 displayField(99% 的用例)。

    如果要显示主键可以设置:

    public $displayField = 'id';
    

    在你的模型中。

    【讨论】:

    • 优秀。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 2019-06-26
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多