简体   繁体   English

CakePHP将project_id显示为名称

[英]CakePHP show project_id as name

My DB structure is quite simple: Projects has many Keywords. 我的数据库结构非常简单:项目有很多关键字。

In my Keywords/index.ctp I have a <Table> which shows all keywords and displays their project_id . 在我的Keywords/index.ctp我有一个<Table> ,它显示了所有keywords并显示了他们的project_id Instead of the ID I want to display the name of the projects . 而不是ID我想显示projectsname

Is there a simple code-line for it? 它有一个简单的代码行吗?

Example: 例:

keyword name | project 
Foo           1
bar           1
ipsum         2

to

keyword name | project 
Foo           DaFoo
bar           DaFoo
ipsum         MoreFoo

如果您在关键字模型中定义了belongsTo关系,那么您可以像这样访问Project数据:

$keyword['Project']['name'];

CakePHP Associations for more info please visit: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html CakePHP协会了解更多信息,请访问: http//book.cakephp.org/2.0/en/models/associations-linking-models-together.html

<?php 
class Keyword extends AppModel {

    var $name = 'Keyword';

    var $belongsTo = array(
        'Project' => array(
            'className' => 'Project',
            'dependent' => true
        )
    );
}

class Project extends AppModel {

    var $name = 'Project';

     var $hasMany = array(
        'Keyword' => array(
            'className' => 'Keyword',
            'dependent' => true
        )
    );

}

?> ?>

and use in .ctp file as below 并在.ctp文件中使用如下

<?php 
$keywords['Project']['name'];
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM