Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 31 | abstract class Model implements \ArrayAccess |
||
| 32 | { |
||
| 33 | const IMMUTABLE = 0; |
||
| 34 | const MUTABLE_CREATE_ONLY = 1; |
||
| 35 | const MUTABLE = 2; |
||
| 36 | |||
| 37 | const TYPE_STRING = 'string'; |
||
| 38 | const TYPE_NUMBER = 'number'; // DEPRECATED |
||
| 39 | const TYPE_INTEGER = 'integer'; |
||
| 40 | const TYPE_FLOAT = 'float'; |
||
| 41 | const TYPE_BOOLEAN = 'boolean'; |
||
| 42 | const TYPE_DATE = 'date'; |
||
| 43 | const TYPE_OBJECT = 'object'; |
||
| 44 | const TYPE_ARRAY = 'array'; |
||
| 45 | |||
| 46 | const DEFAULT_ID_PROPERTY = 'id'; |
||
| 47 | |||
| 48 | ///////////////////////////// |
||
| 49 | // Model visible variables |
||
| 50 | ///////////////////////////// |
||
| 51 | |||
| 52 | /** |
||
| 53 | * List of model ID property names. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected static $ids = [self::DEFAULT_ID_PROPERTY]; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Property definitions expressed as a key-value map with |
||
| 61 | * property names as the keys. |
||
| 62 | * i.e. ['enabled' => ['type' => Model::TYPE_BOOLEAN]]. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected static $properties = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected static $dispatchers; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var Container |
||
| 75 | */ |
||
| 76 | protected static $globalContainer; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var Errors |
||
| 80 | */ |
||
| 81 | protected static $globalErrorStack; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var number|string|false |
||
| 85 | */ |
||
| 86 | protected $_id; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $_ids; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $_values = []; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | protected $_unsaved = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var bool |
||
| 105 | */ |
||
| 106 | protected $_persisted = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | protected $_relationships = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var Errors |
||
| 115 | */ |
||
| 116 | protected $_errors; |
||
| 117 | |||
| 118 | ///////////////////////////// |
||
| 119 | // Base model variables |
||
| 120 | ///////////////////////////// |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | private static $propertyDefinitionBase = [ |
||
| 126 | 'type' => null, |
||
| 127 | 'mutable' => self::MUTABLE, |
||
| 128 | 'null' => false, |
||
| 129 | 'unique' => false, |
||
| 130 | 'required' => false, |
||
| 131 | ]; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var array |
||
| 135 | */ |
||
| 136 | private static $defaultIDProperty = [ |
||
| 137 | 'type' => self::TYPE_INTEGER, |
||
| 138 | 'mutable' => self::IMMUTABLE, |
||
| 139 | ]; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var array |
||
| 143 | */ |
||
| 144 | private static $timestampProperties = [ |
||
| 145 | 'created_at' => [ |
||
| 146 | 'type' => self::TYPE_DATE, |
||
| 147 | 'validate' => 'timestamp|db_timestamp', |
||
| 148 | ], |
||
| 149 | 'updated_at' => [ |
||
| 150 | 'type' => self::TYPE_DATE, |
||
| 151 | 'validate' => 'timestamp|db_timestamp', |
||
| 152 | ], |
||
| 153 | ]; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | private static $softDeleteProperties = [ |
||
| 159 | 'deleted_at' => [ |
||
| 160 | 'type' => self::TYPE_DATE, |
||
| 161 | 'validate' => 'timestamp|db_timestamp', |
||
| 162 | 'null' => true, |
||
| 163 | ], |
||
| 164 | ]; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @var array |
||
| 168 | */ |
||
| 169 | private static $initialized = []; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @var DriverInterface |
||
| 173 | */ |
||
| 174 | private static $driver; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @var array |
||
| 178 | */ |
||
| 179 | private static $accessors = []; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @var array |
||
| 183 | */ |
||
| 184 | private static $mutators = []; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @var bool |
||
| 188 | */ |
||
| 189 | private $_ignoreUnsaved; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Creates a new model object. |
||
| 193 | * |
||
| 194 | * @param array|string|Model|false $id ordered array of ids or comma-separated id string |
||
| 195 | * @param array $values optional key-value map to pre-seed model |
||
| 196 | */ |
||
| 197 | public function __construct($id = false, array $values = []) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Performs initialization on this model. |
||
| 213 | */ |
||
| 214 | private function init() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * The initialize() method is called once per model. It's used |
||
| 226 | * to perform any one-off tasks before the model gets |
||
| 227 | * constructed. This is a great place to add any model |
||
| 228 | * properties. When extending this method be sure to call |
||
| 229 | * parent::initialize() as some important stuff happens here. |
||
| 230 | * If extending this method to add properties then you should |
||
| 231 | * call parent::initialize() after adding any properties. |
||
| 232 | */ |
||
| 233 | protected function initialize() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Installs the `created_at` and `updated_at` properties. |
||
| 266 | */ |
||
| 267 | private function installAutoTimestamps() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Installs the `deleted_at` properties. |
||
| 284 | */ |
||
| 285 | private function installSoftDelete() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Parses the given ID, which can be a single or composite primary key. |
||
| 292 | * |
||
| 293 | * @param mixed $id |
||
| 294 | */ |
||
| 295 | private function parseId($id) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @deprecated |
||
| 341 | * |
||
| 342 | * Injects a DI container |
||
| 343 | * |
||
| 344 | * @param Container $container |
||
| 345 | */ |
||
| 346 | public static function inject(Container $container) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @deprecated |
||
| 353 | * |
||
| 354 | * Gets the DI container used for this model |
||
| 355 | * |
||
| 356 | * @return Container|null |
||
| 357 | */ |
||
| 358 | public function getApp() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Sets the driver for all models. |
||
| 365 | * |
||
| 366 | * @param DriverInterface $driver |
||
| 367 | */ |
||
| 368 | public static function setDriver(DriverInterface $driver) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Gets the driver for all models. |
||
| 375 | * |
||
| 376 | * @return DriverInterface |
||
| 377 | * |
||
| 378 | * @throws DriverMissingException when a driver has not been set yet |
||
| 379 | */ |
||
| 380 | public static function getDriver() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Clears the driver for all models. |
||
| 391 | */ |
||
| 392 | public static function clearDriver() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Gets the name of the model, i.e. User. |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public static function modelName() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Gets the model ID. |
||
| 412 | * |
||
| 413 | * @return string|number|false ID |
||
| 414 | */ |
||
| 415 | public function id() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Gets a key-value map of the model ID. |
||
| 422 | * |
||
| 423 | * @return array ID map |
||
| 424 | */ |
||
| 425 | public function ids() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Sets the global error stack instance. |
||
| 432 | * |
||
| 433 | * @param Errors $stack |
||
| 434 | */ |
||
| 435 | public static function setErrorStack(Errors $stack) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Clears the global error stack instance. |
||
| 442 | */ |
||
| 443 | public static function clearErrorStack() |
||
| 447 | |||
| 448 | ///////////////////////////// |
||
| 449 | // Magic Methods |
||
| 450 | ///////////////////////////// |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Converts the model into a string. |
||
| 454 | * |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | public function __toString() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Shortcut to a get() call for a given property. |
||
| 464 | * |
||
| 465 | * @param string $name |
||
| 466 | * |
||
| 467 | * @return mixed |
||
| 468 | */ |
||
| 469 | public function __get($name) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Sets an unsaved value. |
||
| 478 | * |
||
| 479 | * @param string $name |
||
| 480 | * @param mixed $value |
||
| 481 | */ |
||
| 482 | public function __set($name, $value) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Checks if an unsaved value or property exists by this name. |
||
| 500 | * |
||
| 501 | * @param string $name |
||
| 502 | * |
||
| 503 | * @return bool |
||
| 504 | */ |
||
| 505 | public function __isset($name) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Unsets an unsaved value. |
||
| 512 | * |
||
| 513 | * @param string $name |
||
| 514 | */ |
||
| 515 | public function __unset($name) |
||
| 526 | |||
| 527 | ///////////////////////////// |
||
| 528 | // ArrayAccess Interface |
||
| 529 | ///////////////////////////// |
||
| 530 | |||
| 531 | public function offsetExists($offset) |
||
| 535 | |||
| 536 | public function offsetGet($offset) |
||
| 540 | |||
| 541 | public function offsetSet($offset, $value) |
||
| 545 | |||
| 546 | public function offsetUnset($offset) |
||
| 550 | |||
| 551 | public static function __callStatic($name, $parameters) |
||
| 558 | |||
| 559 | ///////////////////////////// |
||
| 560 | // Property Definitions |
||
| 561 | ///////////////////////////// |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Gets all the property definitions for the model. |
||
| 565 | * |
||
| 566 | * @return array key-value map of properties |
||
| 567 | */ |
||
| 568 | public static function getProperties() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Gets a property defition for the model. |
||
| 575 | * |
||
| 576 | * @param string $property property to lookup |
||
| 577 | * |
||
| 578 | * @return array|null property |
||
| 579 | */ |
||
| 580 | public static function getProperty($property) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Gets the names of the model ID properties. |
||
| 587 | * |
||
| 588 | * @return array |
||
| 589 | */ |
||
| 590 | public static function getIDProperties() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Checks if the model has a property. |
||
| 597 | * |
||
| 598 | * @param string $property property |
||
| 599 | * |
||
| 600 | * @return bool has property |
||
| 601 | */ |
||
| 602 | public static function hasProperty($property) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Gets the mutator method name for a given proeprty name. |
||
| 609 | * Looks for methods in the form of `setPropertyValue`. |
||
| 610 | * i.e. the mutator for `last_name` would be `setLastNameValue`. |
||
| 611 | * |
||
| 612 | * @param string $property property |
||
| 613 | * |
||
| 614 | * @return string|false method name if it exists |
||
| 615 | */ |
||
| 616 | View Code Duplication | public static function getMutator($property) |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Gets the accessor method name for a given proeprty name. |
||
| 637 | * Looks for methods in the form of `getPropertyValue`. |
||
| 638 | * i.e. the accessor for `last_name` would be `getLastNameValue`. |
||
| 639 | * |
||
| 640 | * @param string $property property |
||
| 641 | * |
||
| 642 | * @return string|false method name if it exists |
||
| 643 | */ |
||
| 644 | View Code Duplication | public static function getAccessor($property) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Marshals a value for a given property from storage. |
||
| 665 | * |
||
| 666 | * @param array $property |
||
| 667 | * @param mixed $value |
||
| 668 | * |
||
| 669 | * @return mixed type-casted value |
||
| 670 | */ |
||
| 671 | public static function cast(array $property, $value) |
||
| 691 | |||
| 692 | ///////////////////////////// |
||
| 693 | // CRUD Operations |
||
| 694 | ///////////////////////////// |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Gets the tablename for storing this model. |
||
| 698 | * |
||
| 699 | * @return string |
||
| 700 | */ |
||
| 701 | public function getTablename() |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Gets the ID of the connection in the connection manager |
||
| 710 | * that stores this model. |
||
| 711 | * |
||
| 712 | * @return string|false |
||
| 713 | */ |
||
| 714 | public function getConnection() |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Saves the model. |
||
| 721 | * |
||
| 722 | * @return bool true when the operation was successful |
||
| 723 | */ |
||
| 724 | public function save() |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Saves the model. Throws an exception when the operation fails. |
||
| 735 | * |
||
| 736 | * @throws ModelException when the model cannot be saved |
||
| 737 | */ |
||
| 738 | public function saveOrFail() |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Creates a new model. |
||
| 747 | * |
||
| 748 | * @param array $data optional key-value properties to set |
||
| 749 | * |
||
| 750 | * @return bool true when the operation was successful |
||
| 751 | * |
||
| 752 | * @throws BadMethodCallException when called on an existing model |
||
| 753 | */ |
||
| 754 | public function create(array $data = []) |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Ignores unsaved values when fetching the next value. |
||
| 846 | * |
||
| 847 | * @return self |
||
| 848 | */ |
||
| 849 | public function ignoreUnsaved() |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Fetches property values from the model. |
||
| 858 | * |
||
| 859 | * This method looks up values in this order: |
||
| 860 | * IDs, local cache, unsaved values, storage layer, defaults |
||
| 861 | * |
||
| 862 | * @param array $properties list of property names to fetch values of |
||
| 863 | * |
||
| 864 | * @return array |
||
| 865 | */ |
||
| 866 | public function get(array $properties) |
||
| 911 | |||
| 912 | /** |
||
| 913 | * Populates a newly created model with its ID. |
||
| 914 | */ |
||
| 915 | protected function getNewID() |
||
| 935 | |||
| 936 | /** |
||
| 937 | * Sets a collection values on the model from an untrusted input. |
||
| 938 | * |
||
| 939 | * @param array $values |
||
| 940 | * |
||
| 941 | * @throws MassAssignmentException when assigning a value that is protected or not whitelisted |
||
| 942 | * |
||
| 943 | * @return self |
||
| 944 | */ |
||
| 945 | public function setValues($values) |
||
| 965 | |||
| 966 | /** |
||
| 967 | * Converts the model to an array. |
||
| 968 | * |
||
| 969 | * @return array |
||
| 970 | */ |
||
| 971 | public function toArray() |
||
| 1002 | |||
| 1003 | /** |
||
| 1004 | * Updates the model. |
||
| 1005 | * |
||
| 1006 | * @param array $data optional key-value properties to set |
||
| 1007 | * |
||
| 1008 | * @return bool true when the operation was successful |
||
| 1009 | * |
||
| 1010 | * @throws BadMethodCallException when not called on an existing model |
||
| 1011 | */ |
||
| 1012 | public function set(array $data = []) |
||
| 1080 | |||
| 1081 | /** |
||
| 1082 | * Delete the model. |
||
| 1083 | * |
||
| 1084 | * @return bool true when the operation was successful |
||
| 1085 | */ |
||
| 1086 | public function delete() |
||
| 1122 | |||
| 1123 | /** |
||
| 1124 | * Restores a soft-deleted model. |
||
| 1125 | * |
||
| 1126 | * @return bool |
||
| 1127 | */ |
||
| 1128 | public function restore() |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * Checks if the model has been deleted. |
||
| 1154 | * |
||
| 1155 | * @return bool |
||
| 1156 | */ |
||
| 1157 | public function isDeleted() |
||
| 1165 | |||
| 1166 | ///////////////////////////// |
||
| 1167 | // Queries |
||
| 1168 | ///////////////////////////// |
||
| 1169 | |||
| 1170 | /** |
||
| 1171 | * Generates a new query instance. |
||
| 1172 | * |
||
| 1173 | * @return Query |
||
| 1174 | */ |
||
| 1175 | public static function query() |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Generates a new query instance that includes soft-deleted models. |
||
| 1193 | * |
||
| 1194 | * @return Query |
||
| 1195 | */ |
||
| 1196 | public static function withDeleted() |
||
| 1205 | |||
| 1206 | /** |
||
| 1207 | * Finds a single instance of a model given it's ID. |
||
| 1208 | * |
||
| 1209 | * @param mixed $id |
||
| 1210 | * |
||
| 1211 | * @return Model|null |
||
| 1212 | */ |
||
| 1213 | public static function find($id) |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * Finds a single instance of a model given it's ID or throws an exception. |
||
| 1233 | * |
||
| 1234 | * @param mixed $id |
||
| 1235 | * |
||
| 1236 | * @return Model |
||
| 1237 | * |
||
| 1238 | * @throws ModelNotFoundException when a model could not be found |
||
| 1239 | */ |
||
| 1240 | public static function findOrFail($id) |
||
| 1249 | |||
| 1250 | /** |
||
| 1251 | * @deprecated |
||
| 1252 | * |
||
| 1253 | * Checks if the model exists in the database |
||
| 1254 | * |
||
| 1255 | * @return bool |
||
| 1256 | */ |
||
| 1257 | public function exists() |
||
| 1261 | |||
| 1262 | /** |
||
| 1263 | * Tells if this model instance has been persisted to the data layer. |
||
| 1264 | * |
||
| 1265 | * NOTE: this does not actually perform a check with the data layer |
||
| 1266 | * |
||
| 1267 | * @return bool |
||
| 1268 | */ |
||
| 1269 | public function persisted() |
||
| 1273 | |||
| 1274 | /** |
||
| 1275 | * Loads the model from the storage layer. |
||
| 1276 | * |
||
| 1277 | * @return self |
||
| 1278 | */ |
||
| 1279 | public function refresh() |
||
| 1296 | |||
| 1297 | /** |
||
| 1298 | * Loads values into the model. |
||
| 1299 | * |
||
| 1300 | * @param array $values values |
||
| 1301 | * |
||
| 1302 | * @return self |
||
| 1303 | */ |
||
| 1304 | public function refreshWith(array $values) |
||
| 1318 | |||
| 1319 | /** |
||
| 1320 | * Clears the cache for this model. |
||
| 1321 | * |
||
| 1322 | * @return self |
||
| 1323 | */ |
||
| 1324 | public function clearCache() |
||
| 1332 | |||
| 1333 | ///////////////////////////// |
||
| 1334 | // Relationships |
||
| 1335 | ///////////////////////////// |
||
| 1336 | |||
| 1337 | /** |
||
| 1338 | * @deprecated |
||
| 1339 | * |
||
| 1340 | * Gets the model for a has-one relationship |
||
| 1341 | * |
||
| 1342 | * @param string $k property |
||
| 1343 | * |
||
| 1344 | * @return Model|null |
||
| 1345 | */ |
||
| 1346 | public function relation($k) |
||
| 1361 | |||
| 1362 | /** |
||
| 1363 | * @deprecated |
||
| 1364 | * |
||
| 1365 | * Sets the model for a has-one relationship |
||
| 1366 | * |
||
| 1367 | * @param string $k |
||
| 1368 | * @param Model $model |
||
| 1369 | * |
||
| 1370 | * @return self |
||
| 1371 | */ |
||
| 1372 | public function setRelation($k, Model $model) |
||
| 1379 | |||
| 1380 | /** |
||
| 1381 | * Creates the parent side of a One-To-One relationship. |
||
| 1382 | * |
||
| 1383 | * @param string $model foreign model class |
||
| 1384 | * @param string $foreignKey identifying key on foreign model |
||
| 1385 | * @param string $localKey identifying key on local model |
||
| 1386 | * |
||
| 1387 | * @return Relation\Relation |
||
| 1388 | */ |
||
| 1389 | public function hasOne($model, $foreignKey = '', $localKey = '') |
||
| 1393 | |||
| 1394 | /** |
||
| 1395 | * Creates the child side of a One-To-One or One-To-Many relationship. |
||
| 1396 | * |
||
| 1397 | * @param string $model foreign model class |
||
| 1398 | * @param string $foreignKey identifying key on foreign model |
||
| 1399 | * @param string $localKey identifying key on local model |
||
| 1400 | * |
||
| 1401 | * @return Relation\Relation |
||
| 1402 | */ |
||
| 1403 | public function belongsTo($model, $foreignKey = '', $localKey = '') |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * Creates the parent side of a Many-To-One or Many-To-Many relationship. |
||
| 1410 | * |
||
| 1411 | * @param string $model foreign model class |
||
| 1412 | * @param string $foreignKey identifying key on foreign model |
||
| 1413 | * @param string $localKey identifying key on local model |
||
| 1414 | * |
||
| 1415 | * @return Relation\Relation |
||
| 1416 | */ |
||
| 1417 | public function hasMany($model, $foreignKey = '', $localKey = '') |
||
| 1421 | |||
| 1422 | /** |
||
| 1423 | * Creates the child side of a Many-To-Many relationship. |
||
| 1424 | * |
||
| 1425 | * @param string $model foreign model class |
||
| 1426 | * @param string $tablename pivot table name |
||
| 1427 | * @param string $foreignKey identifying key on foreign model |
||
| 1428 | * @param string $localKey identifying key on local model |
||
| 1429 | * |
||
| 1430 | * @return \Pulsar\Relation\Relation |
||
| 1431 | */ |
||
| 1432 | public function belongsToMany($model, $tablename = '', $foreignKey = '', $localKey = '') |
||
| 1436 | |||
| 1437 | ///////////////////////////// |
||
| 1438 | // Events |
||
| 1439 | ///////////////////////////// |
||
| 1440 | |||
| 1441 | /** |
||
| 1442 | * Gets the event dispatcher. |
||
| 1443 | * |
||
| 1444 | * @return EventDispatcher |
||
| 1445 | */ |
||
| 1446 | public static function getDispatcher($ignoreCache = false) |
||
| 1455 | |||
| 1456 | /** |
||
| 1457 | * Subscribes to a listener to an event. |
||
| 1458 | * |
||
| 1459 | * @param string $event event name |
||
| 1460 | * @param callable $listener |
||
| 1461 | * @param int $priority optional priority, higher #s get called first |
||
| 1462 | */ |
||
| 1463 | public static function listen($event, callable $listener, $priority = 0) |
||
| 1467 | |||
| 1468 | /** |
||
| 1469 | * Adds a listener to the model.creating and model.updating events. |
||
| 1470 | * |
||
| 1471 | * @param callable $listener |
||
| 1472 | * @param int $priority |
||
| 1473 | */ |
||
| 1474 | public static function saving(callable $listener, $priority = 0) |
||
| 1479 | |||
| 1480 | /** |
||
| 1481 | * Adds a listener to the model.created and model.updated events. |
||
| 1482 | * |
||
| 1483 | * @param callable $listener |
||
| 1484 | * @param int $priority |
||
| 1485 | */ |
||
| 1486 | public static function saved(callable $listener, $priority = 0) |
||
| 1491 | |||
| 1492 | /** |
||
| 1493 | * Adds a listener to the model.creating event. |
||
| 1494 | * |
||
| 1495 | * @param callable $listener |
||
| 1496 | * @param int $priority |
||
| 1497 | */ |
||
| 1498 | public static function creating(callable $listener, $priority = 0) |
||
| 1502 | |||
| 1503 | /** |
||
| 1504 | * Adds a listener to the model.created event. |
||
| 1505 | * |
||
| 1506 | * @param callable $listener |
||
| 1507 | * @param int $priority |
||
| 1508 | */ |
||
| 1509 | public static function created(callable $listener, $priority = 0) |
||
| 1513 | |||
| 1514 | /** |
||
| 1515 | * Adds a listener to the model.updating event. |
||
| 1516 | * |
||
| 1517 | * @param callable $listener |
||
| 1518 | * @param int $priority |
||
| 1519 | */ |
||
| 1520 | public static function updating(callable $listener, $priority = 0) |
||
| 1524 | |||
| 1525 | /** |
||
| 1526 | * Adds a listener to the model.updated event. |
||
| 1527 | * |
||
| 1528 | * @param callable $listener |
||
| 1529 | * @param int $priority |
||
| 1530 | */ |
||
| 1531 | public static function updated(callable $listener, $priority = 0) |
||
| 1535 | |||
| 1536 | /** |
||
| 1537 | * Adds a listener to the model.deleting event. |
||
| 1538 | * |
||
| 1539 | * @param callable $listener |
||
| 1540 | * @param int $priority |
||
| 1541 | */ |
||
| 1542 | public static function deleting(callable $listener, $priority = 0) |
||
| 1546 | |||
| 1547 | /** |
||
| 1548 | * Adds a listener to the model.deleted event. |
||
| 1549 | * |
||
| 1550 | * @param callable $listener |
||
| 1551 | * @param int $priority |
||
| 1552 | */ |
||
| 1553 | public static function deleted(callable $listener, $priority = 0) |
||
| 1557 | |||
| 1558 | /** |
||
| 1559 | * Dispatches an event. |
||
| 1560 | * |
||
| 1561 | * @param string $eventName |
||
| 1562 | * |
||
| 1563 | * @return ModelEvent |
||
| 1564 | */ |
||
| 1565 | protected function dispatch($eventName) |
||
| 1571 | |||
| 1572 | /** |
||
| 1573 | * Dispatches the given event and checks if it was successful. |
||
| 1574 | * |
||
| 1575 | * @param string $eventName |
||
| 1576 | * |
||
| 1577 | * @return bool true if the events were successfully propagated |
||
| 1578 | */ |
||
| 1579 | private function handleDispatch($eventName) |
||
| 1585 | |||
| 1586 | ///////////////////////////// |
||
| 1587 | // Validation |
||
| 1588 | ///////////////////////////// |
||
| 1589 | |||
| 1590 | /** |
||
| 1591 | * Gets the error stack for this model. |
||
| 1592 | * |
||
| 1593 | * @return Errors |
||
| 1594 | */ |
||
| 1595 | public function getErrors() |
||
| 1605 | |||
| 1606 | /** |
||
| 1607 | * Checks if the model in its current state is valid. |
||
| 1608 | * |
||
| 1609 | * @return bool |
||
| 1610 | */ |
||
| 1611 | public function valid() |
||
| 1632 | |||
| 1633 | /** |
||
| 1634 | * Validates and marshals a value to storage. |
||
| 1635 | * |
||
| 1636 | * @param array $property |
||
| 1637 | * @param string $propertyName |
||
| 1638 | * @param mixed $value |
||
| 1639 | * |
||
| 1640 | * @return bool |
||
| 1641 | */ |
||
| 1642 | private function filterAndValidate(array $property, $propertyName, &$value) |
||
| 1662 | |||
| 1663 | /** |
||
| 1664 | * Validates a value for a property. |
||
| 1665 | * |
||
| 1666 | * @param array $property |
||
| 1667 | * @param string $propertyName |
||
| 1668 | * @param mixed $value |
||
| 1669 | * |
||
| 1670 | * @return array |
||
| 1671 | */ |
||
| 1672 | private function validateValue(array $property, $propertyName, $value) |
||
| 1695 | |||
| 1696 | /** |
||
| 1697 | * Checks if a value is unique for a property. |
||
| 1698 | * |
||
| 1699 | * @param array $property |
||
| 1700 | * @param string $propertyName |
||
| 1701 | * @param mixed $value |
||
| 1702 | * |
||
| 1703 | * @return bool |
||
| 1704 | */ |
||
| 1705 | private function checkUniqueness(array $property, $propertyName, $value) |
||
| 1720 | |||
| 1721 | /** |
||
| 1722 | * Gets the marshaled default value for a property (if set). |
||
| 1723 | * |
||
| 1724 | * @param string $property |
||
| 1725 | * |
||
| 1726 | * @return mixed |
||
| 1727 | */ |
||
| 1728 | private function getPropertyDefault(array $property) |
||
| 1732 | } |
||
| 1733 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.