This commit is contained in:
2021-03-04 15:34:49 +08:00
parent 356c4a53e5
commit f97c55eecc
2 changed files with 29 additions and 36 deletions
+2 -35
View File
@@ -296,48 +296,15 @@ class ActiveRecord extends BaseActiveRecord
private function runRelate(): array private function runRelate(): array
{ {
$relates = []; $relates = [];
if (empty($with = $this->getWith())) {
echo '--------------->' . PHP_EOL;
var_dump($this->_with, get_called_class());
echo '<---------------' . PHP_EOL;
if (empty($this->_with)) {
return $relates; return $relates;
} }
foreach ($this->_with as $val) { foreach ($with as $val) {
$relates[$val] = $this->resolveObject($val); $relates[$val] = $this->resolveObject($val);
} }
$this->_with = [];
return $relates; return $relates;
} }
private array $_with = [];
/**
* @param $data
* @return ActiveRecord
*/
public function setWith($data): static
{
echo '--------------->' . PHP_EOL;
var_dump($data, get_called_class());
echo '<---------------' . PHP_EOL;
if (empty($data)) {
return $this;
}
$this->_with = $data;
return $this;
}
/**
* @return array|null
*/
public function getWith(): array|null
{
return $this->_with;
}
/** /**
* @param string $modelName * @param string $modelName
+27 -1
View File
@@ -88,6 +88,32 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
protected ?Relation $_relation; protected ?Relation $_relation;
private array $_with = [];
/**
* @param $data
* @return ActiveRecord
*/
public function setWith($data): static
{
if (empty($data)) {
return $this;
}
$this->_with = $data;
return $this;
}
/**
* @return array|null
*/
public function getWith(): array|null
{
return $this->_with;
}
/** /**
* @param SEvent $event * @param SEvent $event
* 默认注入 * 默认注入
@@ -839,7 +865,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
if (array_key_exists($name, $this->_attributes)) { if (array_key_exists($name, $this->_attributes)) {
return static::getColumns()->_decode($name, $value); return static::getColumns()->_decode($name, $value);
} }
if (isset($this->_relate[$name])) { if (isset($this->_with[$name])) {
return $this->resolveClass($this->{$this->_relate[$name]}()); return $this->resolveClass($this->{$this->_relate[$name]}());
} }
return parent::__get($name); return parent::__get($name);