CakePHP 2.x:Model::afterFind() 上的 $primary 标志真的有用吗? [英] CakePHP 2.x: Is the $primary flag on Model::afterFind() actually useful?
问题描述
CakePHP 的 Model::afterFind()
回调看起来像:
CakePHP's Model::afterFind()
callback looks like:
afterFind(array $results, boolean $primary = false)
根据文档:
$primary
参数指示当前模型是否是查询所基于的模型,或者该模型是否作为关联被查询.如果将模型作为关联进行查询,$results
的格式可能会有所不同.
The
$primary
parameter indicates whether or not the current model was the model that the query originated on or whether or not this model was queried as an association. If a model is queried as an association the format of$results
can differ.
它们可以不同,但实验表明它们并不总是不同.据我所知,$primary
参数实际上并不是那么有用.如果将其设置为 false
,您可能会也可能不会获得扁平化的数据结构,因此您可能会或可能不会收到可怕的无法将字符串偏移用作数组"错误消息.
They can differ, but experimentation shows that they don't always differ. As far as I can tell, the $primary
parameter isn't actually all that useful. If it's set to false
you may or may not get a flattened data structure, so you may or may not wind up with the dreaded "cannot use string offset as an array" error message.
虽然我还没有尝试过,但我基于文档的想法是完全忽略 $primary
标志,只检查数据:
Although I haven't tried it yet, my thought based on the documentation was to ignore the $primary
flag altogether and just check the data:
public function afterFind($results, $primary = false) {
if (array_key_exists(0, $results) {
// operate on $results[0]['User']['fieldname']
} else {
// operate on $results['fieldname']
}
return $results;
}
这很黑,我不喜欢它,但它似乎比 $primary
更有用.
This is hackish and I don't like it, but it seems likely to be more useful than $primary
.
明确地说,我的问题是:
Explicitly stated, my questions are:
$primary
标志的实际用途是什么?- 它对确定
$results
数组的结构没有有用,还是我错过了那里的某些东西,我是否正确?
- What is the
$primary
flag actually useful for? - Am I correct that it is not useful for determining the structure of the
$results
array, or have I missed something there?
推荐答案
确实,$primary
参数似乎只在警告您 $results
格式的情况下有用代码> 是不可预测的.它在确定 $results
的格式时没有用.
Indeed the $primary
parameter seems to only be useful in warning you of cases where the format of $results
is unpredictable. It is not useful in determining the format of $results
.
此处的更多信息:https://groups.google.com/forum/?fromgroups=#!topic/cake-php/Mqufi67UoFo
那里提供的解决方案是检查 !isset($results[$this->primaryKey])
以查看 $results
的格式.这也有点小技巧,但可以说比检查键0"要好.
The solution offered there is to check !isset($results[$this->primaryKey])
to see what format $results
is. This is also a bit of a hack, but arguably better than checking for a key '0'.
我最终想出的解决方案是做这样的事情:
The solution I ultimately came up with is to do something like this:
public function afterFind($results, $useless) {
// check for the primaryKey field
if(!isset($results[$this->primaryKey])) {
// standard format, use the array directly
$resultsArray =& $results;
} else {
// stupid format, create a dummy array
$resultsArray = array(array());
// and push a reference to the single value into our array
$resultsArray[0][$this->alias] =& $results;
}
// iterate through $resultsArray
foreach($resultsArray as &$result) {
// operate on $result[$this->alias]['fieldname']
// one piece of code for both cases. yay!
}
// return $results in whichever format it came in
// as but with the values modified by reference
return parent::afterFind($results, $useless);
}
这减少了代码重复,因为您不必两次编写字段更改逻辑(一次用于数组,一次用于非数组).
This reduces code duplication because you don't have to write your field alteration logic twice (once for an array and once for non-array).
您可以通过在方法结束时返回 $resultsArray
来完全避免引用内容,但我不确定如果 CakePHP(或其他一些父class) 期望 $results
以传入的方式传入.此外,这种方式没有复制 $results
数组的开销.
You may be able to avoid the references stuff altogether by just returning $resultsArray
at the end of the method, but I wasn't sure what issues that might cause if CakePHP (or some other parent class) expects $results
in the way it was passed in. Plus this way doesn't have the overhead of copying the $results
array.
这篇关于CakePHP 2.x:Model::afterFind() 上的 $primary 标志真的有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!