CTSchmidt
CTSchmidt

Reputation: 1205

cakephp formatting an array

My goal is to display 1 Projects and many Keywords to it. My DB is simple:

Project has many Keywords
Keyword belongs to Project

So far so good. Now I try to get the Information in my 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());
}

by printing out the array it looks a bit unexpected:

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
            )
        )
    )
)

Now I have the problem, how do i display it with the right Project.id? because the new created array contains a different id than Project.id. My question is how do I filter the right Project.id for display it only in my /View/[id]

EDIT

I think the best way to work with, is an array structure like this:

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
            )
        )
    )
)

Upvotes: 1

Views: 130

Answers (2)

jodator
jodator

Reputation: 2465

From descriptions it looks like you'd like to paginate Keywords not Projects - so you have 'unexpected result'.

This is an expected result for paginating Projects.

If you'd like to paginate Keywords then:

$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);

You'll have a view with 1 Project and you'll be able to paginate Keywords related to given project with sorting.

Upvotes: 1

Anil kumar
Anil kumar

Reputation: 4177

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);
}

Now your project array should suffice your requirement.

Upvotes: 1

Related Questions