在CakePHP的find('all')方法中使用'id'设置索引

Ger*_*obs 6 php cakephp multidimensional-array

我有一个包含数据的数组,所有数据都有自己唯一的ID.我正在使用ORM方法find('all'),结果数组看起来有点像这样:

Array
(
    [0] => Array
        (
            [Wijken] => Array
                (
                    [id] => 1
                    [name] => Naam
                    [lat] => 13.37
                    [lon] => 13.37
                    [zoom] => 14
                )

        )

)
Run Code Online (Sandbox Code Playgroud)

从我的路由我收到一个唯一的ID ..我想要的是,重新使用我的数组并从ID 1获取数据.

所以我需要的是我的关联数组的索引(由find('')返回)被设置为"Wijken" - 对象本身的id.

我解释了一切,以防人们有不同的方法.但是,使用param ID再次查询数据库是不可接受的.

小智 15

尝试Set :: combine

维护find('all')结构(来自icc97注释):

$idsAsIndexes = Set::combine($wijkens, '{n}.Wijken.id', '{n}');
Run Code Online (Sandbox Code Playgroud)

或者,您也可以提取单个模型:

$idsAsIndexes = Set::combine($wijkens, '{n}.Wijken.id', '{n}.Wijken');
Run Code Online (Sandbox Code Playgroud)

希望这就是你要找的:)

  • 如果你想保持与find('all')相同的数组结构,即包括'Wijken'键,你需要`$ wijkensWithIdsAsIndexes = Set :: combine($ wijkens,'{n} .Wijken.id','{ N}');` (6认同)