改进Query类的insert方法一处可能存在的警告错误 改进Model类一处Collection的use

This commit is contained in:
thinkphp
2017-02-08 18:06:12 +08:00
parent 22b4fff3dd
commit 13924b5a8b
2 changed files with 10 additions and 8 deletions

View File

@@ -14,7 +14,7 @@ namespace think;
use InvalidArgumentException; use InvalidArgumentException;
use think\db\Query; use think\db\Query;
use think\Exception\ValidateException; use think\Exception\ValidateException;
use think\model\Collection; use think\model\Collection as ModelCollection;
use think\model\Relation; use think\model\Relation;
use think\model\relation\BelongsTo; use think\model\relation\BelongsTo;
use think\model\relation\BelongsToMany; use think\model\relation\BelongsToMany;
@@ -624,7 +624,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/** /**
* 转换子模型对象 * 转换子模型对象
* @access protected * @access protected
* @param Model|Collection $model * @param Model|ModelCollection $model
* @param $visible * @param $visible
* @param $hidden * @param $hidden
* @param $key * @param $key
@@ -663,7 +663,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} }
foreach ($data as $key => $val) { foreach ($data as $key => $val) {
if ($val instanceof Model || $val instanceof Collection) { if ($val instanceof Model || $val instanceof ModelCollection) {
// 关联模型对象 // 关联模型对象
$item[$key] = $this->subToArray($val, $visible, $hidden, $key); $item[$key] = $this->subToArray($val, $visible, $hidden, $key);
} elseif (is_array($val) && reset($val) instanceof Model) { } elseif (is_array($val) && reset($val) instanceof Model) {
@@ -712,14 +712,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/** /**
* 转换当前模型数据集为数据集对象 * 转换当前模型数据集为数据集对象
* @access public * @access public
* @param array|Collection $collection 数据集 * @param array|\think\Collection $collection 数据集
* @return Collection * @return \think\Collection
*/ */
public function toCollection($collection) public function toCollection($collection)
{ {
if ($this->resultSetType) { if ($this->resultSetType) {
if ('collection' == $this->resultSetType) { if ('collection' == $this->resultSetType) {
$collection = new Collection($collection); $collection = new ModelCollection($collection);
} elseif (false !== strpos($this->resultSetType, '\\')) { } elseif (false !== strpos($this->resultSetType, '\\')) {
$class = $this->resultSetType; $class = $this->resultSetType;
$collection = new $class($collection); $collection = new $class($collection);

View File

@@ -2065,8 +2065,10 @@ class Query
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null); $sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
$lastInsId = $this->getLastInsID($sequence); $lastInsId = $this->getLastInsID($sequence);
if ($lastInsId) { if ($lastInsId) {
$pk = $this->getPk($options); $pk = $this->getPk($options);
$data[$pk] = $lastInsId; if (is_string($pk)) {
$data[$pk] = $lastInsId;
}
} }
$options['data'] = $data; $options['data'] = $data;
$this->trigger('after_insert', $options); $this->trigger('after_insert', $options);