1

I am trying to extract ONLY the PlanDetails where PlanDetail.company_id = Company.id AND PlanDetail.id' => $id.. ( you can see the conditions in my controller below)..

Controller:

    function pd_list_by_company($id = null) {
    $this->recursive = 2; // I am going to use containable to trim this. 
    return $this->PlanDetail->find('all',
        array('conditions' => 
        array('AND' => 
        array('PlanDetail.company_id' => 'Company.id',
        array('PlanDetail.id' => $id)))));
    }

Test View:

$planDetailsByCompany = $this->requestAction('/planDetails/pd_list_by_company');

debug($planDetailsByCompany );

Output result of my debug??

Array()

If I remove the conditions and just have the find all, I get all PlanDetails as expected, so I know the data is being passed.. SQL debug dump even shows the query:

WHERE ((`PlanDetail`.`company_id` = 'Company.id') AND (`PlanDetail`.`id` IS NULL))

And yes, I did notice the $id is NULL, and I know the value needs to be there.. So maybe my question is why is the $id value not being passed to the controller even though I can see the PlanDetail.id value on a find('all') w/ out the conditions??

Thanks for any tips.

3 Answers 3

1

Since $id seems to be null, I would assume that you call the function without the parameter. And you don't get an error message, because as far as PHP is concerned the parameter is optional. In this case it's clearly required, so you should make it a required parameter in your function declaration:

function pd_list_by_company($id) {

Also you could simplify the return statement, you do not need the AND:

return $this->PlanDetail->find('all',
    array('conditions' => 
        array('PlanDetail.company_id' => 'Company.id','PlanDetail.id' => $id)
    )
);
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that too. No value is being passed. || Warning (2): Missing argument 1 for PlanDetailsController::pd_list_by_company() [APP/controllers/plan_details_controller.php, line 179] || Notice (8): Undefined variable: id [APP/controllers/plan_details_controller.php, line 181]
That means you should check where the function is called and make sure it specifies a parameter. The problem does not seem to be in pd_list_by_company() but rather in where you call the function.
1

To answer the question why is the $id not being passed is because you're not passing it

To pass say $id of 2 you need to do the following in your requestAction

$this->requestAction('/planDetails/pd_list_by_company/2');

3 Comments

Hmmm. That changes nothing, but I understand your point. I can call the value within the function like: $this->PlanDetail->id - gives me the current associated id in the url... I'm just trying to run a foreach for each PlanDetail based on the PlanDetail.company_id matching the Company.id.. I did not think this would be so tricky.
why are you declaring AND in the condition, also why the need to define the relationship with company? It will be declared in the model no?
You need to remove the array declaration around user_id in the conditions too
0

Seems to me that your code should just be

return $this->PlanDetail->find('array('PlanDetail.id' => $id));

Assuming you have the $this->PlanDetail->recursive flag set to > 0, your Model should already know about and return the associated data for any 'Company' table.....

I'm used to an old (1.3) version of CakePHP but the find() function is pretty basic and is designed to only return one row.

and yes, you definitely need to call the function with the id appended to the url, eg.

$planDetailsByCompany = $this->requestAction('/planDetails/pd_list_by_company/999');

Comments

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.