diff --git a/module/productplan/model.php b/module/productplan/model.php index e036dfdadb..c38790ce62 100644 --- a/module/productplan/model.php +++ b/module/productplan/model.php @@ -804,13 +804,14 @@ class productplanModel extends model } /** + * 将父计划的parent改为-1, 没有子计划的父计划的parent改为0。 * Change parent field by planID. * * @param int $planID * @access public - * @return void + * @return bool */ - public function changeParentField($planID) + public function changeParentField(int $planID): bool { $plan = $this->getById($planID); if($plan->parent <= 0) return true; @@ -818,8 +819,7 @@ class productplanModel extends model $childCount = count($this->getChildren($plan->parent)); $parent = $childCount == 0 ? '0' : '-1'; - $parentPlan = $this->dao->select('*')->from(TABLE_PRODUCTPLAN)->where('id')->eq($plan->parent)->andWhere('deleted')->eq(0)->fetch(); - if($parentPlan) + if($childCount > 0) { $this->dao->update(TABLE_PRODUCTPLAN)->set('parent')->eq($parent)->where('id')->eq((int)$plan->parent)->exec(); } @@ -827,6 +827,8 @@ class productplanModel extends model { $this->dao->update(TABLE_PRODUCTPLAN)->set('parent')->eq('0')->where('id')->eq((int)$planID)->exec(); } + + return !dao::isError(); } /** diff --git a/module/productplan/test/model/changeparentfield.php b/module/productplan/test/model/changeparentfield.php index ab20fee67a..0dd0b6fd10 100755 --- a/module/productplan/test/model/changeparentfield.php +++ b/module/productplan/test/model/changeparentfield.php @@ -1,25 +1,21 @@ #!/usr/bin/env php changeParentField(); +timeout=0 cid=1 -pid=1 - -传入ID为1的情况,返回true >> 1 -传入ID为5的情况,返回true,如不存在函数会报错 >> 1 */ +include dirname(__FILE__, 5) . '/test/lib/init.php'; +include dirname(__FILE__, 2) . '/productplan.class.php'; -$plan = new productPlan('admin'); +zdTable('user')->gen(5); +zdTable('productplan')->config('productplan')->gen(5); -$planID = array(); -$planID[0] = 1; -$planID[1] = 5; +$planIdList = array(1, 2, 3); -r($plan->changeParentField($planID[0])) && p() && e('1'); //传入ID为1的情况,返回true -r($plan->changeParentField($planID[1])) && p() && e('1'); //传入ID为5的情况,返回true,如不存在函数会报错 -?> \ No newline at end of file +$planTester = new productplan('admin'); +r($planTester->changeParentFieldTest($planIdList[0])) && p('parent') && e('-1'); // 测试更新父计划的parent字段 +r($planTester->changeParentFieldTest($planIdList[1])) && p('parent') && e('1'); // 测试更新子计划的parent字段 +r($planTester->changeParentFieldTest($planIdList[2])) && p('parent') && e('0'); // 测试更新普通计划的parent字段 diff --git a/module/productplan/test/productplan.class.php b/module/productplan/test/productplan.class.php index f994d138b2..03385e76c6 100644 --- a/module/productplan/test/productplan.class.php +++ b/module/productplan/test/productplan.class.php @@ -208,17 +208,19 @@ class productPlan } /** - * Change parent field + * 将父计划的parent改为-1, 没有子计划的父计划的parent改为0。 + * Change parent field by planID. * - * @param int $planID + * @param int $planID * @access public - * @return true + * @return array|object */ - public function changeParentField($planID) + public function changeParentFieldTest(int $planID): array|object { - $productplans = $this->productplan->changeParentField($planID); + $this->productplan->changeParentField($planID); + if(dao::isError()) return dao::getError(); - return $productplans; + return $this->productplan->getByID($planID); } /**