Cakephp 如何简化php中执行的查询行

Cakephp 如何简化php中执行的查询行,cakephp,Cakephp,在我的cakephp中,我需要从数据库中检索数据 如何以整洁的格式显示表格结果 控制器: function MarketingTaskmanagement(){ $data['list'] = $this->TravancoAdmin->get_task_all(); $this->set($data); $this->render('Marketing/taskmanagement'); } 型号: function get_task

在我的cakephp中,我需要从数据库中检索数据

如何以整洁的格式显示表格结果

控制器:

  function MarketingTaskmanagement(){
    $data['list'] = $this->TravancoAdmin->get_task_all();
    $this->set($data); 
    $this->render('Marketing/taskmanagement');
  }
型号:

function get_task_all(){

               $users = $this->query('select * from tbl_tasks');

        if($users){
            return $users;
        }else{
            return 1;
        }
    }
视图:

但它将值显示为许多数组:

Array ( [0] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 1 [task_title_mm] => ghjg [task_description_mm] => gjg [task_from_mm] => 09/04/2012 [task_to_mm] => 09/27/2012 ) ) [1] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 2 [task_title_mm] => hf [task_description_mm] => hfgh [task_from_mm] => 09/03/2012 [task_to_mm] => 09/27/2012 ) ) [2] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 3 [task_title_mm] => hf [task_description_mm] => hfgh [task_from_mm] => 09/03/2012 [task_to_mm] => 09/27/2012 ) ) [3] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 4 [task_title_mm] => hfh [task_description_mm] => fgh [task_from_mm] => 09/03/2012 [task_to_mm] => 09/20/2012 ) ) [4] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 5 [task_title_mm] => h [task_description_mm] => h [task_from_mm] => 09/04/2012 [task_to_mm] => 09/28/2012 ) ) [5] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 6 [task_title_mm] => hjk [task_description_mm] => hk [task_from_mm] => 09/05/2012 [task_to_mm] => 09/22/2012 ) ) [6] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 7 [task_title_mm] => v [task_description_mm] => v [task_from_mm] => 09/03/2012 [task_to_mm] => 09/28/2012 ) ) [7] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 8 [task_title_mm] => d [task_description_mm] => d [task_from_mm] => 09/03/2012 [task_to_mm] => 09/28/2012 ) ) [8] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 9 [task_title_mm] => f [task_description_mm] => d [task_from_mm] => 09/04/2012 [task_to_mm] => 09/27/2012 ) ) [9] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 10 [task_title_mm] => b [task_description_mm] => b [task_from_mm] => 09/05/2012 [task_to_mm] => 09/27/2012 ) ) ) 
在codeigniter中,有如下内容:

$users = $this->query('select * from tbl_tasks')->result();
function index() {
    $this->ModelAlias->recursive = -1;
    $this->paginate = array(
        'conditions' => array('ModelAlias.tasks' => 'incomplete')
    );
    $this->set('list', $this->paginate());
}
然后,它将显示正确的阵列,而不会出现任何复杂情况

那么我如何在cakephp中实现这一点呢

我需要在查看页面中显示表格结果中的所有
task\u title\u mm

我如何才能做到这一点?

使用“查找”方法

如果您需要查询的信息较少,请输入:

$this->ModelName->recursive = -1;

在找到方法之前。

既然你扩展了你的问题,我将详细介绍一下anwser

$this->ModelName->find('all');
在本机蛋糕搜索可以工作的地方,最好不要使用
$this->query()

如果您没有用于
tbl\u任务的模型文件
请创建一个:

<?php
class Task extends AppModel {
    var $name = 'Task';
    var $displayField = 'task_title_mm';
    var $useTable = 'tbl_tasks';
}
find('list')
如果将
'task\u title\u mm'声明为displayField,则可以使用
$this->task->find('all',array('fields'=>array('task\u title\u mm')),但结果的前缀将是
['Task']

函数get\u task\u all(){
$users=$this->query('select*fromtbl_tasks')

嘿,嘿,嘿!停在那里!那让我心脏病发作了

零。阅读烹饪书!不,说真的,停下来!在你继续之前,我建议你至少看一次烹饪书。从一开始就做正确的事情是正确的

  • 使用cake为您提供的适当方法来完成工作。SQL查询可以很好地完成工作,但请相信我,使用cake的内置方法将长期受益,并使您的生活更加轻松

  • 根据您关于如何以表格格式显示数据的查询,请使用cake bake:。cake bake实用程序将为您的应用程序控制器、模型和视图生成基本框架。cake的默认视图模板非常有用,几乎具有内置的所有功能、分页等

  • 想要分页结果吗?使用分页没有问题

    function index() {
        $this->ModelAlias->recursive = -1;
        $this->set('list', $this->paginate());
    }
    
    瞧!您的$list中有分页数据,在视图中随时可用。如果您的视图已烘焙,则所有内容都会根据需要在表中很好地显示

    不想获取所有内容?将条件插入到分页中,如下所示:

    $users = $this->query('select * from tbl_tasks')->result();
    
    function index() {
        $this->ModelAlias->recursive = -1;
        $this->paginate = array(
            'conditions' => array('ModelAlias.tasks' => 'incomplete')
        );
        $this->set('list', $this->paginate());
    }
    
    不仅如此,您还可以包括cake支持的几乎任何其他键,如“字段”、“顺序”、“限制”


    一旦你开始使用cake的函数,事情就会变得非常棒。为了获取相关数据,你可以使用cake的“containable”行为。这只是cake做得非常好的数百件事情中的一件。我不能在这里概述所有事情,我坚持请尝试一下烹饪书

    使用
    containable
    行为进行探索,它将使您的生活更轻松。比使用递归更好。通常您应该使用
    $this->set('some_var',$data);
    ,然后您可以访问此数据-
    foreach($some_var as$foo)…。