Complex classes like Model often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 51 | abstract class Model implements ArrayAccess |
||
| 52 | { |
||
| 53 | const DEFAULT_ID_NAME = 'id'; |
||
| 54 | |||
| 55 | ///////////////////////////// |
||
| 56 | // Model visible variables |
||
| 57 | ///////////////////////////// |
||
| 58 | |||
| 59 | /** |
||
| 60 | * List of model ID property names. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected static $ids = [self::DEFAULT_ID_NAME]; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Property definitions expressed as a key-value map with |
||
| 68 | * property names as the keys. |
||
| 69 | * i.e. ['enabled' => ['type' => Type::BOOLEAN]]. |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected static $properties = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $_values = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | private $_unsaved = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var bool |
||
| 87 | */ |
||
| 88 | protected $_persisted = false; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $_relationships = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var AbstractRelation[] |
||
| 97 | */ |
||
| 98 | private $relationships = []; |
||
| 99 | |||
| 100 | ///////////////////////////// |
||
| 101 | // Base model variables |
||
| 102 | ///////////////////////////// |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | private static $initialized = []; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var DriverInterface |
||
| 111 | */ |
||
| 112 | private static $driver; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | private static $accessors = []; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | private static $mutators = []; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | private static $dispatchers = []; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var string |
||
| 131 | */ |
||
| 132 | private $tablename; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var bool |
||
| 136 | */ |
||
| 137 | private $hasId; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var array |
||
| 141 | */ |
||
| 142 | private $idValues; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @var bool |
||
| 146 | */ |
||
| 147 | private $loaded = false; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var Errors |
||
| 151 | */ |
||
| 152 | private $errors; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var bool |
||
| 156 | */ |
||
| 157 | private $ignoreUnsaved = false; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Creates a new model object. |
||
| 161 | * |
||
| 162 | * @param array|string|Model|false $id ordered array of ids or comma-separated id string |
||
|
|
|||
| 163 | * @param array $values optional key-value map to pre-seed model |
||
| 164 | */ |
||
| 165 | public function __construct(array $values = []) |
||
| 166 | { |
||
| 167 | // initialize the model |
||
| 168 | $this->init(); |
||
| 169 | |||
| 170 | $ids = []; |
||
| 171 | $this->hasId = true; |
||
| 172 | foreach (static::$ids as $name) { |
||
| 173 | $id = null; |
||
| 174 | if (array_key_exists($name, $values)) { |
||
| 175 | $idProperty = static::definition()->get($name); |
||
| 176 | $id = Type::cast($idProperty, $values[$name]); |
||
| 177 | } |
||
| 178 | |||
| 179 | $ids[$name] = $id; |
||
| 180 | $this->hasId = $this->hasId && $id; |
||
| 181 | } |
||
| 182 | |||
| 183 | $this->idValues = $ids; |
||
| 184 | |||
| 185 | // load any given values |
||
| 186 | if ($this->hasId && count($values) > count($ids)) { |
||
| 187 | $this->refreshWith($values); |
||
| 188 | } elseif (!$this->hasId) { |
||
| 189 | $this->_unsaved = $values; |
||
| 190 | } else { |
||
| 191 | $this->_values = $this->idValues; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Performs initialization on this model. |
||
| 197 | */ |
||
| 198 | private function init() |
||
| 199 | { |
||
| 200 | // ensure the initialize function is called only once |
||
| 201 | $k = static::class; |
||
| 202 | if (!isset(self::$initialized[$k])) { |
||
| 203 | $this->initialize(); |
||
| 204 | self::$initialized[$k] = true; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * The initialize() method is called once per model. This is a great |
||
| 210 | * place to install event listeners. Any methods on the model that have |
||
| 211 | * "autoInitialize" in the name will automatically be called. |
||
| 212 | */ |
||
| 213 | protected function initialize() |
||
| 214 | { |
||
| 215 | // Use reflection to automatically call any method here that has a name |
||
| 216 | // that starts with "autoInitialize". This is useful for traits to install listeners. |
||
| 217 | $methods = get_class_methods(static::class); |
||
| 218 | foreach ($methods as $method) { |
||
| 219 | if (0 === strpos($method, 'autoInitialize')) { |
||
| 220 | $this->$method(); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Sets the driver for all models. |
||
| 227 | */ |
||
| 228 | public static function setDriver(DriverInterface $driver) |
||
| 229 | { |
||
| 230 | self::$driver = $driver; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Gets the driver for all models. |
||
| 235 | * |
||
| 236 | * @throws DriverMissingException when a driver has not been set yet |
||
| 237 | */ |
||
| 238 | public static function getDriver(): DriverInterface |
||
| 239 | { |
||
| 240 | if (!self::$driver) { |
||
| 241 | throw new DriverMissingException('A model driver has not been set yet.'); |
||
| 242 | } |
||
| 243 | |||
| 244 | return self::$driver; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Clears the driver for all models. |
||
| 249 | */ |
||
| 250 | public static function clearDriver() |
||
| 251 | { |
||
| 252 | self::$driver = null; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Gets the name of the model, i.e. User. |
||
| 257 | */ |
||
| 258 | public static function modelName(): string |
||
| 259 | { |
||
| 260 | // strip namespacing |
||
| 261 | $paths = explode('\\', static::class); |
||
| 262 | |||
| 263 | return end($paths); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Gets the model ID. |
||
| 268 | * |
||
| 269 | * @return string|number|false ID |
||
| 270 | */ |
||
| 271 | public function id() |
||
| 272 | { |
||
| 273 | if (!$this->hasId) { |
||
| 274 | return false; |
||
| 275 | } |
||
| 276 | |||
| 277 | if (1 == count($this->idValues)) { |
||
| 278 | return reset($this->idValues); |
||
| 279 | } |
||
| 280 | |||
| 281 | $result = []; |
||
| 282 | foreach (static::$ids as $k) { |
||
| 283 | $result[] = $this->idValues[$k]; |
||
| 284 | } |
||
| 285 | |||
| 286 | return implode(',', $result); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Gets a key-value map of the model ID. |
||
| 291 | * |
||
| 292 | * @return array ID map |
||
| 293 | */ |
||
| 294 | public function ids(): array |
||
| 295 | { |
||
| 296 | return $this->idValues; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Checks if the model has an identifier present. |
||
| 301 | * This does not indicate whether the model has been |
||
| 302 | * persisted to the database or loaded from the database. |
||
| 303 | */ |
||
| 304 | public function hasId(): bool |
||
| 305 | { |
||
| 306 | return $this->hasId; |
||
| 307 | } |
||
| 308 | |||
| 309 | ///////////////////////////// |
||
| 310 | // Magic Methods |
||
| 311 | ///////////////////////////// |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Converts the model into a string. |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public function __toString() |
||
| 319 | { |
||
| 320 | $values = array_merge($this->_values, $this->_unsaved, $this->idValues); |
||
| 321 | ksort($values); |
||
| 322 | |||
| 323 | return static::class.'('.json_encode($values, JSON_PRETTY_PRINT).')'; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Shortcut to a get() call for a given property. |
||
| 328 | * |
||
| 329 | * @param string $name |
||
| 330 | * |
||
| 331 | * @return mixed |
||
| 332 | */ |
||
| 333 | public function __get($name) |
||
| 334 | { |
||
| 335 | $result = $this->get([$name]); |
||
| 336 | |||
| 337 | return reset($result); |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Sets an unsaved value. |
||
| 342 | * |
||
| 343 | * @param string $name |
||
| 344 | * @param mixed $value |
||
| 345 | */ |
||
| 346 | public function __set($name, $value) |
||
| 347 | { |
||
| 348 | // if changing property, remove relation model |
||
| 349 | if (isset($this->_relationships[$name])) { |
||
| 350 | unset($this->_relationships[$name]); |
||
| 351 | } |
||
| 352 | |||
| 353 | // call any mutators |
||
| 354 | $mutator = self::getMutator($name); |
||
| 355 | if ($mutator) { |
||
| 356 | $this->_unsaved[$name] = $this->$mutator($value); |
||
| 357 | } else { |
||
| 358 | $this->_unsaved[$name] = $value; |
||
| 359 | } |
||
| 360 | |||
| 361 | // set local ID property on belongs_to relationship |
||
| 362 | if (static::definition()->has($name)) { |
||
| 363 | $property = static::definition()->get($name); |
||
| 364 | if (Relationship::BELONGS_TO == $property->getRelationshipType() && !$property->isPersisted()) { |
||
| 365 | if ($value instanceof self) { |
||
| 366 | $this->_unsaved[$property->getLocalKey()] = $value->{$property->getForeignKey()}; |
||
| 367 | } elseif (null === $value) { |
||
| 368 | $this->_unsaved[$property->getLocalKey()] = null; |
||
| 369 | } else { |
||
| 370 | throw new ModelException('The value set on the "'.$name.'" property must be a model or null.'); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Checks if an unsaved value or property exists by this name. |
||
| 378 | * |
||
| 379 | * @param string $name |
||
| 380 | * |
||
| 381 | * @return bool |
||
| 382 | */ |
||
| 383 | public function __isset($name) |
||
| 384 | { |
||
| 385 | // isset() must return true for any value that could be returned by offsetGet |
||
| 386 | // because many callers will first check isset() to see if the value is accessible. |
||
| 387 | // This method is not supposed to only be valid for unsaved values, or properties |
||
| 388 | // that have a value. |
||
| 389 | return array_key_exists($name, $this->_unsaved) || static::definition()->has($name); |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Unsets an unsaved value. |
||
| 394 | * |
||
| 395 | * @param string $name |
||
| 396 | */ |
||
| 397 | public function __unset($name) |
||
| 398 | { |
||
| 399 | if (array_key_exists($name, $this->_unsaved)) { |
||
| 400 | // if changing property, remove relation model |
||
| 401 | if (isset($this->_relationships[$name])) { |
||
| 402 | unset($this->_relationships[$name]); |
||
| 403 | } |
||
| 404 | |||
| 405 | unset($this->_unsaved[$name]); |
||
| 406 | } |
||
| 407 | } |
||
| 408 | |||
| 409 | ///////////////////////////// |
||
| 410 | // ArrayAccess Interface |
||
| 411 | ///////////////////////////// |
||
| 412 | |||
| 413 | public function offsetExists($offset) |
||
| 414 | { |
||
| 415 | return isset($this->$offset); |
||
| 416 | } |
||
| 417 | |||
| 418 | public function offsetGet($offset) |
||
| 419 | { |
||
| 420 | return $this->$offset; |
||
| 421 | } |
||
| 422 | |||
| 423 | public function offsetSet($offset, $value) |
||
| 424 | { |
||
| 425 | $this->$offset = $value; |
||
| 426 | } |
||
| 427 | |||
| 428 | public function offsetUnset($offset) |
||
| 429 | { |
||
| 430 | unset($this->$offset); |
||
| 431 | } |
||
| 432 | |||
| 433 | public static function __callStatic($name, $parameters) |
||
| 434 | { |
||
| 435 | // Any calls to unkown static methods should be deferred to |
||
| 436 | // the query. This allows calls like User::where() |
||
| 437 | // to replace User::query()->where(). |
||
| 438 | return call_user_func_array([static::query(), $name], $parameters); |
||
| 439 | } |
||
| 440 | |||
| 441 | ///////////////////////////// |
||
| 442 | // Property Definitions |
||
| 443 | ///////////////////////////// |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Gets the model definition. |
||
| 447 | */ |
||
| 448 | public static function definition(): Definition |
||
| 449 | { |
||
| 450 | return DefinitionBuilder::get(static::class); |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * The buildDefinition() method is called once per model. It's used |
||
| 455 | * to generate the model definition. This is a great place to add any |
||
| 456 | * dynamic model properties. |
||
| 457 | */ |
||
| 458 | public static function buildDefinition(): Definition |
||
| 459 | { |
||
| 460 | // Use reflection to automatically call any method on the model that has a name |
||
| 461 | // that starts with "buildDefinition". This is useful for traits to add properties. |
||
| 462 | $methods = get_class_methods(static::class); |
||
| 463 | foreach ($methods as $method) { |
||
| 464 | if (0 === strpos($method, 'autoDefinition')) { |
||
| 465 | static::$method(); |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | // Deprecated: this setting is deprecated |
||
| 470 | // remove any hidden properties |
||
| 471 | if (property_exists(static::class, 'hidden')) { |
||
| 472 | foreach (static::$hidden as $k) { |
||
| 473 | if (isset(static::$properties[$k])) { |
||
| 474 | static::$properties[$k]['in_array'] = false; |
||
| 475 | } |
||
| 476 | } |
||
| 477 | } |
||
| 478 | |||
| 479 | // Deprecated: this setting is deprecated |
||
| 480 | // add any appended properties |
||
| 481 | if (property_exists(static::class, 'appended')) { |
||
| 482 | foreach (static::$appended as $k) { |
||
| 483 | static::$properties[$k] = [ |
||
| 484 | 'in_array' => true, |
||
| 485 | 'persisted' => false, |
||
| 486 | ]; |
||
| 487 | } |
||
| 488 | } |
||
| 489 | |||
| 490 | return DefinitionBuilder::build(static::$properties, static::class); |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Gets the names of the model ID properties. |
||
| 495 | */ |
||
| 496 | public static function getIDProperties(): array |
||
| 497 | { |
||
| 498 | return static::$ids; |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Gets the mutator method name for a given property name. |
||
| 503 | * Looks for methods in the form of `setPropertyValue`. |
||
| 504 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 505 | * |
||
| 506 | * @param string $property property |
||
| 507 | * |
||
| 508 | * @return string|null method name if it exists |
||
| 509 | */ |
||
| 510 | public static function getMutator(string $property): ?string |
||
| 511 | { |
||
| 512 | $class = static::class; |
||
| 513 | |||
| 514 | $k = $class.':'.$property; |
||
| 515 | if (!array_key_exists($k, self::$mutators)) { |
||
| 516 | $inflector = Inflector::get(); |
||
| 517 | $method = 'set'.$inflector->camelize($property).'Value'; |
||
| 518 | |||
| 519 | if (!method_exists($class, $method)) { |
||
| 520 | $method = null; |
||
| 521 | } |
||
| 522 | |||
| 523 | self::$mutators[$k] = $method; |
||
| 524 | } |
||
| 525 | |||
| 526 | return self::$mutators[$k]; |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Gets the accessor method name for a given property name. |
||
| 531 | * Looks for methods in the form of `getPropertyValue`. |
||
| 532 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 533 | * |
||
| 534 | * @param string $property property |
||
| 535 | * |
||
| 536 | * @return string|null method name if it exists |
||
| 537 | */ |
||
| 538 | public static function getAccessor(string $property): ?string |
||
| 539 | { |
||
| 540 | $class = static::class; |
||
| 541 | |||
| 542 | $k = $class.':'.$property; |
||
| 543 | if (!array_key_exists($k, self::$accessors)) { |
||
| 544 | $inflector = Inflector::get(); |
||
| 545 | $method = 'get'.$inflector->camelize($property).'Value'; |
||
| 546 | |||
| 547 | if (!method_exists($class, $method)) { |
||
| 548 | $method = null; |
||
| 549 | } |
||
| 550 | |||
| 551 | self::$accessors[$k] = $method; |
||
| 552 | } |
||
| 553 | |||
| 554 | return self::$accessors[$k]; |
||
| 555 | } |
||
| 556 | |||
| 557 | ///////////////////////////// |
||
| 558 | // CRUD Operations |
||
| 559 | ///////////////////////////// |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Gets the table name for storing this model. |
||
| 563 | */ |
||
| 564 | public function getTablename(): string |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Gets the ID of the connection in the connection manager |
||
| 577 | * that stores this model. |
||
| 578 | */ |
||
| 579 | public function getConnection(): ?string |
||
| 583 | |||
| 584 | protected function usesTransactions(): bool |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Saves the model. |
||
| 591 | * |
||
| 592 | * @return bool true when the operation was successful |
||
| 593 | */ |
||
| 594 | public function save(): bool |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Saves the model. Throws an exception when the operation fails. |
||
| 605 | * |
||
| 606 | * @throws ModelException when the model cannot be saved |
||
| 607 | */ |
||
| 608 | public function saveOrFail() |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Creates a new model. |
||
| 622 | * |
||
| 623 | * @param array $data optional key-value properties to set |
||
| 624 | * |
||
| 625 | * @return bool true when the operation was successful |
||
| 626 | * |
||
| 627 | * @throws BadMethodCallException when called on an existing model |
||
| 628 | */ |
||
| 629 | public function create(array $data = []): bool |
||
| 630 | { |
||
| 631 | if ($this->hasId) { |
||
| 632 | throw new BadMethodCallException('Cannot call create() on an existing model'); |
||
| 633 | } |
||
| 634 | |||
| 635 | // mass assign values passed into create() |
||
| 636 | $this->setValues($data); |
||
| 637 | |||
| 638 | // clear any previous errors |
||
| 639 | $this->getErrors()->clear(); |
||
| 640 | |||
| 641 | // start a DB transaction if needed |
||
| 642 | $usesTransactions = $this->usesTransactions(); |
||
| 643 | if ($usesTransactions) { |
||
| 644 | self::$driver->startTransaction($this->getConnection()); |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Ignores unsaved values when fetching the next value. |
||
| 756 | * |
||
| 757 | * @return $this |
||
| 758 | */ |
||
| 759 | public function ignoreUnsaved() |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Fetches property values from the model. |
||
| 768 | * |
||
| 769 | * This method looks up values in this order: |
||
| 770 | * IDs, local cache, unsaved values, storage layer, defaults |
||
| 771 | * |
||
| 772 | * @param array $properties list of property names to fetch values of |
||
| 773 | */ |
||
| 774 | public function get(array $properties): array |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Loads the model from the database if needed. |
||
| 798 | */ |
||
| 799 | private function loadIfNeeded(array $properties, bool $ignoreUnsaved): void |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Gets a property value from the model. |
||
| 819 | * |
||
| 820 | * Values are looked up in this order: |
||
| 821 | * 1. unsaved values |
||
| 822 | * 2. local values |
||
| 823 | * 3. default value |
||
| 824 | * 4. null |
||
| 825 | * |
||
| 826 | * @return mixed |
||
| 827 | */ |
||
| 828 | private function getValue(string $name, bool $ignoreUnsaved) |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Populates a newly created model with its ID. |
||
| 854 | */ |
||
| 855 | private function getNewId() |
||
| 877 | |||
| 878 | protected function getMassAssignmentWhitelist(): ?array |
||
| 887 | |||
| 888 | protected function getMassAssignmentBlacklist(): ?array |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Sets a collection values on the model from an untrusted input. |
||
| 900 | * |
||
| 901 | * @param array $values |
||
| 902 | * |
||
| 903 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
| 904 | * |
||
| 905 | * @return $this |
||
| 906 | */ |
||
| 907 | public function setValues($values) |
||
| 938 | |||
| 939 | /** |
||
| 940 | * Converts the model to an array. |
||
| 941 | */ |
||
| 942 | public function toArray(): array |
||
| 973 | |||
| 974 | /** |
||
| 975 | * Checks if the unsaved value for a property is present and |
||
| 976 | * is different from the original value. |
||
| 977 | * |
||
| 978 | * @property string|null $name |
||
| 979 | * @property bool $hasChanged when true, checks if the unsaved value is different from the saved value |
||
| 980 | */ |
||
| 981 | public function dirty(?string $name = null, bool $hasChanged = false): bool |
||
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Updates the model. |
||
| 1004 | * |
||
| 1005 | * @param array $data optional key-value properties to set |
||
| 1006 | * |
||
| 1007 | * @return bool true when the operation was successful |
||
| 1008 | * |
||
| 1009 | * @throws BadMethodCallException when not called on an existing model |
||
| 1010 | */ |
||
| 1011 | public function set(array $data = []): bool |
||
| 1110 | |||
| 1111 | /** |
||
| 1112 | * Delete the model. |
||
| 1113 | * |
||
| 1114 | * @return bool true when the operation was successful |
||
| 1115 | */ |
||
| 1116 | public function delete(): bool |
||
| 1166 | |||
| 1167 | /** |
||
| 1168 | * Restores a soft-deleted model. |
||
| 1169 | */ |
||
| 1170 | public function restore(): bool |
||
| 1204 | |||
| 1205 | /** |
||
| 1206 | * Checks if the model has been deleted. |
||
| 1207 | */ |
||
| 1208 | public function isDeleted(): bool |
||
| 1216 | |||
| 1217 | ///////////////////////////// |
||
| 1218 | // Queries |
||
| 1219 | ///////////////////////////// |
||
| 1220 | |||
| 1221 | /** |
||
| 1222 | * Generates a new query instance. |
||
| 1223 | */ |
||
| 1224 | public static function query(): Query |
||
| 1239 | |||
| 1240 | /** |
||
| 1241 | * Generates a new query instance that includes soft-deleted models. |
||
| 1242 | */ |
||
| 1243 | public static function withDeleted(): Query |
||
| 1252 | |||
| 1253 | /** |
||
| 1254 | * Finds a single instance of a model given it's ID. |
||
| 1255 | * |
||
| 1256 | * @param mixed $id |
||
| 1257 | * |
||
| 1258 | * @return static|null |
||
| 1259 | */ |
||
| 1260 | public static function find($id): ?self |
||
| 1277 | |||
| 1278 | /** |
||
| 1279 | * Finds a single instance of a model given it's ID or throws an exception. |
||
| 1280 | * |
||
| 1281 | * @param mixed $id |
||
| 1282 | * |
||
| 1283 | * @return static |
||
| 1284 | * |
||
| 1285 | * @throws ModelNotFoundException when a model could not be found |
||
| 1286 | */ |
||
| 1287 | public static function findOrFail($id): self |
||
| 1296 | |||
| 1297 | /** |
||
| 1298 | * Tells if this model instance has been persisted to the data layer. |
||
| 1299 | * |
||
| 1300 | * NOTE: this does not actually perform a check with the data layer |
||
| 1301 | */ |
||
| 1302 | public function persisted(): bool |
||
| 1306 | |||
| 1307 | /** |
||
| 1308 | * Loads the model from the storage layer. |
||
| 1309 | * |
||
| 1310 | * @return $this |
||
| 1311 | */ |
||
| 1312 | public function refresh() |
||
| 1336 | |||
| 1337 | /** |
||
| 1338 | * Loads values into the model. |
||
| 1339 | * |
||
| 1340 | * @param array $values values |
||
| 1341 | * |
||
| 1342 | * @return $this |
||
| 1343 | */ |
||
| 1344 | public function refreshWith(array $values) |
||
| 1352 | |||
| 1353 | /** |
||
| 1354 | * Clears the cache for this model. |
||
| 1355 | * |
||
| 1356 | * @return $this |
||
| 1357 | */ |
||
| 1358 | public function clearCache() |
||
| 1367 | |||
| 1368 | ///////////////////////////// |
||
| 1369 | // Relationships |
||
| 1370 | ///////////////////////////// |
||
| 1371 | |||
| 1372 | /** |
||
| 1373 | * Gets the relationship manager for a property. |
||
| 1374 | * |
||
| 1375 | * @throws InvalidArgumentException when the relationship manager cannot be created |
||
| 1376 | */ |
||
| 1377 | private function getRelationship(Property $property): AbstractRelation |
||
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Saves any unsaved models attached through a relationship. This will only |
||
| 1389 | * save attached models that have not been saved yet. |
||
| 1390 | */ |
||
| 1391 | private function saveRelationships(bool $usesTransactions): bool |
||
| 1425 | |||
| 1426 | /** |
||
| 1427 | * This hydrates an individual property in the model. It can be a |
||
| 1428 | * scalar value or relationship. |
||
| 1429 | * |
||
| 1430 | * @internal |
||
| 1431 | * |
||
| 1432 | * @param $value |
||
| 1433 | */ |
||
| 1434 | public function hydrateValue(string $name, $value): void |
||
| 1443 | |||
| 1444 | /** |
||
| 1445 | * @deprecated |
||
| 1446 | * |
||
| 1447 | * Gets the model(s) for a relationship |
||
| 1448 | * |
||
| 1449 | * @param string $k property |
||
| 1450 | * |
||
| 1451 | * @throws InvalidArgumentException when the relationship manager cannot be created |
||
| 1452 | * |
||
| 1453 | * @return Model|array|null |
||
| 1454 | */ |
||
| 1455 | public function relation(string $k) |
||
| 1464 | |||
| 1465 | /** |
||
| 1466 | * @deprecated |
||
| 1467 | * |
||
| 1468 | * Sets the model for a one-to-one relationship (has-one or belongs-to) |
||
| 1469 | * |
||
| 1470 | * @return $this |
||
| 1471 | */ |
||
| 1472 | public function setRelation(string $k, Model $model) |
||
| 1479 | |||
| 1480 | /** |
||
| 1481 | * @deprecated |
||
| 1482 | * |
||
| 1483 | * Sets the model for a one-to-many relationship |
||
| 1484 | * |
||
| 1485 | * @return $this |
||
| 1486 | */ |
||
| 1487 | public function setRelationCollection(string $k, iterable $models) |
||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * @deprecated |
||
| 1496 | * |
||
| 1497 | * Sets the model for a one-to-one relationship (has-one or belongs-to) as null |
||
| 1498 | * |
||
| 1499 | * @return $this |
||
| 1500 | */ |
||
| 1501 | public function clearRelation(string $k) |
||
| 1508 | |||
| 1509 | ///////////////////////////// |
||
| 1510 | // Events |
||
| 1511 | ///////////////////////////// |
||
| 1512 | |||
| 1513 | /** |
||
| 1514 | * Gets the event dispatcher. |
||
| 1515 | */ |
||
| 1516 | public static function getDispatcher($ignoreCache = false): EventDispatcher |
||
| 1525 | |||
| 1526 | /** |
||
| 1527 | * Subscribes to a listener to an event. |
||
| 1528 | * |
||
| 1529 | * @param string $event event name |
||
| 1530 | * @param int $priority optional priority, higher #s get called first |
||
| 1531 | */ |
||
| 1532 | public static function listen(string $event, callable $listener, int $priority = 0) |
||
| 1536 | |||
| 1537 | /** |
||
| 1538 | * Adds a listener to the model.creating and model.updating events. |
||
| 1539 | */ |
||
| 1540 | public static function saving(callable $listener, int $priority = 0) |
||
| 1545 | |||
| 1546 | /** |
||
| 1547 | * Adds a listener to the model.created and model.updated events. |
||
| 1548 | */ |
||
| 1549 | public static function saved(callable $listener, int $priority = 0) |
||
| 1554 | |||
| 1555 | /** |
||
| 1556 | * Adds a listener to the model.creating, model.updating, and model.deleting events. |
||
| 1557 | */ |
||
| 1558 | public static function beforePersist(callable $listener, int $priority = 0) |
||
| 1564 | |||
| 1565 | /** |
||
| 1566 | * Adds a listener to the model.created, model.updated, and model.deleted events. |
||
| 1567 | */ |
||
| 1568 | public static function afterPersist(callable $listener, int $priority = 0) |
||
| 1574 | |||
| 1575 | /** |
||
| 1576 | * Adds a listener to the model.creating event. |
||
| 1577 | */ |
||
| 1578 | public static function creating(callable $listener, int $priority = 0) |
||
| 1582 | |||
| 1583 | /** |
||
| 1584 | * Adds a listener to the model.created event. |
||
| 1585 | */ |
||
| 1586 | public static function created(callable $listener, int $priority = 0) |
||
| 1590 | |||
| 1591 | /** |
||
| 1592 | * Adds a listener to the model.updating event. |
||
| 1593 | */ |
||
| 1594 | public static function updating(callable $listener, int $priority = 0) |
||
| 1598 | |||
| 1599 | /** |
||
| 1600 | * Adds a listener to the model.updated event. |
||
| 1601 | */ |
||
| 1602 | public static function updated(callable $listener, int $priority = 0) |
||
| 1606 | |||
| 1607 | /** |
||
| 1608 | * Adds a listener to the model.deleting event. |
||
| 1609 | */ |
||
| 1610 | public static function deleting(callable $listener, int $priority = 0) |
||
| 1614 | |||
| 1615 | /** |
||
| 1616 | * Adds a listener to the model.deleted event. |
||
| 1617 | */ |
||
| 1618 | public static function deleted(callable $listener, int $priority = 0) |
||
| 1622 | |||
| 1623 | /** |
||
| 1624 | * Dispatches the given event and checks if it was successful. |
||
| 1625 | * |
||
| 1626 | * @return bool true if the events were successfully propagated |
||
| 1627 | */ |
||
| 1628 | private function performDispatch(AbstractEvent $event, bool $usesTransactions): bool |
||
| 1643 | |||
| 1644 | ///////////////////////////// |
||
| 1645 | // Validation |
||
| 1646 | ///////////////////////////// |
||
| 1647 | |||
| 1648 | /** |
||
| 1649 | * Gets the error stack for this model. |
||
| 1650 | */ |
||
| 1651 | public function getErrors(): Errors |
||
| 1659 | |||
| 1660 | /** |
||
| 1661 | * Checks if the model in its current state is valid. |
||
| 1662 | */ |
||
| 1663 | public function valid(): bool |
||
| 1677 | } |
||
| 1678 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.