简体   繁体   English

Cakephp:Model-> find('all')是否可以返回没有模型名称的结果

[英]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.

相关问题 CakePHP:模型->模型-> find() - CakePHP: Model->Model->find() 我如何在不使用`$ this-> Model-> query()`的情况下在CakePHP中编写复杂的查找查询? - How can I write a complex find query in CakePHP without using `$this->Model->query()`? 如何在Laravel 4中了解Model-> all()或Model-> get()的返回值? - How can I hook into the return values of either Model->all() or Model->get() in Laravel 4? CakePHP模型->查找似乎忽略了条件 - CakePHP model->find seems to be ignoring the conditions CakePHP模型->在字段中查找和IF条件 - CakePHP model->find and IF condition in field CakePHP:$ this-> Model-> contain(…)更改不同模型的搜索结果 - CakePHP: $this->Model->contain(…) changes search results for DIFFERENT model 使用finderQuery的CakePHP 2.3模型关联可用于Model-> read(),但不适用于Model-> find() - CakePHP 2.3 model association using finderQuery works with Model->read() but not with Model->find() CakePHP:$ this-> model-> find('all')崩溃所有页面。 怎么回事? - CakePHP: $this->model->find('all') crash all pages. Any idea of what happened? CakePHP中model-> alias和model-> name之间的区别是什么? - what's the difference between model->alias and model->name in CakePHP? CakePHP 2:在$ this-> Model-> find语句中使用存储的数组作为键 - CakePHP 2: Using stored array as keys in a $this->Model->find statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM