Dynamically Change Value Of Cjuidatepicker; Save One Model, Update Another

Hello!

I am a newbie in Yii framework and also quite new to MVC programming so I am having an issue with an application that I’m working on.

I have two models OsnovnoSredstvo and Poseg. When I am doing new create in model Poseg I need to change date in model OsnovnoSredstvo. Now I have two problems:

  1. How can I dynamically change value of CJuiDatePicker with onchange of dropdown list?

_form for creating new Poseg:

The dropdownlist I’m changing.




<div class="row">

		<?php echo $form->labelEx($model,'Inventarna'); ?>

		<?php echo $form->dropDownList($model,'Inventarna',$model->getOsnovnoSredstvo(), array('empty'=>'Izberi osnovno sredstvo'));?>

		<?php echo $form->error($model,'Inventarna'); ?>

	</div>



The CJuiDatePicker that I want to change it’s value with the data from database




<div class="row">

		<?php echo $form->labelEx(OsnovnoSredstvo::model(),'NRedni'); ?>

		<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(

    		'model'=>OsnovnoSredstvo::model(),

			'name'=>'Model[test]',

			'value'=>'2013-07-07',

//			'attribute'=>'NRedni',

    		'options'=>array(

				'dateFormat'=>'yy-mm-dd',

				'changeMonth'=>true,

				'changeYear'=>true,		

        		'showAnim'=>'fold', // 'show' (the default), 'slideDown', 'fadeIn', 'fold'

        		'showOn'=>'button', // 'focus', 'button', 'both'

        		'buttonText'=>Yii::t('ui','Select form calendar'),

        		'buttonImage'=>Yii::app()->request->baseUrl.'/images/calendar.gif',

        		'buttonImageOnly'=>true,

    		),

    		'htmlOptions'=>array(

        		'style'=>'width:80px;vertical-align:top'

    		),

		)); ?>

		<?php echo $form->error(OsnovnoSredstvo::model(),'NRedni'); ?>

	</div>



  1. I have a problem when trying to save new poseg and update date in OsnovnoSredstvo. I am able to save data to Poseg, but I do not find a way to properly load model OsnovnoSredstvo and find the correct line in OsnovnoSredstvo

(findbypk($model->Inventarna))

and update the date NRedni to database. I have tried to change the actionCreate in controller Poseg or to add function afterSave in model Poseg. Looks like I have a problem with applying the right value of search paramerer Inventarna to either action or function.

I hope I wrote it down clearly and I hope my english is OK.

Thanks for any reply in advance. Maybe just with some suggestions of good examples, because I just don’t seem to find any that would make it clear to me.

Hi,


$model = new Poseg();

$model->date=$_POST['Poseg']['date'];

$model->save(false);



and update query


$update= OsnovnoSredstvo::model()->updateByPk($model->id,array('date'=>'$_POST['Poseg']['date']'));



hope it will help u.

Thank you for quick reply, but I do not know if I do not understand you or we missunderstood with what I need.

There is no date field in model Poseg that means that I can not save this field to Poseg. I want to update it in model OsnovnoSredstvo.

Here is how I thought this should be done, but not working:

actionCreate in PosegController:




public function actionCreate()

	{

		$model=new Poseg;

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);

		if(isset($_POST['Poseg']))

		{

			$model->attributes=$_POST['Poseg'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->idPoseg));	

		}

		$this->render('create',array(

			'model'=>$model,

		));

	}



function afterSave in model Poseg:




public function afterSave()

	{

		$modelOS=OsnovnoSredstvo::model()->updateByPk($model->Inventarna,array('NRedni'=>$model->'Model[test]'));

	}



Hi

u can insert the in Poseg id in table OsnovnoSredstvo?

There is a column Inventarna in table Poseg that is foreign key of table OsnovnoSredstvo if that is what you have in mind.

Hi,

ok so please change the


public function actionCreate()

        {

                $model=new Poseg;

                // Uncomment the following line if AJAX validation is needed

                // $this->performAjaxValidation($model);

                if(isset($_POST['Poseg']))

                {

                        $model->attributes=$_POST['Poseg'];

                        if($model->save())

//add line


                        $modelOS=OsnovnoSredstvo::model()->updateByPk($model->id,array('NRedni'=>$model->'Model[test]'));

        //

                                $this->redirect(array('view','id'=>$model->idPoseg));   

                }

                $this->render('create',array(

                        'model'=>$model,

                ));

        }