Commit c5022203 by Carsten Brandt

fixed issue with null value in CRUD controller findModel()

parent 74a74c84
...@@ -5,6 +5,7 @@ Yii Framework 2 gii extension Change Log ...@@ -5,6 +5,7 @@ Yii Framework 2 gii extension Change Log
---------------------------- ----------------------------
- Bug #1405: fixed disambiguation of relation names generated by gii (qiangxue) - Bug #1405: fixed disambiguation of relation names generated by gii (qiangxue)
- Bug: fixed controller in crud template to avoid returning query in findModel() (cebe)
- Enh #1624: generate rules for unique indexes (lucianobaraglia) - Enh #1624: generate rules for unique indexes (lucianobaraglia)
- Enh #1818: Do not display checkbox column if all rows are empty (johonunu) - Enh #1818: Do not display checkbox column if all rows are empty (johonunu)
......
...@@ -140,15 +140,18 @@ class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->bas ...@@ -140,15 +140,18 @@ class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->bas
<?php <?php
if (count($pks) === 1) { if (count($pks) === 1) {
$condition = '$id'; $condition = '$id';
// find() would return Query when $id === null
$nullCheck = '$id !== null && ';
} else { } else {
$condition = []; $condition = [];
foreach ($pks as $pk) { foreach ($pks as $pk) {
$condition[] = "'$pk' => \$$pk"; $condition[] = "'$pk' => \$$pk";
} }
$condition = '[' . implode(', ', $condition) . ']'; $condition = '[' . implode(', ', $condition) . ']';
$nullCheck = '';
} }
?> ?>
if (($model = <?= $modelClass ?>::find(<?= $condition ?>)) !== null) { if (<?= $nullCheck ?>($model = <?= $modelClass ?>::find(<?= $condition ?>)) !== null) {
return $model; return $model;
} else { } else {
throw new NotFoundHttpException('The requested page does not exist.'); throw new NotFoundHttpException('The requested page does not exist.');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment