0

我有一个基于这个问题的问题。

相同的数据库:项目有很多关键字-关键字属于项目
访问方便使用Project TableKeywords/index.ctp$keyword['Project']['id']

但反过来,它似乎有点困难。这里有一点代码:

public function view($id = null) 
{
    if (!$id) 
    {
        throw new NotFoundException(__('Invalid project'));
    }
    $this->paginate['Project']['conditions'] = array('Project.id' => $id);
    $this->set('projects', $this->paginate());

    $this->set('project', $project);
}

Projects/view我尝试像这样访问关键字表:$project['Keyword']['id']我可能期望这不起作用。

4

1 回答 1

1

试试下面的代码:

public function view($id = null) 
{
    if (!$id) 
    {
        throw new NotFoundException(__('Invalid project'));
    }
    $this->Project->bindModel(array('hasMany' => array('Keyword' => array('className' => 'Keyword',
                                                       'foreignKey' => 'project_id')
                                    )), false);
    $this->paginate['conditions'] = array('Project.id' => $id);
    $this->paginate['recursive'] = '2';
    $this->set('projects', $this->paginate('Project'));

    $this->set('project', $project);
}
于 2013-09-20T09:34:01.120 回答