/** * Это магический метод __get, доработанный под данную модель. При запросе * свойства author проверяет, не пустое ли у объекта свойство $author_id, и * если не пустое, то возвращает объект класса Author * * @param $k string Имя свойства * @return null Если имя свойства не равно 'author' * @return object Authors, если имя свойства равно author */ public function __get($k) { switch ($k) { case 'author': return Author::findById($this->author_id); break; default: return null; } }
/** * Геттер * Обработка обращения к author * @return object| FALSE | NULL Возвращает объект Author, если есть author_id * FALSE Если author_id не установлен * NULL Если запрошено свойство которого, нет */ public function __get($k) { if ($k == 'author') { if (isset($this->author_id)) { return \App\Models\Author::findById($this->author_id); } else { return false; } } return NULL; }
/** * @param string $name * @return Author|null */ public function __get($name) { switch ($name) { case 'authors': if (!empty($this->author_id)) { return Author::findById($this->author_id); } return null; default: return null; } }
public function fill($arr) { foreach ($arr as $k => $v) { $k = trim($k); $v = trim($v); if ('author' == $k) { continue; } $this->{$k} = $v; } $this->author_id = !empty($this->author_id) ? $this->author_id : null; // Достаем из БД массив полей с ограничением NotNull $notNullColumn = $this->getNotNullCol(); $e = new \Lib\MultiException(); foreach ($this as $k => $v) { if (in_array($k, $notNullColumn) && (empty($v) || '' == $v) && 'id' != $k) { $msg = 'title' == $k ? 'заголовка' : 'текста новости'; $e[] = new MyException('Проверка данных: пустое поле ' . $msg . ' '); } } if (!empty($this->author_id)) { $auth = \App\Models\Author::findById($this->author_id); if (!$auth) { $e[] = new Err404('Проверка данных: такой автор не существует '); } } if (!is_null($e[0])) { throw $e; } return $this; }