| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * The MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright 2014-2018 James Ekow Abaka Ainooson | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * in the Software without restriction, including without limitation the rights | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * furnished to do so, subject to the following conditions: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * The above copyright notice and this permission notice shall be included in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * all copies or substantial portions of the Software. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * THE SOFTWARE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | namespace ntentan\nibii\relationships; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use ntentan\nibii\exceptions\FieldNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use ntentan\nibii\ORMContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use ntentan\nibii\Relationship; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use ntentan\utils\Text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * Represents a one to many belongs to relationship. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | class BelongsToRelationship extends Relationship | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $type = self::BELONGS_TO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     private $relatedData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |     public function doprepareQuery($data) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  |         if (!array_key_exists($this->options['local_key'], $data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             throw new FieldNotFoundException("Field {$this->options['local_key']} not found for belongs to relationship query."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         if (!isset($data[$this->options['local_key']])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 2 |  |         $query = $this->createQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |         if ($this->queryPrepared) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |             $query->setBoundData($this->options['foreign_key'], $data[$this->options['local_key']]); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 53 |  | View Code Duplication |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |             $query->setTable($this->getModelInstance()->getDBStoreInformation()['quoted_table']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |                 ->addFilter($this->options['foreign_key'], $data[$this->options['local_key']]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |                 ->setFirstOnly(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |             $this->queryPrepared = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |         return $query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |     public function runSetup() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 2 |  |         $model = ORMContext::getInstance()->getModelFactory()->createModel($this->options['model'], self::BELONGS_TO); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |         $table = $model->getDBStoreInformation()['table']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 2 |  |         if ($this->options['foreign_key'] == null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |             $this->options['foreign_key'] = $model->getDescription()->getPrimaryKey()[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |         if ($this->options['local_key'] == null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |             $this->options['local_key'] = Text::singularize($table).'_id'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function preSave(&$wrapper, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if(!$value->save()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $this->invalidFields = $value->getInvalidFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $wrapper[$this->options['local_key']] = $value[$this->options['foreign_key']]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         unset($wrapper[$this->options['model']]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |     public function postSave(&$wrapper) | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |         $wrapper[$this->options['model']] = $this->relatedData; | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  | 
            
                        
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.