Answer the question
In order to leave comments, you need to log in
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())
...
}
<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>
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();
// Типа через связь присвоили
$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 questionAsk a Question
731 491 924 answers to any question