// +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Date: 2017-5-25 // +---------------------------------------------------------------------- namespace huo\model\slide; use huo\model\common\CommonModel; use think\exception\PDOException; class SlideModel extends CommonModel { /** * 全局查询范围status=1显示状态 * * @param $query */ protected function base($query) { $query->where('status', 2)->where('delete_time', 0); } /** * SlideItemModel 一对多关联模型 关联分类下的幻灯片 * */ protected function items() { return $this->hasMany(SlideItemModel::className(), 'slide_id', 'id') ->order('list_order ASC'); } /** * SlideList 幻灯片获取 * * @param array $map * * @param string $page * * @return false|\PDOStatement|string|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getSlideList($map, $page = '1,10') { $_page = $page; if (empty($page)) { $_page = '1,10'; } $data = $this->relation('items')->field(true)->where($map)->page($_page)->select(); return $data; } public function getSlideIdsByDevice($device) { if (empty($device)) { return false; } return $this->where(['device' => $device])->column(['id']); } public function getSlideCodesByDevice($device) { if (empty($device)) { return false; } return $this->where(['device' => $device])->column('code'); } public function getSlideIdByCode($code) { if (empty($code)) { return false; } return $this->where(['code' => $code])->value('id'); } public function getCodeById($id = 0) { if (empty($id)) { return 0; } return $this->where(['id' => $id])->limit('1')->value('code'); } public function getTypeIdById($id = 0) { if (empty($id)) { return 0; } return $this->where(['id' => $id])->limit('1')->value('type_id'); } /** * 添加数据 * * @param $data * * @return bool */ public function addData($data) { if (empty($data)) { return false; } $_data = $data; $_obj = self::create($_data, true); if ($_obj) { return true; } return false; } /** * 删除数据 * * @param array $_map 删除条件 * * @return int */ public function deleteDataByMap($_map) { $_id = self::where($_map)->column('id'); self::startTrans(); try { self::where($_map)->delete(); (new SlideItemModel())->where(['slide_id' => ['in', $_id]])->delete(); // 提交事务 self::commit(); } catch (\Exception $e) { // 回滚事务 self::rollback(); return false; } return true; } }