Good day, experts. I recently started learning php and yii 1.1, but I’ve already started creating a web application to optimize my business processes. Of course, I face difficult moments for me. The next one got me into a dead end.
There are two tables. "Customers" and "Customer Numbers". This is done in order because customers can have one or many contact numbers. In the table of numbers "Customer numbers" there are two fields "phone" - PK and "clientId" - FK.
Models generated with gii. There are such connections:
In the "Customers" model:
'clientPhones' => array(self::HAS_MANY, 'ClientPhone', 'clientId')
In the "Customer Numbers" model:
'client' => array(self::BELONGS_TO, 'Client', 'clientId')
Task:
In the form of adding a client, one field should be displayed for entering the phone, as well as a button, when clicked, new fields for additional numbers will be added. Also need a button to remove the added field. All this business needs to be saved in a DB.
In the case of client editing, it is necessary to display the fields for each of the available phone numbers for this client with the ability to edit, add and delete individual phone number entries.
Thanks in advance for your help!
UPD: I found the solution to my problem after all, though collecting a lot of pieces of information from the network. It turned out such a hodgepodge.
ClientController.php controller:
public function actionCreate() { $model=new Client; $modelPhone = new ClientPhone; if(isset($_POST['Client'])) { $model->attributes=$_POST['Client']; if($model->save()) $this->redirect(array('view','id'=>$model->id)); } $this->render('create',array( 'model'=>$model, 'modelPhone'=>$modelPhone, )); } ... public function actionUpdate($id) { $model=$this->loadModel($id); $modelPhone=$this->loadModelsPhone($id); if(isset($_POST['Client'])) { if(isset($_POST['ClientPhone'])) { $this->savePhones($modelPhone); } $model->attributes=$_POST['Client']; if($model->save()) $this->redirect(array('view','id'=>$model->id)); } $this->render('update',array( 'model'=>$model, 'modelPhone'=>$modelPhone, )); } ... public function actionDeletePhone($id,$modelId) { if($this->loadModelPhoneToDelete($id)->delete()) $this->redirect(array('update','id'=>$modelId)); } ... public function loadModel($id) { $model=Client::model()->findByPk($id); if($model===null) throw new CHttpException(404,'The requested page does not exist.'); return $model; } public function loadModelsPhone($id) { return $model=ClientPhone::model()->findAllByAttributes(array('clientId'=>$id)); } public function loadModelPhoneToDelete($id) { return $model=ClientPhone::model()->findByPk($id); } public function savePhones($modelPhone) { if(isset($_POST['ClientPhone'])) { foreach($modelPhone as $i=>$phone) { if(isset($_POST['ClientPhone'])) $phone->phone = $_POST['ClientPhone'][$i]['phone']; $phone->save(false); } return true; }}
Form file _form.php:
<?php if ($model->isNewRecord): ?> <div class="row"> <?php echo $form->labelEx($modelPhone,'phone'); ?> <?php echo $form->textField($modelPhone,'phone[0]'); ?> <script> var count = 0; function addField() { $("#container").append('<div id="ttt' + (++count) + '"><input id="ClientPhone_phone" type="text" maxlength="20" name="ClientPhone[phone][' + count + ']"><input onclick="delField(' + count + '); return false;" type="button" value="Удалить" /><div>'); } function delField(counter) { $("#ttt" + counter).remove(); } </script> <div id="container"></div> <?php echo CHtml::Button('Добавить еще номер', array('onclick'=>'addField(); return false;')); ?> <?php echo $form->error($modelPhone,'phone'); ?> </div> <?php endif; ?> <?php if (!$model->isNewRecord): ?> <div class="row"> <?php foreach($modelPhone as $i=>$phone): ?> <?php echo $form->labelEx($phone,"[$i]phone"); ?> <?php echo $form->textField($phone,"[$i]phone") .CHtml::Button('X',array( 'submit'=>array('deletePhone','id'=>$phone->id,'modelId'=>$model->id), 'confirm'=>"Точно удалить номер телефона? Уверен?", )); ?> <?php echo $form->error($phone,"[$i]phone"); ?> <?php endforeach; ?> <script> var count = 0; function addField() { $("#container").append('<div id="ttt' + (++count) + '"><input id="ClientPhone_phone" type="text" maxlength="20" name="ClientPhone[phone][' + count + ']"><input onclick="delField(' + count + '); return false;" type="button" value="Удалить" /><div>'); } function delField(counter) { $("#ttt" + counter).remove(); } </script> <div id="container"></div> <?php echo CHtml::Button('Добавить еще номер', array('onclick'=>'addField(); return false;')); ?> </div> <?php endif; ?>
Model Client.php:
public function afterSave() { if(isset($_POST['ClientPhone']['phone'])) { $id = $this->id; $phones = $_POST['ClientPhone']['phone']; foreach($phones as $i=>$phone) { $modelPhone = new ClientPhone; $modelPhone->phone = $_POST['ClientPhone']['phone'][$i]; $modelPhone->clientId = $id; $modelPhone->save(); }} return parent::afterSave(); }