* Adjust branch create model.
This commit is contained in:
@@ -8,8 +8,12 @@ $config->branch->form->batchedit['name'] = array('type' => 'string', 'requir
|
||||
$config->branch->form->batchedit['desc'] = array('type' => 'string', 'required' => false, 'default' => '');
|
||||
$config->branch->form->batchedit['status'] = array('type' => 'string', 'required' => false, 'default' => 'active');
|
||||
|
||||
$config->branch->form->create['name'] = array('type' => 'string', 'required' => true, 'default' => '');
|
||||
$config->branch->form->create['desc'] = array('type' => 'string', 'required' => false, 'default' => '');
|
||||
$config->branch->form->create['status'] = array('type' => 'string', 'required' => false, 'default' => 'active');
|
||||
$config->branch->form->create['createdDate'] = array('type' => 'string', 'required' => false, 'default' => helper::today());
|
||||
|
||||
$config->branch->form->mergebranch = $config->branch->form->create;
|
||||
$config->branch->form->mergebranch['createBranch'] = array('type' => 'int', 'required' => false, 'default' => 0);
|
||||
$config->branch->form->mergebranch['targetBranch'] = array('type' => 'int', 'required' => false, 'default' => 0);
|
||||
$config->branch->form->mergebranch['mergedBranchIDList'] = array('type' => 'string', 'required' => false, 'default' => '');
|
||||
$config->branch->form->mergebranch['name'] = array('type' => 'string', 'required' => false, 'default' => '');
|
||||
$config->branch->form->mergebranch['desc'] = array('type' => 'string', 'required' => false, 'default' => '');
|
||||
|
||||
@@ -56,17 +56,21 @@ class branch extends control
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个分支。
|
||||
* Create a branch.
|
||||
*
|
||||
* @param int $productID
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function create($productID)
|
||||
public function create(int $productID)
|
||||
{
|
||||
$this->branch->changeBranchLanguage($productID);
|
||||
|
||||
if($_POST)
|
||||
{
|
||||
$branchID = $this->branch->create($productID);
|
||||
$branch = form::data()->get();
|
||||
$branchID = $this->branch->create($productID, $branch);
|
||||
if(dao::isError()) return $this->sendError(dao::getError());
|
||||
|
||||
$this->loadModel('action')->create('branch', $branchID, 'Opened');
|
||||
|
||||
+9
-16
@@ -205,34 +205,27 @@ class branchModel extends model
|
||||
/**
|
||||
* Create a branch.
|
||||
*
|
||||
* @param int $productID
|
||||
* @param bool $withMerge
|
||||
* @param int $productID
|
||||
* @param object $branch
|
||||
* @access public
|
||||
* @return int|bool
|
||||
* @return int|false
|
||||
*/
|
||||
public function create($productID, $withMerge = false)
|
||||
public function create(int $productID, object $branch): int|false
|
||||
{
|
||||
$branch = fixer::input('post')
|
||||
->add('product', $productID)
|
||||
->add('createdDate', helper::today())
|
||||
->add('status', 'active')
|
||||
->removeIF($withMerge, 'createBranch,mergedBranchIDList,targetBranch')
|
||||
->get();
|
||||
|
||||
$lastOrder = (int)$this->dao->select('`order`')->from(TABLE_BRANCH)->where('product')->eq($productID)->orderBy('order_desc')->limit(1)->fetch('order');
|
||||
$branch->order = empty($lastOrder) ? 1 : $lastOrder + 1;
|
||||
|
||||
$this->app->loadLang('product');
|
||||
$productType = $this->dao->select('`type`')->from(TABLE_PRODUCT)->where('id')->eq($productID)->fetch('type');
|
||||
$this->lang->error->unique = str_replace('@branch@', $this->lang->product->branchName[$productType], $this->lang->branch->existName);
|
||||
|
||||
$lastOrder = (int)$this->dao->select('`order`')->from(TABLE_BRANCH)->where('product')->eq($productID)->orderBy('order_desc')->limit(1)->fetch('order');
|
||||
$branch->order = $lastOrder + 1;
|
||||
$branch->product = $productID;
|
||||
$this->dao->insert(TABLE_BRANCH)->data($branch)
|
||||
->batchCheck($this->config->branch->create->requiredFields, 'notempty')
|
||||
->checkIF(!empty($branch->name), 'name', 'unique', "product = $productID")
|
||||
->exec();
|
||||
|
||||
if(!dao::isError()) return $this->dao->lastInsertID();
|
||||
return false;
|
||||
if(dao::isError()) return false;
|
||||
return $this->dao->lastInsertID();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -93,13 +93,11 @@ class branchTest
|
||||
*/
|
||||
public function createTest($productID, $param = array())
|
||||
{
|
||||
foreach($param as $key => $value) $_POST[$key] = $value;
|
||||
|
||||
$objectID = $this->objectModel->create($productID, $withMerge = false);
|
||||
|
||||
unset($_POST);
|
||||
|
||||
if(dao::isError()) return dao::getError()['name'][0];
|
||||
$objectID = $this->objectModel->create($productID, (object) $param);
|
||||
if(dao::isError())
|
||||
{
|
||||
return dao::getError();
|
||||
}
|
||||
|
||||
global $tester;
|
||||
$object = $tester->dao->select('*')->from(TABLE_BRANCH)->where('id')->eq($objectID)->fetch();
|
||||
|
||||
@@ -2,22 +2,19 @@
|
||||
<?php
|
||||
include dirname(__FILE__, 5) . '/test/lib/init.php';
|
||||
include dirname(__FILE__, 2) . '/branch.class.php';
|
||||
|
||||
zdTable('product')->config('product')->gen(10);
|
||||
zdTable('branch')->gen(0);
|
||||
su('admin');
|
||||
|
||||
/**
|
||||
|
||||
title=测试 branchModel->create();
|
||||
timeout=0
|
||||
cid=1
|
||||
pid=1
|
||||
|
||||
测试新建分支1 >> 新建分支1,新建分支1的描述
|
||||
测试新建分支2 >> 新建分支2,新建分支2的描述
|
||||
测试新建 重名新建分支1 >> 分支名称已存在
|
||||
测试新建 重名分支1 >> 分支名称已存在
|
||||
测试新建 名称为空的分支 >> 『名称』不能为空。
|
||||
|
||||
*/
|
||||
$productID = 41;
|
||||
$productID = 6;
|
||||
|
||||
$branch1 = array('name' => '新建分支1', 'desc' => '新建分支1的描述');
|
||||
$branch2 = array('name' => '新建分支2', 'desc' => '新建分支2的描述');
|
||||
@@ -30,6 +27,6 @@ $branch = new branchTest();
|
||||
|
||||
r($branch->createTest($productID, $branch1)) && p('name,desc') && e('新建分支1,新建分支1的描述'); // 测试新建分支1
|
||||
r($branch->createTest($productID, $branch2)) && p('name,desc') && e('新建分支2,新建分支2的描述'); // 测试新建分支2
|
||||
r($branch->createTest($productID, $repeatName1)) && p() && e('分支名称已存在'); // 测试新建 重名新建分支1
|
||||
r($branch->createTest($productID, $repeatName2)) && p() && e('分支名称已存在'); // 测试新建 重名分支1
|
||||
r($branch->createTest($productID, $emptyName)) && p() && e('『名称』不能为空。'); // 测试新建 名称为空的分支
|
||||
r($branch->createTest($productID, $repeatName1)) && p('name:0') && e('分支名称已存在'); // 测试新建 重名新建分支1
|
||||
r($branch->createTest($productID, $repeatName2)) && p('name') && e('分支1'); // 测试新建 分支1
|
||||
r($branch->createTest($productID, $emptyName)) && p('name:0') && e('『名称』不能为空。'); // 测试新建 名称为空的分支
|
||||
|
||||
Reference in New Issue
Block a user