hasMany('SpecItem','spec_id','id')->order('order_index asc'); } public function delete() { $id = $this->getAttr('id'); $spec_items = db('spec_item')->where('spec_id', $id)->select(); if($spec_items){ $spec_item_ids = []; foreach($spec_items as $spec_item){ array_push($spec_item_ids, $spec_item['id']); $spec_goods_price = db('spec_goods_price')->whereOr('key', $spec_item['id']) ->whereOr('key', 'LIKE', '%\_' . $spec_item['id'])->whereOr('key', 'LIKE', $spec_item['id'] . '\_%')->find(); if ($spec_goods_price) { $goods_name = db('goods')->where('goods_id', $spec_goods_price['goods_id'])->value('goods_name'); throw new TpshopException('删除规格项', 0, ['status' => 0, 'msg' => $goods_name . '在使用该规格项,不能删除']); } } db('spec_item')->where('id', 'in', $spec_item_ids)->delete(); } return parent::delete(); // TODO: Change the autogenerated stub } }