BaseModel.php 984 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace app\api\model;
  3. use think\Model;
  4. class BaseModel extends Model
  5. {
  6. /**
  7. * @param $id
  8. * @param array $extend
  9. * @return array|bool|false|\PDOStatement|string|Model|null
  10. * @throws \think\db\exception\DataNotFoundException
  11. * @throws \think\db\exception\ModelNotFoundException
  12. * @throws \think\exception\DbException
  13. */
  14. public function findById($id, $extend = [])
  15. {
  16. $query = $this->where("id", $id);
  17. $keys = array_keys($extend);
  18. foreach ($keys as $key) {
  19. $query->where("$key", "=", $extend[$key]);
  20. }
  21. return $query->find();
  22. }
  23. public function findByIds(array $ids, $extend = [])
  24. {
  25. if (0 === count($ids))
  26. return [];
  27. $query = $this->where("id", "in", $ids);
  28. $keys = array_keys($extend);
  29. foreach ($keys as $key) {
  30. $query->where("$key", "=", $extend[$key]);
  31. }
  32. return $query->select();
  33. }
  34. }