* Refactor changeParentField method and case.

This commit is contained in:
tianshujie
2023-09-26 17:03:27 +08:00
parent 61b9b813cc
commit 5b0fb40b49
3 changed files with 24 additions and 24 deletions
+6 -4
View File
@@ -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();
}
/**
@@ -1,25 +1,21 @@
#!/usr/bin/env php
<?php
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(Dirname(dirname(__FILE__))) . '/class/productplan.class.php';
/**
title=productplanModel->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,如不存在函数会报错
?>
$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字段
@@ -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);
}
/**