Cakephp 格式化数组


cakephp formatting an array

我的目标是显示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显示它? 因为新创建的数组包含与Project.id不同的 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
            )
        )
    )
)

从描述来看,您似乎想分页关键字而不是项目 - 因此您有"意外结果"。

这是分页项目的预期结果。

如果您想对关键字进行分页,请:

$this->Project->recursive = 1;
$project = $this->Project->findById($id);
$this->loadModel('Keywords'); // I don't remember if this is needed
$this->paginate['Keywords'] = array(
    'project_id' => $project['Project']['id']
);
$this->set('keywords', $this->paginate('Keyword'));
$this->set('project', $project);

您将拥有一个包含 1 个项目的视图,并且您将能够通过排序对与给定项目相关的关键字进行分页。

public function view($id = null) {
    $this->Project->bindModel(array(
        'hasMany' => array('Keyword' => array(
            'className' => 'Keyword',
            'foreignKey' => 'project_id')
        )), false
    );
    $this->Project->recursive = 2;
    $project = $this->Project->findById($id);
}

现在,您的项目数组应该可以满足您的要求。