0

我在我的网站上使用了 cakephp。我使用 sql server 2012。当我使用时,我很困惑:

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

要获取我所有产品类型的数组,返回数组是:

Array
(

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

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

    )

)

为什么有 [0] 而不是 [product_types]????

4

2 回答 2

1

请遵循可用的文档和教程。

然后你就可以使用

$this->set('managers', $this->Manager->find('all'));

对于同样的事情 - 使用带有包装函数和 sql server 数据源的干净方法。

对于 SqlServer,应该有一个可用的数据源,例如: https ://github.com/cakephp/datasources/blob/2.0/Model/Datasource/Database/Sqlsrv.php

于 2013-05-18T11:40:11.790 回答
0

这是来自 CakePHP 文档:

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
        )
    )
)

所以你需要:

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

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

PS:

此语法和相应的数组结构仅对 MySQL 有效。手动运行查询时,Cake 不提供任何数据抽象,因此确切的结果会因数据库而异。

于 2013-05-18T11:15:10.260 回答