/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { if (CustomerType::model()->count('active=1') == 0) { throw new CHttpException(412, 'No hay tipos de clientes activos. Para crear un cliente, primero debe ' . CHtml::link('crear un tipo de cliente', array('customerType/create')) . '.'); } $model = new Customer(); $model->contact = new Contact(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Customer'])) { $model->attributes = $_POST['Customer']; $model->dependence_id = $model->dependence_id == '' ? NULL : $model->dependence_id; $model->contact->active = $model->active; if (isset($_POST['Contact'])) { $model->contact->attributes = $_POST['Contact']; $successful = $model->contact->validate(); } $successful = $model->validate() && $successful; if ($successful) { $transaction = Yii::app()->db->beginTransaction(); try { if ($model->save(false)) { $model->contact->customer_id = $model->id; if ($model->contact->save(false)) { $transaction->commit(); if (!empty($_POST['yt1'])) { Yii::app()->user->setFlash('customer-created', "¡El cliente <b><i>"{$model->name}"</i></b> fue creado exitosamente!"); $this->redirect(array('create')); } else { $this->redirect(array('view', 'id' => $model->id)); } } } $transaction->rollBack(); } catch (Exception $e) { $transaction->rollBack(); } } } $this->render('create', array('model' => $model)); }
echo $form->labelEx($model, 'name'); ?> <?php echo $form->textField($model, 'name', array('size' => 60, 'maxlength' => 100)); ?> <?php echo $form->error($model, 'name'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'customer_type_id'); ?> <?php echo $form->DropDownList($model, 'customer_type_id', CHtml::listData(CustomerType::model()->findAll(array('condition' => 'active = 1', 'order' => 'type')), 'id', 'type'), array('empty' => 'Seleccionar')); ?> <?php echo $form->error($model, 'customer_type_id'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'dependence_id'); ?> <?php echo $form->DropDownList($model, 'dependence_id', CHtml::listData(Dependence::model()->findAll(array('condition' => 'active = 1', 'order' => 'name')), 'id', 'name'), array('empty' => 'Seleccionar')); ?> <?php echo $form->error($model, 'dependence_id');
public static function getActiveCustomerTypes() { $customer_types = array('' => "Seleccionar"); $customer_types += CHtml::ListData(CustomerType::model()->findAll('t.active = 1'), 'id', 'type'); return $customer_types; }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = CustomerType::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }