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:
| 1 | <?php |
||
| 39 | class RecordSelector extends AbstractSelect implements LoggerAwareInterface |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * Selector provides set of profiling functionality helps to understand what is going on with |
||
| 43 | * query and data parsing. |
||
| 44 | */ |
||
| 45 | use LoggerTrait, BenchmarkTrait, SaturateTrait; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Class name of record to be loaded. |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $class = ''; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Data columns are set of columns automatically created by inner loaders using |
||
| 56 | * generateColumns() method, this is not the same column set as one provided by user using |
||
| 57 | * columns() method. Do not define columns using generateColumns() method outside of loaders. |
||
| 58 | * |
||
| 59 | * @see generateColumns() |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $dataColumns = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * We have to track count of loader columns to define correct offsets. |
||
| 66 | * |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | protected $countColumns = 0; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Primary selection loader. |
||
| 73 | * |
||
| 74 | * @var Loader |
||
| 75 | */ |
||
| 76 | protected $loader = null; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @invisible |
||
| 80 | * @var ORM |
||
| 81 | */ |
||
| 82 | protected $orm = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param string $class |
||
| 86 | * @param ORM $orm |
||
| 87 | * @param Loader $loader |
||
| 88 | */ |
||
| 89 | public function __construct($class, ORM $orm = null, Loader $loader = null) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Primary selection table. |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function primaryTable() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Primary alias points to table related to parent record. |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function primaryAlias() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | public function columns($columns = ['*']) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Automatically generate set of columns for specified table or alias, method used by loaders |
||
| 143 | * in cases where data is joined. |
||
| 144 | * |
||
| 145 | * @param string $table Source table name or alias. |
||
| 146 | * @param array $columns Original set of record columns. |
||
| 147 | * @return int |
||
| 148 | */ |
||
| 149 | public function generateColumns($table, array $columns) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Request primary selector loader to pre-load relation name. Any type of loader can be used |
||
| 162 | * for |
||
| 163 | * data preloading. ORM loaders by default will select the most efficient way to load related |
||
| 164 | * data which might include additional select query or left join. Loaded data will |
||
| 165 | * automatically pre-populate record relations. You can specify nested relations using "." |
||
| 166 | * separator. |
||
| 167 | * |
||
| 168 | * Examples: |
||
| 169 | * |
||
| 170 | * //Select users and load their comments (will cast 2 queries, HAS_MANY comments) |
||
| 171 | * User::find()->with('comments'); |
||
| 172 | * |
||
| 173 | * //You can load chain of relations - select user and load their comments and post related to |
||
| 174 | * //comment |
||
| 175 | * User::find()->with('comments.post'); |
||
| 176 | * |
||
| 177 | * //We can also specify custom where conditions on data loading, let's load only public |
||
| 178 | * comments. User::find()->load('comments', [ |
||
| 179 | * 'where' => ['{@}.status' => 'public'] |
||
| 180 | * ]); |
||
| 181 | * |
||
| 182 | * Please note using "{@}" column name, this placeholder is required to prevent collisions and |
||
| 183 | * it will be automatically replaced with valid table alias of pre-loaded comments table. |
||
| 184 | * |
||
| 185 | * //In case where your loaded relation is MANY_TO_MANY you can also specify pivot table |
||
| 186 | * conditions, |
||
| 187 | * //let's pre-load all approved user tags, we can use same placeholder for pivot table alias |
||
| 188 | * User::find()->load('tags', [ |
||
| 189 | * 'wherePivot' => ['{@}.approved' => true] |
||
| 190 | * ]); |
||
| 191 | * |
||
| 192 | * //In most of cases you don't need to worry about how data was loaded, using external query |
||
| 193 | * or |
||
| 194 | * //left join, however if you want to change such behaviour you can force load method to |
||
| 195 | * INLOAD |
||
| 196 | * User::find()->load('tags', [ |
||
| 197 | * 'method' => Loader::INLOAD, |
||
| 198 | * 'wherePivot' => ['{@}.approved' => true] |
||
| 199 | * ]); |
||
| 200 | * |
||
| 201 | * Attention, you will not be able to correctly paginate in this case and only ORM loaders |
||
| 202 | * support different loading types. |
||
| 203 | * |
||
| 204 | * You can specify multiple loaders using array as first argument. |
||
| 205 | * |
||
| 206 | * Example: |
||
| 207 | * User::find()->load(['posts', 'comments', 'profile']); |
||
| 208 | * |
||
| 209 | * @see with() |
||
| 210 | * @param string $relation |
||
| 211 | * @param array $options |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | View Code Duplication | public function load($relation, array $options = []) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * With method is very similar to load() one, except it will always include related data to |
||
| 238 | * parent query using INNER JOIN, this method can be applied only to ORM loaders and relations |
||
| 239 | * using same database as parent record. |
||
| 240 | * |
||
| 241 | * Method generally used to filter data based on some relation condition. |
||
| 242 | * Attention, with() method WILL NOT load relation data, it will only make it accessible in |
||
| 243 | * query. |
||
| 244 | * |
||
| 245 | * By default joined tables will be available in query based on realtion name, you can change |
||
| 246 | * joined table alias using relation option "alias". |
||
| 247 | * |
||
| 248 | * Do not forget to set DISTINCT flag while including HAS_MANY and MANY_TO_MANY relations. In |
||
| 249 | * other scenario you will not able to paginate data well. |
||
| 250 | * |
||
| 251 | * Examples: |
||
| 252 | * |
||
| 253 | * //Find all users who have comments comments |
||
| 254 | * User::find()->with('comments'); |
||
| 255 | * |
||
| 256 | * //Find all users who have approved comments (we can use comments table alias in where |
||
| 257 | * statement). |
||
| 258 | * User::find()->with('comments')->where('comments.approved', true); |
||
| 259 | * |
||
| 260 | * //Find all users who have posts which have approved comments |
||
| 261 | * User::find()->with('posts.comments')->where('posts_comments.approved', true); |
||
| 262 | * |
||
| 263 | * //Custom join alias for post comments relation |
||
| 264 | * $user->with('posts.comments', [ |
||
| 265 | * 'alias' => 'comments' |
||
| 266 | * ])->where('comments.approved', true); |
||
| 267 | * |
||
| 268 | * //If you joining MANY_TO_MANY relation you will be able to use pivot table used as relation |
||
| 269 | * name |
||
| 270 | * //plus "_pivot" postfix. Let's load all users with approved tags. |
||
| 271 | * $user->with('tags')->where('tags_pivot.approved', true); |
||
| 272 | * |
||
| 273 | * //You can also use custom alias for pivot table as well |
||
| 274 | * User::find()->with('tags', [ |
||
| 275 | * 'pivotAlias' => 'tags_connection' |
||
| 276 | * ]) |
||
| 277 | * ->where('tags_connection.approved', false); |
||
| 278 | * |
||
| 279 | * You can safely combine with() and load() methods. |
||
| 280 | * |
||
| 281 | * //Load all users with approved comments and pre-load all their comments |
||
| 282 | * User::find()->with('comments')->where('comments.approved', true) |
||
| 283 | * ->load('comments'); |
||
| 284 | * |
||
| 285 | * //You can also use custom conditions in this case, let's find all users with approved |
||
| 286 | * comments |
||
| 287 | * //and pre-load such approved comments |
||
| 288 | * User::find()->with('comments')->where('comments.approved', true) |
||
| 289 | * ->load('comments', [ |
||
| 290 | * 'where' => ['{@}.approved' => true] |
||
| 291 | * ]); |
||
| 292 | * |
||
| 293 | * //As you might notice previous construction will create 2 queries, however we can simplify |
||
| 294 | * //this construction to use already joined table as source of data for relation via "using" |
||
| 295 | * //keyword |
||
| 296 | * User::find()->with('comments')->where('comments.approved', true) |
||
| 297 | * ->load('comments', ['using' => 'comments']); |
||
| 298 | * |
||
| 299 | * //You will get only one query with INNER JOIN, to better understand this example let's use |
||
| 300 | * //custom alias for comments in with() method. |
||
| 301 | * User::find()->with('comments', ['alias' => 'commentsR'])->where('commentsR.approved', true) |
||
| 302 | * ->load('comments', ['using' => 'commentsR']); |
||
| 303 | * |
||
| 304 | * @see load() |
||
| 305 | * @param string $relation |
||
| 306 | * @param array $options |
||
| 307 | * @return $this |
||
| 308 | */ |
||
| 309 | View Code Duplication | public function with($relation, array $options = []) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Fetch one record from database using it's primary key. You can use INLOAD and JOIN_ONLY |
||
| 333 | * loaders with HAS_MANY or MANY_TO_MANY relations with this method as no limit were used. |
||
| 334 | * |
||
| 335 | * @see findOne() |
||
| 336 | * @param mixed $id Primary key value. |
||
| 337 | * @return RecordEntity|null |
||
| 338 | * @throws SelectorException |
||
| 339 | */ |
||
| 340 | public function findByPK($id) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Fetch one record from database. Attention, LIMIT statement will be used, meaning you can not |
||
| 356 | * use loaders for HAS_MANY or MANY_TO_MANY relations with data inload (joins), use default |
||
| 357 | * loading method. |
||
| 358 | * |
||
| 359 | * @see findByPK() |
||
| 360 | * @param array $where Selection WHERE statement. |
||
| 361 | * @param bool $withLimit Use limit 1. |
||
| 362 | * @return RecordEntity|null |
||
| 363 | */ |
||
| 364 | public function findOne(array $where = [], $withLimit = true) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * {@inheritdoc} |
||
| 381 | */ |
||
| 382 | public function sqlStatement(QueryCompiler $compiler = null) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * {@inheritdoc} |
||
| 413 | * |
||
| 414 | * Return type will depend if custom columns set were used. |
||
| 415 | * |
||
| 416 | * @param array $callbacks Callbacks to be used in record iterator as magic methods. |
||
| 417 | * @return QueryResult|RecordIterator |
||
| 418 | */ |
||
| 419 | public function getIterator(array $callbacks = []) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * All records. |
||
| 441 | * |
||
| 442 | * @return RecordInterface[] |
||
| 443 | */ |
||
| 444 | public function all() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Execute query and every related query to compile records data in tree form - every relation |
||
| 451 | * data will be included as sub key. |
||
| 452 | * |
||
| 453 | * Attention, Selector will cache compiled data tree and not query itself to keep data integrity |
||
| 454 | * and to skip data compilation on second query. |
||
| 455 | * |
||
| 456 | * @return array |
||
| 457 | */ |
||
| 458 | public function fetchData() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Cloning selector presets |
||
| 518 | */ |
||
| 519 | public function __clone() |
||
| 523 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.