| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Spiral, Core Components | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Wolfy-J | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace Spiral\ORM\Entities; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Psr\SimpleCache\CacheInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Spiral\Core\Component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Spiral\Core\Traits\SaturateTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Spiral\Database\Builders\SelectQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Spiral\Models\EntityInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Spiral\ORM\Entities\Loaders\RootLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Spiral\ORM\Entities\Nodes\OutputNode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Spiral\ORM\Exceptions\SelectorException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Spiral\ORM\ORMInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Spiral\ORM\RecordInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Spiral\Pagination\PaginatorAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Spiral\Pagination\PaginatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * Attention, RecordSelector DOES NOT extends QueryBuilder but mocks it! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @method $this where(...$args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @method $this andWhere(...$args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @method $this orWhere(...$args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @method $this having(...$args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @method $this andHaving(...$args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @method $this orHaving(...$args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @method $this paginate($limit = 25, $page = 'page') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * @method $this orderBy($expression, $direction = 'ASC'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * @method int avg($identifier) Perform aggregation (AVG) based on column or expression value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * @method int min($identifier) Perform aggregation (MIN) based on column or expression value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  * @method int max($identifier) Perform aggregation (MAX) based on column or expression value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  * @method int sum($identifier) Perform aggregation (SUM) based on column or expression value. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | class RecordSelector extends Component implements \IteratorAggregate, \Countable, PaginatorAwareInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     use SaturateTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     private $class; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @invisible | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @var ORMInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     private $orm; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * @var RootLoader | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     private $loader; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @param string       $class | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @param ORMInterface $orm | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function __construct(string $class, ORMInterface $orm) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->class = $class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->orm = $orm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->loader = new RootLoader( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $orm->define($class, ORMInterface::R_SCHEMA), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $orm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * Get associated ORM instance, can be used to create separate query/selection using same | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * (nested) memory scope for ORM cache. | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @see ORM::selector() | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * @return ORMInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function getORM(): ORMInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $this->orm; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * Get associated class. | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function getClass(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return $this->class; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * Get alias used for primary table. | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function getAlias(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         return $this->loader->getAlias(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      * Columns to be selected, please note, primary will always be included, DO not include | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |      * column aliases in here, aliases will be added automatically. Creates selector as response. | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * @param array $columns | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      * @return RecordSelector | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     public function withColumns(array $columns): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $selector = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $selector->loader = $selector->loader->withColumns($columns); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return $selector; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * Request primary selector loader to pre-load relation name. Any type of loader can be used | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * for | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      * data preloading. ORM loaders by default will select the most efficient way to load related | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |      * data which might include additional select query or left join. Loaded data will | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      * automatically pre-populate record relations. You can specify nested relations using "." | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |      * separator. | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |      * Examples: | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |      * //Select users and load their comments (will cast 2 queries, HAS_MANY comments) | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      * User::find()->with('comments'); | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      * //You can load chain of relations - select user and load their comments and post related to | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      * //comment | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      * User::find()->with('comments.post'); | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |      * //We can also specify custom where conditions on data loading, let's load only public | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |      * comments. User::find()->load('comments', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |      *      'where' => ['{@}.status' => 'public'] | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |      * ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |      * Please note using "{@}" column name, this placeholder is required to prevent collisions and | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |      * it will be automatically replaced with valid table alias of pre-loaded comments table. | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |      * //In case where your loaded relation is MANY_TO_MANY you can also specify pivot table | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |      * conditions, | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |      * //let's pre-load all approved user tags, we can use same placeholder for pivot table alias | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |      * User::find()->load('tags', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |      *      'wherePivot' => ['{@}.approved' => true] | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |      * ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |      * //In most of cases you don't need to worry about how data was loaded, using external query | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |      * or | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |      * //left join, however if you want to change such behaviour you can force load method to | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |      * INLOAD | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |      * User::find()->load('tags', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |      *      'method'     => Loader::INLOAD, | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |      *      'wherePivot' => ['{@}.approved' => true] | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |      * ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |      * Attention, you will not be able to correctly paginate in this case and only ORM loaders | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |      * support different loading types. | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |      * You can specify multiple loaders using array as first argument. | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |      * Example: | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |      * User::find()->load(['posts', 'comments', 'profile']); | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |      * Attention, consider disabling entity map if you want to use recursive loading (i.e. | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |      * post.tags.posts), but first think why you even need recursive relation loading. | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |      * @see with() | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |      * @param string|array $relation | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |      * @param array        $options | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |      * @return $this|RecordSelector | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |     public function load($relation, array $options = []): self | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |         if (is_array($relation)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |             foreach ($relation as $name => $subOption) { | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |                 if (is_string($subOption)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |                     //Array of relation names | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |                     $this->load($subOption, $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  |                 } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 194 |  |  |                     //Multiple relations or relation with addition load options | 
            
                                                                        
                            
            
                                    
            
            
                | 195 |  |  |                     $this->load($name, $subOption + $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 196 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 197 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 199 |  |  |             return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 200 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 201 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 202 |  |  |         //We are requesting primary loaded to pre-load nested relation | 
            
                                                                        
                            
            
                                    
            
            
                | 203 |  |  |         $this->loader->loadRelation($relation, $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 205 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 206 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 208 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 209 |  |  |      * With method is very similar to load() one, except it will always include related data to | 
            
                                                                        
                            
            
                                    
            
            
                | 210 |  |  |      * parent query using INNER JOIN, this method can be applied only to ORM loaders and relations | 
            
                                                                        
                            
            
                                    
            
            
                | 211 |  |  |      * using same database as parent record. | 
            
                                                                        
                            
            
                                    
            
            
                | 212 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 213 |  |  |      * Method generally used to filter data based on some relation condition. | 
            
                                                                        
                            
            
                                    
            
            
                | 214 |  |  |      * Attention, with() method WILL NOT load relation data, it will only make it accessible in | 
            
                                                                        
                            
            
                                    
            
            
                | 215 |  |  |      * query. | 
            
                                                                        
                            
            
                                    
            
            
                | 216 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 217 |  |  |      * By default joined tables will be available in query based on relation name, you can change | 
            
                                                                        
                            
            
                                    
            
            
                | 218 |  |  |      * joined table alias using relation option "alias". | 
            
                                                                        
                            
            
                                    
            
            
                | 219 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 220 |  |  |      * Do not forget to set DISTINCT flag while including HAS_MANY and MANY_TO_MANY relations. In | 
            
                                                                        
                            
            
                                    
            
            
                | 221 |  |  |      * other scenario you will not able to paginate data well. | 
            
                                                                        
                            
            
                                    
            
            
                | 222 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 223 |  |  |      * Examples: | 
            
                                                                        
                            
            
                                    
            
            
                | 224 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 225 |  |  |      * //Find all users who have comments comments | 
            
                                                                        
                            
            
                                    
            
            
                | 226 |  |  |      * User::find()->with('comments'); | 
            
                                                                        
                            
            
                                    
            
            
                | 227 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 228 |  |  |      * //Find all users who have approved comments (we can use comments table alias in where | 
            
                                                                        
                            
            
                                    
            
            
                | 229 |  |  |      * statement). | 
            
                                                                        
                            
            
                                    
            
            
                | 230 |  |  |      * User::find()->with('comments')->where('comments.approved', true); | 
            
                                                                        
                            
            
                                    
            
            
                | 231 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 232 |  |  |      * //Find all users who have posts which have approved comments | 
            
                                                                        
                            
            
                                    
            
            
                | 233 |  |  |      * User::find()->with('posts.comments')->where('posts_comments.approved', true); | 
            
                                                                        
                            
            
                                    
            
            
                | 234 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 235 |  |  |      * //Custom join alias for post comments relation | 
            
                                                                        
                            
            
                                    
            
            
                | 236 |  |  |      * $user->with('posts.comments', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 237 |  |  |      *      'alias' => 'comments' | 
            
                                                                        
                            
            
                                    
            
            
                | 238 |  |  |      * ])->where('comments.approved', true); | 
            
                                                                        
                            
            
                                    
            
            
                | 239 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 240 |  |  |      * //If you joining MANY_TO_MANY relation you will be able to use pivot table used as relation | 
            
                                                                        
                            
            
                                    
            
            
                | 241 |  |  |      * name | 
            
                                                                        
                            
            
                                    
            
            
                | 242 |  |  |      * //plus "_pivot" postfix. Let's load all users with approved tags. | 
            
                                                                        
                            
            
                                    
            
            
                | 243 |  |  |      * $user->with('tags')->where('tags_pivot.approved', true); | 
            
                                                                        
                            
            
                                    
            
            
                | 244 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 245 |  |  |      * //You can also use custom alias for pivot table as well | 
            
                                                                        
                            
            
                                    
            
            
                | 246 |  |  |      * User::find()->with('tags', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 247 |  |  |      *      'pivotAlias' => 'tags_connection' | 
            
                                                                        
                            
            
                                    
            
            
                | 248 |  |  |      * ]) | 
            
                                                                        
                            
            
                                    
            
            
                | 249 |  |  |      * ->where('tags_connection.approved', false); | 
            
                                                                        
                            
            
                                    
            
            
                | 250 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 251 |  |  |      * You can safely combine with() and load() methods. | 
            
                                                                        
                            
            
                                    
            
            
                | 252 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 253 |  |  |      * //Load all users with approved comments and pre-load all their comments | 
            
                                                                        
                            
            
                                    
            
            
                | 254 |  |  |      * User::find()->with('comments')->where('comments.approved', true) | 
            
                                                                        
                            
            
                                    
            
            
                | 255 |  |  |      *             ->load('comments'); | 
            
                                                                        
                            
            
                                    
            
            
                | 256 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 257 |  |  |      * //You can also use custom conditions in this case, let's find all users with approved | 
            
                                                                        
                            
            
                                    
            
            
                | 258 |  |  |      * comments | 
            
                                                                        
                            
            
                                    
            
            
                | 259 |  |  |      * //and pre-load such approved comments | 
            
                                                                        
                            
            
                                    
            
            
                | 260 |  |  |      * User::find()->with('comments')->where('comments.approved', true) | 
            
                                                                        
                            
            
                                    
            
            
                | 261 |  |  |      *             ->load('comments', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 262 |  |  |      *                  'where' => ['{@}.approved' => true] | 
            
                                                                        
                            
            
                                    
            
            
                | 263 |  |  |      *              ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 264 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 265 |  |  |      * //As you might notice previous construction will create 2 queries, however we can simplify | 
            
                                                                        
                            
            
                                    
            
            
                | 266 |  |  |      * //this construction to use already joined table as source of data for relation via "using" | 
            
                                                                        
                            
            
                                    
            
            
                | 267 |  |  |      * //keyword | 
            
                                                                        
                            
            
                                    
            
            
                | 268 |  |  |      * User::find()->with('comments') | 
            
                                                                        
                            
            
                                    
            
            
                | 269 |  |  |      *             ->where('comments.approved', true) | 
            
                                                                        
                            
            
                                    
            
            
                | 270 |  |  |      *             ->load('comments', ['using' => 'comments']); | 
            
                                                                        
                            
            
                                    
            
            
                | 271 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 272 |  |  |      * //You will get only one query with INNER JOIN, to better understand this example let's use | 
            
                                                                        
                            
            
                                    
            
            
                | 273 |  |  |      * //custom alias for comments in with() method. | 
            
                                                                        
                            
            
                                    
            
            
                | 274 |  |  |      * User::find()->with('comments', ['alias' => 'commentsR']) | 
            
                                                                        
                            
            
                                    
            
            
                | 275 |  |  |      *             ->where('commentsR.approved', true) | 
            
                                                                        
                            
            
                                    
            
            
                | 276 |  |  |      *             ->load('comments', ['using' => 'commentsR']); | 
            
                                                                        
                            
            
                                    
            
            
                | 277 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 278 |  |  |      * @see load() | 
            
                                                                        
                            
            
                                    
            
            
                | 279 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 280 |  |  |      * @param string|array $relation | 
            
                                                                        
                            
            
                                    
            
            
                | 281 |  |  |      * @param array        $options | 
            
                                                                        
                            
            
                                    
            
            
                | 282 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 283 |  |  |      * @return $this|RecordSelector | 
            
                                                                        
                            
            
                                    
            
            
                | 284 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 285 |  |  |     public function with($relation, array $options = []): self | 
            
                                                                        
                            
            
                                    
            
            
                | 286 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 287 |  |  |         if (is_array($relation)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 288 |  |  |             foreach ($relation as $name => $options) { | 
            
                                                                        
                            
            
                                    
            
            
                | 289 |  |  |                 if (is_string($options)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 290 |  |  |                     //Array of relation names | 
            
                                                                        
                            
            
                                    
            
            
                | 291 |  |  |                     $this->with($options, []); | 
            
                                                                        
                            
            
                                    
            
            
                | 292 |  |  |                 } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 293 |  |  |                     //Multiple relations or relation with addition load options | 
            
                                                                        
                            
            
                                    
            
            
                | 294 |  |  |                     $this->with($name, $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 295 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 296 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 297 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 298 |  |  |             return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 299 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 300 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 301 |  |  |         //Requesting primary loader to join nested relation, will only work for ORM loaders | 
            
                                                                        
                            
            
                                    
            
            
                | 302 |  |  |         $this->loader->loadRelation($relation, $options, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 303 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 304 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 305 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 306 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 307 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 308 |  |  |      * Shortcut to where method to set AND condition for parent record primary key. | 
            
                                                                        
                            
            
                                    
            
            
                | 309 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 310 |  |  |      * @param string|int $id | 
            
                                                                        
                            
            
                                    
            
            
                | 311 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 312 |  |  |      * @return RecordSelector | 
            
                                                                        
                            
            
                                    
            
            
                | 313 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 314 |  |  |      * @throws SelectorException | 
            
                                                                        
                            
            
                                    
            
            
                | 315 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |     public function wherePK($id): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |         if (empty($this->loader->primaryKey())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |             //This MUST never happen due ORM requires PK now for every entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |             throw new SelectorException("Unable to set wherePK condition, no proper PK were defined"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |         //Adding condition to initial query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |         $this->loader->initialQuery()->where([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |             //Must be already aliased | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |             $this->loader->primaryKey() => $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 330 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 331 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 332 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 333 |  |  |      * Find one entity or return null. | 
            
                                                                        
                            
            
                                    
            
            
                | 334 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 335 |  |  |      * @param array|null $query | 
            
                                                                        
                            
            
                                    
            
            
                | 336 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 337 |  |  |      * @return EntityInterface|null | 
            
                                                                        
                            
            
                                    
            
            
                | 338 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |     public function findOne(array $query = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |         $data = (clone $this)->where($query)->fetchData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |         if (empty($data[0])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |         return $this->orm->make($this->class, $data[0], ORMInterface::STATE_LOADED, true); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 348 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 349 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 350 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 351 |  |  |      * Fetch all records in a form of array. | 
            
                                                                        
                            
            
                                    
            
            
                | 352 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 353 |  |  |      * @param string              $cacheKey | 
            
                                                                        
                            
            
                                    
            
            
                | 354 |  |  |      * @param int|\DateInterval   $ttl | 
            
                                                                        
                            
            
                                    
            
            
                | 355 |  |  |      * @param CacheInterface|null $cache Can be automatically resoled via ORM container scope. | 
            
                                                                        
                            
            
                                    
            
            
                | 356 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 357 |  |  |      * @return RecordInterface[] | 
            
                                                                        
                            
            
                                    
            
            
                | 358 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |     public function fetchAll( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |         string $cacheKey = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |         $ttl = 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |         CacheInterface $cache = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |     ): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |         return iterator_to_array($this->getIterator($cacheKey, $ttl, $cache)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 365 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 366 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 367 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 368 |  |  |      * Get RecordIterator (entity iterator) for a requested data. Provide cache key and lifetime in | 
            
                                                                        
                            
            
                                    
            
            
                | 369 |  |  |      * order to cache request data. | 
            
                                                                        
                            
            
                                    
            
            
                | 370 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 371 |  |  |      * @param string              $cacheKey | 
            
                                                                        
                            
            
                                    
            
            
                | 372 |  |  |      * @param int|\DateInterval   $ttl | 
            
                                                                        
                            
            
                                    
            
            
                | 373 |  |  |      * @param CacheInterface|null $cache Can be automatically resoled via ORM container scope. | 
            
                                                                        
                            
            
                                    
            
            
                | 374 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 375 |  |  |      * @return RecordIterator|RecordInterface[] | 
            
                                                                        
                            
            
                                    
            
            
                | 376 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |     public function getIterator( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |         string $cacheKey = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |         $ttl = 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |         CacheInterface $cache = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |     ): RecordIterator { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |         if (!empty($cacheKey)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |             /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |              * When no cache is provided saturate it using container scope | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |              * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |              * @var CacheInterface $cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |              */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |             $cache = $this->saturate($cache, CacheInterface::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |             if ($cache->has($cacheKey)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |                 $data = $cache->get($cacheKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |                 //Cache parsed tree with all sub queries executed! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |                 $cache->set($cacheKey, $data = $this->fetchData(), $ttl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |             $data = $this->fetchData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |         return new RecordIterator($data, $this->class, $this->orm); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 401 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 402 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 403 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 404 |  |  |      * Attention, column will be quoted by driver! | 
            
                                                                        
                            
            
                                    
            
            
                | 405 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 406 |  |  |      * @param string|null $column When column is null DISTINCT(PK) will be generated. | 
            
                                                                        
                            
            
                                    
            
            
                | 407 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 408 |  |  |      * @return int | 
            
                                                                        
                            
            
                                    
            
            
                | 409 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  |     public function count(string $column = null): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 |  |  |         if (is_null($column)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  |             if (!empty($this->loader->primaryKey())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 |  |  |                 //@tuneyourserver solves the issue with counting on queries with joins. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  |                 $column = "DISTINCT({$this->loader->primaryKey()})"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 |  |  |                 $column = '*'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 |  |  |         return $this->compiledQuery()->count($column); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 422 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 423 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 424 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 425 |  |  |      * Query used as basement for relation. | 
            
                                                                        
                            
            
                                    
            
            
                | 426 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 427 |  |  |      * @return SelectQuery | 
            
                                                                        
                            
            
                                    
            
            
                | 428 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 429 |  |  |     public function initialQuery(): SelectQuery | 
            
                                                                                                            
                            
            
                                    
            
            
                | 430 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 431 |  |  |         return $this->loader->initialQuery(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 432 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 433 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 434 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 435 |  |  |      * Get compiled version of SelectQuery, attentionly only first level query access is allowed. | 
            
                                                                        
                            
            
                                    
            
            
                | 436 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 437 |  |  |      * @return SelectQuery | 
            
                                                                        
                            
            
                                    
            
            
                | 438 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 439 |  |  |     public function compiledQuery(): SelectQuery | 
            
                                                                                                            
                            
            
                                    
            
            
                | 440 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 441 |  |  |         return $this->loader->compiledQuery(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 442 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 443 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 444 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 445 |  |  |      * Load data tree from databases and linked loaders in a form of array. | 
            
                                                                        
                            
            
                                    
            
            
                | 446 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 447 |  |  |      * @param OutputNode $node When empty node will be created automatically by root relation | 
            
                                                                        
                            
            
                                    
            
            
                | 448 |  |  |      *                         loader. | 
            
                                                                        
                            
            
                                    
            
            
                | 449 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 450 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 451 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 452 |  |  |     public function fetchData(OutputNode $node = null): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 |  |  |         /** @var OutputNode $node */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 |  |  |         $node = $node ?? $this->loader->createNode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  |         //Working with parser defined by loader itself | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 |  |  |         $this->loader->loadData($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 459 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 460 |  |  |         return $node->getResult(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 461 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 462 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 463 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 464 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 465 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 466 |  |  |     public function hasPaginator(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 467 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 468 |  |  |         return $this->loader->initialQuery()->hasPaginator(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 469 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 470 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 471 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 472 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 473 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 474 |  |  |     public function setPaginator(PaginatorInterface $paginator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 475 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 476 |  |  |         $this->loader->initialQuery()->setPaginator($paginator); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 477 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 478 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 479 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 480 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 481 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 482 |  |  |     public function getPaginator(bool $prepare = true): PaginatorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 483 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 484 |  |  |         return $this->loader->initialQuery()->getPaginator($prepare); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 485 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 486 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 487 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 488 |  |  |      * Bypassing call to primary select query. | 
            
                                                                        
                            
            
                                    
            
            
                | 489 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 490 |  |  |      * @param string $name | 
            
                                                                        
                            
            
                                    
            
            
                | 491 |  |  |      * @param        $arguments | 
            
                                                                        
                            
            
                                    
            
            
                | 492 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 493 |  |  |      * @return $this|mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 494 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 495 |  |  |     public function __call(string $name, array $arguments) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 496 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 497 |  |  |         if (in_array(strtoupper($name), ['AVG', 'MIN', 'MAX', 'SUM'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 498 |  |  |             //One of aggregation requests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 499 |  |  |             $result = call_user_func_array([$this->compiledQuery(), $name], $arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 500 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 501 |  |  |             //Where condition or statement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 502 |  |  |             $result = call_user_func_array([$this->loader->initialQuery(), $name], $arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 503 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 504 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 505 |  |  |         if ($result === $this->loader->initialQuery()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 506 |  |  |             return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 507 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 508 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 509 |  |  |         return $result; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 510 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 511 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 512 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 513 |  |  |      * Cloning with loader tree cloning. | 
            
                                                                        
                            
            
                                    
            
            
                | 514 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 515 |  |  |      * @attention at this moment binded query parameters would't be cloned! | 
            
                                                                        
                            
            
                                    
            
            
                | 516 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 517 |  |  |     public function __clone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 518 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 519 |  |  |         $this->loader = clone $this->loader; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 520 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 521 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 522 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 523 |  |  |      * Remove nested loaders and clean ORM link. | 
            
                                                                        
                            
            
                                    
            
            
                | 524 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 525 |  |  |     public function __destruct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 526 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 527 |  |  |         $this->orm = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 528 |  |  |         $this->loader = null; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 529 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 530 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 531 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 532 |  |  |      * @return \Interop\Container\ContainerInterface|null | 
            
                                                                        
                            
            
                                    
            
            
                | 533 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 534 |  |  |     protected function iocContainer() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 535 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 536 |  |  |         if ($this->orm instanceof Component) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 537 |  |  |             //Working inside ORM container scope | 
            
                                                                                                            
                            
            
                                    
            
            
                | 538 |  |  |             return $this->orm->iocContainer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 539 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 540 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 541 |  |  |         return parent::iocContainer(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 542 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 543 |  |  | } |