[英]Cakephp: can Model->find('all') return results without model name
Using Model->find('all') returns an array with the following structure: 使用Model-> find('all')返回具有以下结构的数组:
array(
0 => array('Model1' => array(/* Model1 fields*/), 'Model2' => array(/* Model2 fields*/), ...),
1 => array('Model1' => array(/* Model1 fields*/), 'Model2' => array(/* Model2 fields*/), ...),
...)
When a single Model is queried (ie Recursive = -1), is it possible to have the results returned as an array with the following structure: 当查询单个模型时(即递归= -1),是否可以将结果作为具有以下结构的数组返回:
array(0 => /* Model1 fields*/, 1 => /* Model1 fields*/, etc...)
I thought I read this somewhere a while back but cannot figure out how to do this or if it is possible. 我以为我在一段时间之前读过这篇文章,但无法弄清楚该怎么做或是否有可能。
What about this ? 那这个呢 ? works in php 5.4+
可以在php 5.4+
$result = array_map( function ($elem) {
return $elem['YourModelName'] ;
} ,$this->Tagcloud-> find('all')
);
For older php versions you need to iterate after fetching the results 对于较旧的php版本,您需要在获取结果后进行迭代
Maybe you are thinking about related models that might get returned this way? 也许您正在考虑可能以这种方式返回的相关模型? AFAIK Cake query results are pretty standardized, and that's a good thing.
AFAIK Cake查询结果非常标准化,这是一件好事。
array(
0 => array(
'Model' => array(
'id',
'field1',
...
),
'belongsTo/hasOneModel' => array(
'id',
'field1',
...
)
'habtm/hasManyModel' => array(
0 => array(
'id',
'field1',
...
),
1 => array(
...
)
)
),
1 => array(
'Model' => array(
...
),
...
)
)
As you can see, the related HABTM or hasMany models are returned in a "flat" array, but the primary model should always contain the model name. 如您所见,相关的HABTM或hasMany模型以“平面”数组返回,但是主要模型应始终包含模型名称。
You could also modify that model's aftersave method so that it returns $data['Modelname'] after it performs the query... In essence, it'd basically be an array shift and you would only have $arrayname['fieldname'] rather than $arrayname['Model']['fieldname']. 您还可以修改该模型的aftersave方法,以便在执行查询后返回$ data ['Modelname'] ...本质上,它基本上是一个数组移位,并且您只有$ arrayname ['fieldname']而不是$ arrayname ['Model'] ['fieldname']。 Is that what you were asking?
那是你在问什么吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.