Skip to content

Instantly share code, notes, and snippets.

@gms8994
Last active February 18, 2016 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gms8994/f65232109cd297453cb4 to your computer and use it in GitHub Desktop.
Save gms8994/f65232109cd297453cb4 to your computer and use it in GitHub Desktop.
$_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
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--
Copy link

tom-- commented Feb 18, 2016

public function actionMytest()
{
    Yii::trace('1');
    Vendor::find()->where(['name' => 'somgthing'])->one();
    Yii::trace('2');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment