|
@@ -5,6 +5,7 @@ namespace Qii\Driver\Entity;
|
|
|
|
|
|
use Qii\Driver\Model;
|
|
|
use Qii\Driver\Response;
|
|
|
+use Qii\Exceptions\InvalidParams;
|
|
|
|
|
|
class Base {
|
|
|
/**
|
|
@@ -1017,10 +1018,44 @@ class Base {
|
|
|
}
|
|
|
return (bool) $this->db()->where($where)->selectOne($this->prepareTable());
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * replace into database
|
|
|
+ *
|
|
|
+ * @return mixed|Response
|
|
|
+ * @throws InvalidParams
|
|
|
+ */
|
|
|
+ final public function replace(){
|
|
|
+ $valid = $this->validFieldsForAdd();
|
|
|
+ if($valid->isError()) {
|
|
|
+ return $valid;
|
|
|
+ }
|
|
|
+ //如果设置了 unique 就先验证唯一性
|
|
|
+ list($uniqueWhere, $uniqueOr, $exclude, $primary) = $this->condition();
|
|
|
+ unset($uniqueWhere, $uniqueOr, $exclude, $exclude);
|
|
|
+ if(count($primary) == 0) {
|
|
|
+ throw new InvalidParams('DB::replace需要设置主键');
|
|
|
+ }
|
|
|
+ $values = array_merge($this->getDefaultValue(), $this->properties());
|
|
|
+ $this->lastInsertID = $res = $this->db()->replaceObject($this->prepareTable(), $values);
|
|
|
+
|
|
|
+ if($this->db()->isError()) {
|
|
|
+ return Response::FailSave(static::class .'::'. __FUNCTION__,
|
|
|
+ [
|
|
|
+ '_result' => ['code' => Response::FAIL_FOR_SAVE, 'msg' => $this->db()->getMessage(), 'body' => []],
|
|
|
+ 'message' => $this->db()->getMessage()
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ return Response::Success(static::class .'::'. __FUNCTION__,
|
|
|
+ [
|
|
|
+ '_result' => ['code' => Response::DO_SUCCESS, 'msg' => '添加成功', 'body' => $res]
|
|
|
+ ]
|
|
|
+ );
|
|
|
+ }
|
|
|
/**
|
|
|
* 保存数据
|
|
|
*
|
|
|
- * @return mixed|Qii\Driver\Response
|
|
|
+ * @return mixed|Response
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
final public function add() {
|
|
@@ -1747,28 +1782,34 @@ class Base {
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
protected function uniqueCondition() {
|
|
|
- $uniqueWhere = [];
|
|
|
- $uniqueOr = [];
|
|
|
$unique = $this->uniqueKey();
|
|
|
- $useWhere = true;
|
|
|
-
|
|
|
if(!is_array($unique)) {
|
|
|
- $unique = $this->explode(',', $unique);
|
|
|
- $useWhere = false;
|
|
|
- if(count($unique) == 1) {
|
|
|
- $useWhere = true;
|
|
|
+ if(strpos($unique, ',') !== false) {
|
|
|
+ $unique = explode(',', $unique);
|
|
|
+ }else{
|
|
|
+ $unique = array($unique);
|
|
|
}
|
|
|
}
|
|
|
- foreach ($unique as $key) {
|
|
|
- $key = $this->entity()->convertToField($key);
|
|
|
- $property = $this->entity()->convertToProperty($key);
|
|
|
- if($useWhere) {
|
|
|
- $uniqueWhere[$key] = $this->$property;
|
|
|
+ $where = ['and' => [], 'or' => []];
|
|
|
+ $a1 = array_reduce($unique, function ($where, $item){
|
|
|
+ if(strpos($item, ',') !== false) {
|
|
|
+ $item = explode(',', $item);
|
|
|
+ foreach($item as $k){
|
|
|
+ $key = $this->entity()->convertToField($k);
|
|
|
+ $property = $this->entity()->convertToProperty($k);
|
|
|
+ $where['and'][$key] = $this->$property;
|
|
|
+ }
|
|
|
}else{
|
|
|
- $uniqueOr[$key] = $this->$property;
|
|
|
+ $key = $this->entity()->convertToField($item);
|
|
|
+ $property = $this->entity()->convertToProperty($item);
|
|
|
+ $where['or'][$key] = $this->$property;
|
|
|
+ $where['or'][] = 'or';
|
|
|
}
|
|
|
- }
|
|
|
- return [$uniqueWhere, $uniqueOr];
|
|
|
+ return $where;
|
|
|
+ }, $where);
|
|
|
+ //弹出or的最后一个or操作符
|
|
|
+ array_pop($a1['or']);
|
|
|
+ return [$a1['and'], $a1['or']];
|
|
|
}
|
|
|
|
|
|
/**
|