| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright  Copyright (c) Flipbox Digital Limited | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @link       https://github.com/flipboxfactory/craft-ember/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace flipbox\craft\ember\queries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use craft\base\ClonefixTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use craft\db\QueryAbortedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use flipbox\craft\ember\exceptions\RecordNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use flipbox\craft\ember\records\ActiveRecord; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use yii\db\Connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * This class provides Element Query like functionality to Yii's ActiveQuery.  Primarily, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * the query can throw a `QueryAbortedException` which is caught and handled appropriately. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author Flipbox Factory <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @since 2.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class ActiveQuery extends \yii\db\ActiveQuery | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     use ClonefixTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * Executes query and returns all results as an array.  If results are not found, an exception is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * thrown as we explicitly expect results. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param Connection $db the DB connection used to create the DB command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * If null, the DB connection returned by [[modelClass]] will be used. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @throws RecordNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function requireAll($db = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $records = $this->all($db); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if (empty($records)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             throw new RecordNotFoundException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                     "Records not found." | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return $records; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Executes query and returns a single result.  If a result is not found, an exception is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * thrown as we explicitly expect a result. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param Connection|null $db the DB connection used to create the DB command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * If `null`, the DB connection returned by [[modelClass]] will be used. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return ActiveRecord|array a single row of query result. Depending on the setting of [[asArray]], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * the query result may be either an array or an ActiveRecord object. `null` will be returned | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * if the query results in nothing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @throws RecordNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function requireOne($db = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if (null === ($record = $this->one($db))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             throw new RecordNotFoundException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     "Record not found." | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return $record; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function all($db = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             return parent::all($db); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } /** @noinspection PhpRedundantCatchClauseInspection */ catch (QueryAbortedException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     public function one($db = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         $limit = $this->limit; | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         $this->limit = 1; | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |             $result = parent::one($db); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         } /** @noinspection PhpRedundantCatchClauseInspection */ catch (QueryAbortedException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             $result = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         $this->limit = $limit; | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         return $result; | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function scalar($db = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $limit = $this->limit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $this->limit = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $result = parent::scalar($db); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } /** @noinspection PhpRedundantCatchClauseInspection */ catch (QueryAbortedException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $result = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $this->limit = $limit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     public function column($db = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             return parent::column($db); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } /** @noinspection PhpRedundantCatchClauseInspection */ catch (QueryAbortedException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function exists($db = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             return parent::exists($db); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } /** @noinspection PhpRedundantCatchClauseInspection */ catch (QueryAbortedException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     protected function queryScalar($selectExpression, $db) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             return parent::queryScalar($selectExpression, $db); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         } /** @noinspection PhpRedundantCatchClauseInspection */ catch (QueryAbortedException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 155 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |  |