1

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
            )
        )
    )
)
3
  • It is always a good hint to not use terms like "a bit strange" but instead (additionally) clearly say what you expected exactly instead. If someone know cake PHP she or he might be just able to tell you how that is done by the example you need, but couldn't but the way you do it (your "a bit strange" way). Commented Sep 27, 2013 at 9:56
  • okay good argument. I updated my question Commented Sep 27, 2013 at 10:03
  • don't you want to paginate the records ? Commented Sep 27, 2013 at 10:14

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Now your project array should suffice your requirement.

3 Comments

You are partial right but You didn't use paginate witch allows to sort the table.
do you want to sort in view page? which doesn't mean anything here.
you can't get the array in the way you want if you use paginate here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.