PuShaoWei /
SimpleEloquent
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Childish; |
||
| 3 | |||
| 4 | use Childish\database\DatabaseManager; |
||
| 5 | use Childish\database\ModelManager; |
||
| 6 | use Childish\support\Collection; |
||
| 7 | use Childish\support\Tools; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * ChildishModel |
||
| 11 | * |
||
| 12 | * @author Pu ShaoWei <[email protected]> |
||
| 13 | * @date 2017/12/7 |
||
| 14 | * @version 1.0 |
||
| 15 | */ |
||
| 16 | abstract class ChildishModel |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Indicates if the model should be timestamped. |
||
| 20 | * |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | public $timestamps = true; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The connection name for the model. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $connection; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The table associated with the model. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $table; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The primary key for the model. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $primaryKey = 'id'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The "type" of the auto-incrementing ID. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $keyType = 'int'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Indicates if the IDs are auto-incrementing. |
||
| 55 | * |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | public $incrementing = true; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Indicates if the model exists. |
||
| 62 | * |
||
| 63 | * @var bool |
||
| 64 | */ |
||
| 65 | public $exists = false; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The model attribute's original state. |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $original = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Indicates if the model was inserted during the current request lifecycle. |
||
| 76 | * |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | public $wasRecentlyCreated = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var \Childish\database\DatabaseManager |
||
| 83 | */ |
||
| 84 | protected static $resolver; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The array of booted models. |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected static $booted = []; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The model's attributes. |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected $attributes = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var string 是否被删除 |
||
| 102 | */ |
||
| 103 | const DELETED_AT = null; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Custom update |
||
| 107 | * |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | const DATE_FORMAT = 'Y-m-d H:i:s'; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The name of the "created at" column. |
||
| 114 | * |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | const CREATED_AT = 'created_at'; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * The name of the "updated at" column. |
||
| 121 | * |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | const UPDATED_AT = 'updated_at'; |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Clear the list of booted models so they will be re-booted. |
||
| 129 | * |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | public static function clearBootedModels() |
||
| 133 | { |
||
| 134 | static::$booted = []; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Create a new instance of the given model. |
||
| 139 | * |
||
| 140 | * @param array $attributes |
||
| 141 | * @param bool $exists |
||
| 142 | * @return static |
||
| 143 | */ |
||
| 144 | public function newInstance($attributes = [], $exists = false) |
||
| 145 | { |
||
| 146 | $model = new static((array)$attributes); |
||
| 147 | |||
| 148 | $model->exists = $exists; |
||
| 149 | |||
| 150 | $model->setConnection( |
||
| 151 | $this->getConnectionName() |
||
| 152 | ); |
||
| 153 | |||
| 154 | return $model; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Begin querying the model on the write connection. |
||
| 159 | * |
||
| 160 | * @return \Childish\query\Builder |
||
| 161 | */ |
||
| 162 | public static function onWriteConnection() |
||
| 163 | { |
||
| 164 | $instance = new static; |
||
| 165 | |||
| 166 | return $instance->newQuery()->useWritePdo(); |
||
|
0 ignored issues
–
show
|
|||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get all of the models from the database. |
||
| 171 | * |
||
| 172 | * @param array|mixed $columns |
||
| 173 | * @return \Childish\support\Collection|static[] |
||
| 174 | */ |
||
| 175 | public static function all($columns = ['*']) |
||
| 176 | { |
||
| 177 | return self::query()->get( |
||
| 178 | is_array($columns) ? $columns : func_get_args() |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Set the value of the "updated at" attribute. |
||
| 184 | * |
||
| 185 | * @param mixed $value |
||
| 186 | * @return $this |
||
| 187 | */ |
||
| 188 | public function setUpdatedAt($value) |
||
| 189 | { |
||
| 190 | $this->{static::UPDATED_AT} = $value; |
||
| 191 | |||
| 192 | return $this; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Set the value of the "created at" attribute. |
||
| 197 | * |
||
| 198 | * @param mixed $value |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function setCreatedAt($value) |
||
| 202 | { |
||
| 203 | $this->{static::CREATED_AT} = $value; |
||
| 204 | |||
| 205 | return $this; |
||
| 206 | } |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Begin querying the model. |
||
| 211 | * |
||
| 212 | * @return \Childish\query\Builder |
||
| 213 | */ |
||
| 214 | public static function query() |
||
| 215 | { |
||
| 216 | return (new static)->newQuery(); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * getConnection |
||
| 221 | * |
||
| 222 | * @static |
||
| 223 | * @return mixed |
||
| 224 | */ |
||
| 225 | public static function getTableConnection() |
||
| 226 | { |
||
| 227 | return (new static())->getConnection(); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get a new query builder for the model's table. |
||
| 232 | * |
||
| 233 | * @return \Childish\database\ModelManager |
||
| 234 | */ |
||
| 235 | public function newQuery() |
||
| 236 | { |
||
| 237 | $builder = new ModelManager($this->newBaseQueryBuilder()); |
||
| 238 | return $builder->setModel($this); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get a new query builder instance for the connection. |
||
| 243 | * |
||
| 244 | * @return \Childish\query\Builder |
||
| 245 | */ |
||
| 246 | protected function newBaseQueryBuilder() |
||
| 247 | { |
||
| 248 | return $this->getConnection()->table($this->getTable()); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Create a new Eloquent Collection instance. |
||
| 253 | * |
||
| 254 | * @param array $models |
||
| 255 | * @return \Childish\support\Collection |
||
| 256 | */ |
||
| 257 | public function newCollection(array $models = []) |
||
| 258 | { |
||
| 259 | return new Collection($models); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get the database connection for the model. |
||
| 264 | * |
||
| 265 | * @return \Childish\connection\Connection |
||
| 266 | */ |
||
| 267 | public function getConnection() |
||
| 268 | { |
||
| 269 | return static::resolveConnection($this->getConnectionName()); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get the current connection name for the model. |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public function getConnectionName() |
||
| 278 | { |
||
| 279 | return $this->connection; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Set the connection associated with the model. |
||
| 284 | * |
||
| 285 | * @param string $name |
||
| 286 | * @return $this |
||
| 287 | */ |
||
| 288 | public function setConnection($name) |
||
| 289 | { |
||
| 290 | $this->connection = $name; |
||
| 291 | |||
| 292 | return $this; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Resolve a connection instance. |
||
| 297 | * |
||
| 298 | * @param string|null $connection |
||
| 299 | * @return \Childish\connection\Connection |
||
| 300 | */ |
||
| 301 | public static function resolveConnection($connection = null) |
||
| 302 | { |
||
| 303 | return static::$resolver->connection($connection); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get the connection resolver instance. |
||
| 308 | * |
||
| 309 | * @return \Childish\database\DatabaseManager $resolver |
||
| 310 | */ |
||
| 311 | public static function getConnectionResolver() |
||
| 312 | { |
||
| 313 | return static::$resolver; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * et the connection resolver instance. |
||
| 318 | * |
||
| 319 | * @static |
||
| 320 | * @param \Childish\database\DatabaseManager $resolver |
||
| 321 | */ |
||
| 322 | public static function setConnectionResolver(DatabaseManager $resolver) |
||
| 323 | { |
||
| 324 | static::$resolver = $resolver; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Unset the connection resolver for models. |
||
| 329 | * |
||
| 330 | * @return void |
||
| 331 | */ |
||
| 332 | public static function unsetConnectionResolver() |
||
| 333 | { |
||
| 334 | static::$resolver = null; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get the table associated with the model. |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getTable() |
||
| 343 | { |
||
| 344 | if (!isset($this->table)) { |
||
| 345 | return str_replace('\\', '', Tools::snake(Tools::plural(Tools::Basename($this)))); |
||
| 346 | } |
||
| 347 | |||
| 348 | return $this->table; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Set the table associated with the model. |
||
| 353 | * |
||
| 354 | * @param string $table |
||
| 355 | * @return $this |
||
| 356 | */ |
||
| 357 | public function setTable($table) |
||
| 358 | { |
||
| 359 | $this->table = $table; |
||
| 360 | |||
| 361 | return $this; |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Get the name of the "created at" column. |
||
| 366 | * |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | public function getCreatedAtColumn() |
||
| 370 | { |
||
| 371 | return static::CREATED_AT; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Get the name of the "updated at" column. |
||
| 376 | * |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | public function getUpdatedAtColumn() |
||
| 380 | { |
||
| 381 | return static::UPDATED_AT; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Get a fresh timestamp for the model. |
||
| 386 | * |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | public function freshTimestampString() |
||
| 390 | { |
||
| 391 | if (false === static::DATE_FORMAT) { |
||
| 392 | return time(); |
||
| 393 | } |
||
| 394 | return date(static::DATE_FORMAT); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Create a new model instance that is existing. |
||
| 399 | * |
||
| 400 | * @param array $attributes |
||
| 401 | * @param string|null $connection |
||
| 402 | * @return static |
||
| 403 | */ |
||
| 404 | public function newFromBuilder($attributes = [], $connection = null) |
||
| 405 | { |
||
| 406 | $model = $this->newInstance([], true); |
||
| 407 | |||
| 408 | $model->attributes = (array)$attributes; |
||
| 409 | |||
| 410 | $model->setConnection($connection ? : $this->getConnectionName()); |
||
| 411 | |||
| 412 | return $model; |
||
| 413 | } |
||
| 414 | } |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: