dow5001 2013-05-18 11:09
浏览 37
已采纳

在cakephp中使用自定义查询时出错?

I have using cakephp for my website. And i use sql server 2012 with it. I have confused that when i use:

$this->set('types',$this->Manager->query('select * from product_types'));

to get the array of all my product types the return array is:

Array
(

    [0] => Array
    (
        [0] => Array
        (
            [id] => 1
            [name] => hoa my pham
        )
    )

    [1] => Array
    (
        [0] => Array
        (
            [id] => 2
            [name] => hoa my
        )

    )

)

Why has the [0] instead of [product_types]????

  • 写回答

2条回答 默认 最新

  • dongzuan4917 2013-05-18 11:15
    关注

    This is from the CakePHP documentation:

    To use the model name as the array key, and get a result consistent with that returned by the Find methods, the query can be rewritten:
    
    $this->Picture->query("SELECT * FROM pictures AS Picture LIMIT 2;");
    
    which returns:
    
    Array
    (
        [0] => Array
        (
            [Picture] => Array
            (
                [id] => 1304
                [user_id] => 759
            )
        )
    
        [1] => Array
        (
            [Picture] => Array
            (
                [id] => 1305
                [user_id] => 759
            )
        )
    )
    

    So you need:

    $this->set('types',$this->Manager->query('select * from product_types as Product_Types'));
    

    Source: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

    P.S.:

    This syntax and the corresponding array structure is valid for MySQL only. Cake does not provide any data abstraction when running queries manually, so exact results will vary between databases.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度