Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6944987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:22:40+00:00 2026-05-27T13:22:40+00:00

I have two issues with my CakePHP application (its my first one in CakePHP).

  • 0

I have two issues with my CakePHP application (its my first one in CakePHP). I am trying to convert an old php website to cake.

1.Issue

I have my controller that accepts a parameter $id, but the data is comming from joined tables so in the cookbook it had something like this

MY Controller
dish_categories_controller.php

class DishCategoriesController extends AppController {


    var $uses = array("DishCategory");
    var $hasOne ='';
    function get_categories($id)
    {

       $this->set('dishes',$this->DishCategory->find());
       $this->layout = 'master_layout';
    }   
  }

model

dish_category.php

class DishCategory extends AppModel{

    var $name = 'DishCategory';

    var $hasOne = array(
            'Dish' => array(
              'className' => 'Dish',
              'conditions' => array('Dish.id' => '1'),
              'dependent' => true
                   )
           );
 }

As you can see the Dish.id=> ‘1’ is hard coded, how can make it dynamic there so that I pass a value and make it something like Dish.if =>$id ?.

So that was my first issue.

The second issue is related to the view

That model returns only one record, how can I make it so that it returns all and also how would I be able to loop through that, below the code in the view currently and the array format.

This is in my view

 <?php
         echo $dishes['DishCategory']['category_info'];
         echo $dishes['DishCategory']['category_title'];
         echo $dishes['Dish']['dish_name'];
         echo $dishes['Dish']['dish_image'];        
         echo $this->Html->image($dishes['Dish']['dish_image'], array('alt' => 'CakePHP'))


    ?>

Array Format

Array ( [DishCategory] => Array 
       ( [id] => 1 [category_name] => Appetizers 
         [category_keywords] => appetizer, appetizers 
         [category_title] => Our Side Dishes 
         [category_info] => Test Test

[dish_id] => 1 ) 
      [Dish] => Array ( [id] => 1 
                        [dish_name] => Rice 
                        [dish_disc] => The Best flavor ever 
                        [dish_price] => 2.90 [dish_image] => /img/rice_chicken.jpeg [dish_category_id] => 1 
[dish_price_label] => Delicious Arepa ) ) 

I would appreciate your help to help me understand how to better do this. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:22:41+00:00Added an answer on May 27, 2026 at 1:22 pm

    Firstly, you DishCategoriesController has model properties, you can remove them. In your controller, you will set up the conditions for the find like so:

    class DishCategoriesController extends AppController {
    
      function get_categories($id)
      { 
        // find category with a dish of $id
        $this->set('dishes', $this->DishCategory->find('all', array(
          'conditions' => array(
            'Dish.id' => $id
          )
        )));
    
        // set master layout
        $this->layout = 'master_layout';
      }   
    
    }
    

    Your DishCategory model will look very basic, you don’t need to hard code the relationship condition:

    class DishCategory extends AppModel {
    
      /**
       * hasOne associations
       *
       * @var array
       */
        public $hasOne = array(
            'Dish' => array(
                'className' => 'Dish',
                'foreignKey' => 'dish_category_id'
            )
        )
    }
    

    At this point it is worth noting that since the DishCategory hasOne Dish, using the above find query, it will only ever return a single result. But, it you were returning multiple results, you could loop through them in your view like so:

    <?php foreach ($dishes as $key => $dish): ?>
      <?php var_dump($dish) ?>
    <?php endforeach ?> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two issues I'm trying to work though. The first is I'm trying
I'm using Subversion for one of my PHP projects. I have two questions/issues: How
I have made a simple test application for the issue, two winforms each containing
The issue at hand is this. We have a web application with two different
I have two issues both related to (I believe) my SQL Server setup. I
I have found many people with simliar issues but no soultions...basically I have two
I have inherited a middle tier system with some multi-Threading issues. Two different threads,
I have a very strange issue on my hands. I have two IIS websites
What is the issue with this code? Here we have two files: classA.h and
I have simple issue setting a two-way databinding of a checkbox in Silverlight 3.0.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.