ActiveQuery.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: NODELOG
  5. * DateTime: 2017/8/4 14:36
  6. * Description:
  7. */
  8. namespace common\components;
  9. use yii\db\Connection;
  10. use yii\db\Query;
  11. class ActiveQuery extends \yii\db\ActiveQuery
  12. {
  13. /**
  14. * Executes query and returns a single row of result.
  15. * @param Connection|null $db the DB connection used to create the DB command.
  16. * If `null`, the DB connection returned by [[modelClass]] will be used.
  17. * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
  18. * the query result may be either an array or an ActiveRecord object. `null` will be returned
  19. * if the query results in nothing.
  20. * @throws ModelNotFountException
  21. */
  22. public function oneOrFail($db = null)
  23. {
  24. $row = Query::one($db);
  25. if ($row !== false) {
  26. $models = $this->populate([$row]);
  27. if (reset($models)) {
  28. return reset($models);
  29. }
  30. }
  31. throw new ModelNotFountException();
  32. }
  33. }