E
E
EVOSandru62014-12-23 10:27:14
Yii
EVOSandru6, 2014-12-23 10:27:14

How to properly use beforeSave in Yii?

The situation is this, I have 2 tables:
-----------
t_service |
-----------
CODE |
name |
PRICE |
------------
and
--------------
mc_price |
-------------
CODE |
name |
--------------
Communication via Foreign Key : t_servce.PRICE = mc_price.CODE
BeforeSave works nicely , but else throws an error:if($this->isNewRecord)
CDbCommand failed to execute SQL query: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`visakaz`.`t_service`, CONSTRAINT `fk_t_service_mc_price1` FOREIGN KEY (`PRICE` ) REFERENCES `mc_price` (`CODE`) ON DELETE NO ACTION ON UPDATE NO ACTION). The SQL statement executed was: UPDATE `t_service` SET `CODE`=:yp0, `CODE_PAR`=:yp1, `LEVEL`=:yp2, `SHIFR`=:yp3, `NAME`=:yp4, `NAME_EN`= :yp5, `NAME_KZ`=:yp6, `NAME_DE`=:yp7, `NAME_CH`=:yp8, `NAME_TU`=:yp9, `NAME_AR`=:yp10, `DESCRIPTION`=:yp11, `DESCRIPTION_EN`=: yp12, `DESCRIPTION_KZ`=:yp13, `DESCRIPTION_DE`=:yp14, `DESCRIPTION_CH`=:yp15, `DESCRIPTION_TU`=:yp16, `DESCRIPTION_AR`=:yp17, `PHOTO`=:yp18, `PRICE`=:yp19 WHERE `t_service`.`CODE`=1 The
exception also refers to saving in the controller:

class ServiceController extends CController{
...
if($model->save())
...
}

In the form through the link, I display the price value for this service and, in order not to lose it, I store the price code in a hidden field.
<div class="row">
        <?php echo $form->labelEx($model,'PRICE_CODE'); ?>
        <?php echo $form->textField($model,'PRICE_CODE', array('type'=>'hidden', 'value'=>$model->PRICE)); ?>
        <?php echo $form->error($model,'PRICE_CODE'); ?>
    </div>

<div class="row">
  <?php echo $form->labelEx($model,'PRICE'); ?>
  <?php echo $form->textField($model,'PRICE', array('value'=>$model->price->NAME)); ?>
  <?php echo $form->error($model,'PRICE'); ?>
</div>

To do this, we had to create a PRICE_CODE property in the Service model.
As I understand it, the error is due to the fact that I have a connection between the tables and at the time of updating the t_service table they are trying to slip the actual price instead of the price code. This is absurd.
My considerations: 1. It is
necessary either to forbid updating t_service.PRICE
when updating 2. Try to update t_service.PRICE in beforeSave to the value from the hidden field, i. nothing will change and the connection will be correct.
How can such options be implemented? And also, is it possible to somehow do without a hidden field in the $PRICE_CODE formor is it possible to do without mentioning this property in the class at all by using some event that occurs after the update button is pressed and before $this->PRICE being the code for a field reference in the mc_price table becomes, for example, 500.50?
And after that in the method beforeSave() when the update occurs in the Service Model , I have the following code
$price = Price::model()->findByPk($this->PRICE_CODE);
$price->NAME = $this->PRICE;
$price->save();

An extra request hurts my eyes, is it possible to somehow save data through a connection, such as
// Типа через связь присвоили
$this->price->NAME = $this->PRICE;
// это строка заведомо неправильная, просто интересно, возможно ли сохранения таким образом проводить.
$price->save();

???

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question