public function saveMeta(&$model) { $isNewMeta = !$model->id; if (!$isNewMeta) { $record = Seomatic_MetaRecord::model()->findById($model->id); /* $record = Seomatic_SettingsRecord::model()->findByAttributes(array( 'locale' => $locale, 'elementId' => $model->elementId, )); */ if (!$record) { throw new Exception(Craft::t('No meta exists with the ID “{id}”', array('id' => $model->id))); } else { /* -- If the locale of the saved record is different than the locale of our model, create a new record if ($record->locale != $model->locale) { $record = new Seomatic_MetaRecord(); $model->id = null; $isNewMeta = true; } */ } } else { $record = new Seomatic_MetaRecord(); } $record->setAttributes($model->getAttributes(), false); $assetId = !empty($model->seoImageId) ? $model->seoImageId[0] : null; $record->seoImageId = $assetId; $assetId = !empty($model->seoTwitterImageId) ? $model->seoTwitterImageId[0] : null; $record->seoTwitterImageId = $assetId; $assetId = !empty($model->seoFacebookImageId) ? $model->seoFacebookImageId[0] : null; $record->seoFacebookImageId = $assetId; if (!$record->validate()) { // Copy the record's errors over to the ad model $model->addErrors($record->getErrors()); // Might as well validate the content as well, // so we get a complete list of validation errors if (!craft()->content->validateContent($model)) { // Copy the content model's errors over to the ad model $model->addErrors($model->getContent()->getErrors()); } return false; } if (!$model->hasErrors()) { $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null; try { if (craft()->elements->saveElement($model)) { if ($isNewMeta) { $record->id = $model->id; $record->elementId = $model->id; } $record->save(false); if ($transaction !== null) { $transaction->commit(); } return true; } } catch (\Exception $e) { if ($transaction !== null) { $transaction->rollback(); } throw $e; } } }