【问题标题】:Paginating and retriving data with CakePHP使用 CakePHP 分页和检索数据
【发布时间】:2011-02-18 02:42:36
【问题描述】:

嗨 我正在尝试从 KindsController 进行分页,但使用来自 LinkModel 的数据

表格:

  Kinds       links          
     id           id
     name         title
     order        url
                  kind_id
                  order

型号:

class Kind extends AppModel{

        var $name = 'Kind';
        var $hasMany = 'Link';
    }

控制器:

class KindsController extends AppController{

    var $name = 'Kinds';
    var $paginate = array(

        'Kind'=> array(
            'limit' => 12,
            'order'=> array('Kind.order'=>'ASC')

        )

    );

}

期望的结果(视图);

 Kind.name ( numbers of links )
    links.name
    ........
    ..........

    Kind.name ( numbers of links )
    links.name
    ........
    ..........

我使用 kind.order 和 links.order 来控制数据在视图中的显示方式 来自管理区域。

我需要从按 kinds.order ASC 排序的种类表中检索数据和链接数,以及按 links.order 排序的链接数据

谢谢

【问题讨论】:

    标签: cakephp pagination


    【解决方案1】:

    您可以像这样在关系中定义顺序:

    class Kind extends AppModel{
        var $hasMany = array(
            'Link' => array(
                'className' => 'Link',
                'order' => 'Link.name'
            ),
        );
    }
    

    【讨论】:

    • 将 'contain' => array('Link') 添加到 paginate 属性并调用 $kinds = $this->paginate();
    • 谢谢 nik,现在订单正常。如何从链接表中获取每种链接的数量?我知道查询将是这样的:SELECT count(*) FROM links WHERE kind_id = id
    • 获取计数可以通过 counterCache 完成。检查这个:book.cakephp.org/view/1033/counterCache-Cache-your-count 或者,如果链接显示在表中,您始终可以使用 count($data['Kind'][$i]['Link']) 获得数字。只需检查视图中的输出
    • 您好,我需要特定类型的链接数,我认为使用 counterCache 我只能获得链接总数。 count($data['Kind'][$i]['Link']) 是一个很好的解决方案。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 2011-01-29
    相关资源
    最近更新 更多