私のphpプロジェクトでIlluminateを使用していて、最近PHP 5からPHP 7.2にアップグレードしました。
モデルにこのスコープがあります
Model.php
public function scopeWhereInOrAny(Builder $query, $field, $value)
{
if ($value === null || $value === '' || count($value) === 0) {
return $query;
}
return $query->whereIn($field, $value);
}
whereIn を使用するときに null を受け入れる非常に単純なスコープ。今、私は関数でそれを使用します
public function getList($params = []) {
$cancelReasons = CancellationReasons
::orderBy('id', 'asc')
->whereInOrAny('type_booking', $params['type_booking']) // HERE
->where('status', '=', 1)
->get(['id','name','type_client']);
return $cancelReasons;
そして、そのメソッドを呼び出すと、ちょっとうまくいき、返すべきものを返しますが、このように最後に警告を追加します
[{"id":1,"name":"He cancelado mi viaje","type_client":1},{"id":2,"name":"Quiero hacer otra actividad","type_client":1},{"id":3,"name":"Quiero modificar la reserva","type_client":1},{"id":4,"name":"He encontrado un precio m\u00e1s barato en otra web","type_client":1},{"id":5,"name":"El proveedor me ha pedido que cancele","type_client":1},{"id":6,"name":"Otros motivos","type_client":1},{"id":7,"name":"Condiciones climatol\u00f3gicas","type_client":2},{"id":8,"name":"Problemas en el destino","type_client":2},{"id":9,"name":"No hay un n\u00famero m\u00ednimo de personas","type_client":2},{"id":10,"name":"Falta de disponibilidad","type_client":2},{"id":11,"name":"Restricciones Covid","type_client":2},{"id":12,"name":"Cancelaci\u00f3n solicitada por el cliente","type_client":2},{"id":13,"name":"Otros motivos","type_client":2},{"id":14,"name":"Cese de la colaboraci\u00f3n con el proveedor","type_client":3},{"id":15,"name":"El proveedor no realiz\u00f3 la actividad","type_client":3},{"id":16,"name":"Otros motivos","type_client":3}]<br />
<b>Warning</b>: count(): Parameter must be an array or an object that implements Countable in <b>/var/www/html/newadmin/vendor7/illuminate/database/Eloquent/Builder.php</b> on line <b>1015</b><br />
デバッグ中、問題はここ Builder.php にあることがわかりました
/**
* Apply the given scope on the current builder instance.
*
* @param callable $scope
* @param array $parameters
* @return mixed
*/
protected function callScope(callable $scope, $parameters = [])
{
array_unshift($parameters, $this);
$query = $this->getQuery();
// We will keep track of how many wheres are on the query before running the
// scope so that we can properly group the added scope constraints in the
// query as their own isolated nested where statement and avoid issues.
$originalWhereCount = count($query->wheres); // HERE IS THE PROBLEM
$result = $scope(...array_values($parameters)) ?: $this;
if (count($query->wheres) > $originalWhereCount) {
$this->addNewWheresWithinGroup($query, $originalWhereCount);
}
return $result;
}
どうやら $query->wheres is equals null この場合、php 7.2 では count on null を使用すると警告がスローされます。なぜ $query->wheres が null に等しいのですか? カスタムスコープの前に他の場所がないため、場所の順序を変更すると
public function getList($params = []) {
$cancelReasons = CancellationReasons
::orderBy('id', 'asc')
->where('status', '=', 1) //This where before the scope
->whereInOrAny('type_booking', $params['type_booking'])
->get(['id','name','type_client']);
return $cancelReasons;
}
それは完全に機能します!しかし、これが長期的に問題を本当に解決するとは思いません。確かにこの特定のケースを解決しますが、スコープを使用するたびに、最初にランダムな場所を追加する必要がありますか? 必要ないのに?それは正しくないようです。そのため、クエリビルダーでスコープを使用している他の部分で機能する、この問題に対するよりクリーンなソリューションを探しています。
誰かが私を助けてくれることを願っています