我的目标是显示 1 个项目和许多关键字。我的数据库很简单:
项目有很多关键字
关键字属于项目
到现在为止还挺好。现在我尝试在我的ProjectsController
:
public function view($id = null)
{
$this->Project->bindModel(array('hasMany' => array('Keyword' => array('className' => 'Keyword',
'foreignKey' => 'project_id')
)), false);
$this->paginate['Project']['conditions'] = array('Project.id' => $id);
$this->paginate['recursive'] = '2';
$this->set('projects', $this->paginate('Project'));
$Projects = $this->paginate('Project');
$this->set('projects', $this->paginate());
}
通过打印出数组,它看起来有点出乎意料:
Array
(
[0] => Array
(
[Project] => Array
(
[id] => 3
[title] => Foo
[created] => 2013-08-05 17:39:07
[modified] => 2013-08-05 17:39:07
)
[Keyword] => Array
(
[0] => Array
(
[id] => 1
[project_id] => 3
[title] => Num1
[demand] => 50000000000
[competition] => 37889.56700
[cpc] => 676.50
[created] => 2013-06-26 17:54:48
[modified] => 2013-09-19 13:37:25
)
)
)
[1] => Array
(
[Project] => Array
(
[id] => 4
[title] => Bar
[created] => 2013-08-05 17:39:07
[modified] => 2013-08-05 17:39:07
)
[Keyword] => Array
(
[0] => Array
(
[id] => 3
[project_id] => 4
[title] => Num1
[demand] => 76534000000
[competition] => 5555.55560
[cpc] => 99.34
[created] => 2013-06-26 17:54:48
[modified] => 2013-09-19 13:37:36
)
)
)
)
现在我有问题,我如何用右边的方式显示它Project.id
?因为新创建的数组包含的 id 不同于Project.id
. 我的问题是如何过滤Project.id
仅在我的/View/[id]
编辑
我认为最好的工作方式是这样的数组结构:
Array
(
[Project] => Array
(
[id] => 3
[title] => Lerncoachies
[created] => 0000-00-00 00:00:00
[modified] => 2013-08-05 17:39:07
)
[Keyword] => Array
(
[0] => Array
(
[id] => 1
[project_id] => 3
[title] => Num1
[demand] => 50000000000
[competition] => 37889.56700
[cpc] => 676.50
[created] => 2013-06-26 17:54:48
[modified] => 2013-09-19 13:37:25
)
)
)
)