【问题标题】:Not sure how to retrieve my the values from array不知道如何从数组中检索我的值
【发布时间】:2011-12-08 08:37:37
【问题描述】:

我的 CakePHP 应用程序有两个问题(这是我在 CakePHP 中的第一个问题)。我正在尝试将旧的 php 网站转换为蛋糕。

1.问题

我的控制器接受参数 $id,但数据来自连接的表,所以在食谱中它有这样的内容

我的控制器 disc_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';
    }   
  }

型号

dish_category.php

class DishCategory extends AppModel{

    var $name = 'DishCategory';

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

如您所见, Dish.id=> '1' 是硬编码的,如何让它在那里动态化,以便我传递一个值并使其类似于 Dish.if =>$id ?.

所以这是我的第一个问题。

第二个问题与视图有关

该模型只返回一条记录,我怎样才能使它返回所有记录,以及我如何能够在当前视图中的代码和数组格式下面循环遍历它。

这是我的看法

 <?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 ( [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 ) ) 

感谢您的帮助,帮助我了解如何更好地做到这一点。谢谢。

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    首先,您的 DishCategoriesController 具有 model 属性,您可以删除它们。在您的控制器中,您将设置查找条件,如下所示:

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

    您的 DishCategory 模型看起来非常基本,您无需对关系条件进行硬编码:

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

    此时值得注意的是,由于 DishCategory hasOne Dish,使用上述查找查询,它只会返回一个单结果。但是,如果您返回多个结果,您可以像这样在视图中循环它们:

    <?php foreach ($dishes as $key => $dish): ?>
      <?php var_dump($dish) ?>
    <?php endforeach ?> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-25
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      相关资源
      最近更新 更多