Last active
          February 18, 2016 15:27 
        
      - 
      
- 
        Save gms8994/f65232109cd297453cb4 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | $_vendorModel = $this->instantiate('Vendor')->find()->where(['name' => $vendorModel->name])->one(); | |
| if (is_null($_vendorModel)) { | |
| $_vendorModel = $this->instantiate('Vendor'); | |
| $_vendorModel->name = $vendorModel->name; | |
| $success = $_vendorModel->save(); | |
| $dirtyAttributes = $itemVendorModel->dirtyAttributes; | |
| $dirtyAttributes = array_filter($dirtyAttributes, function($v) { return $v !== ""; }); | |
| if (! $success | |
| && count($dirtyAttributes) > 0 | |
| ) { | |
| $message = "Item {$model->name} saved without vendor attributes because of errors with vendor attributes"; | |
| if (isset($warnings[$lineNum])) { | |
| $warnings[$lineNum] = [$warnings[$lineNum], $message]; | |
| } else { | |
| $warnings[$lineNum] = $message; | |
| } | |
| } | |
| $vendorModel = $_vendorModel; | |
| } | |
| // This generates the following queries | |
| /* | |
| SELECT * FROM `vendor` WHERE `name`='' ORDER BY `name` | |
| SELECT * FROM `vendor_contact` WHERE `vendor_id`='6f546ed5-76c3-4bc7-9531-c869bcc5337b' | |
| SELECT * FROM `contact` WHERE (`status`='1') AND (0=1) | |
| SELECT * FROM `vendor_address` WHERE `vendor_id`='6f546ed5-76c3-4bc7-9531-c869bcc5337b' | |
| SELECT * FROM `address` WHERE (`status`='1') AND (0=1) | |
| SELECT COUNT(*) FROM `vendor` WHERE `id`='6f546ed5-76c3-4bc7-9531-c869bcc5337b' ORDER BY `name` | |
| */ | |
| // 6f546ed5-76c3-4bc7-9531-c869bcc5337b is not a UUID used anywhere in my database | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public function rules() | |
| { | |
| return [ | |
| ['id', 'default', 'value' => Uuid::generate()], | |
| [['name'], 'required'], | |
| [['created_at', 'updated_at', 'code', 'website', 'duns', 'terms_id', 'created_by', 'updated_at', 'created_at', | |
| 'contacts', 'address', 'updated_by'], 'safe'], | |
| [['name'], 'string', 'max' => 64], | |
| [['name'], 'unique'], | |
| ['status', 'default', 'value' => 1], | |
| [['fax_number', 'account_number', 'quickbooks_list_id'], 'string'], | |
| [['quickbooks_last_sync'], 'number'] | |
| ]; | |
| } | |
| public function fields() | |
| { | |
| return array_merge(parent::fields(), ['contacts', 'attachments', 'notes', 'address']); | |
| } | |
| public function getContacts() | |
| { | |
| return $this->hasMany(Contact::className(), ['id' => 'contact_id']) | |
| ->viaTable('{{%vendor_contact}}', ['vendor_id' => 'id']); | |
| } | |
| public function getAddress() | |
| { | |
| return $this->hasOne(Address::className(), ['id' => 'address_id']) | |
| ->viaTable('{{%vendor_address}}', ['vendor_id' => 'id']); | |
| } | 
        
      
            tom--
  
      
      
      commented 
        Feb 18, 2016 
      
    
  
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment