Call Update With Attribute

Hum . . . hum. . . . After fiddling around with no success I decided to ask what is the best way to do the following:

I have Person model , which has an id (PK auto increment) , passport number , first name, etc. . .

Now, I have a create form with 2 use cases:

  1. A new member is filling the info and sending - works.

  2. An already registered member is filling only the passport number , pressing a button or link , and is being redirected to the update form with all his info.

Obviously I need somewhere to find a Person model which matches the passport number , get it’s id and call the action update. Easy in words but I’m not sure:

  1. I couldn’t pass the passport number using cHtml link or button (I don’t want to submit the whole form , only the paassport number field)

  2. where should I write the transition between the number and the id? Should I create a new action in the controller? or can I do it in the form itself?

Thanks,

Mark.

Your model:




<?php

// protected/models/FindPersonForm.php

class FindPersonForm extends CFormModel {

   public $passport;

   public function rules() { return array(array("passport","required")); };

   public function personExists(){

    	return Person::model()->findByAttributes(array("passport"=>$this->passport));

   }

}



Your view:




 // protected/views/some/findperson.php

<?php 

	$form=$this->beginWidget('CActiveForm', array(

		'id'=>'find-person-form',

		'enableClientValidation'=>true,

		'clientOptions'=>array('validateOnSubmit'=>true,),

)); ?>

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

	<?php echo $form->textField($model,'passport'); ?>

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

	<?php echo CHtml::submitButton('Find'); ?>

<?php $this->endWidget(); ?>



Your controller:




<?php

class SomeController extends CController {


 public function actionFindPerson(){

   	$model = new FindPersonForm;

   	if(isset($_POST["FindPersonForm"])){

         	$model->attributes = $_POST["FindPersonForm"];

        	if($model->validate()){

             	if($person = $model->personExists()){

               	$this->redirect(array("update","id"=>$person->primarykey));

             	}else

             	$this->redirect(array("create","passport"=>$model->passport));

        	}

   	}

   	$this->render("findperson");

 }

 

}



How to call ?

echo CHtml::link("find a person",array("/some/findperson"));

Hello Bluyell, I’v created the model, controller and “some” folder with the view in it (all named as you’v written) , but when I redirect the url to some/findperson I see only white screen.

What am I missing?

Try having the SomeController class to extend from Controller instead of CController and see if that helps.

Tried. Still get white screen. . . .

Do you have error reporting enabled for php? There might be an error somewhere and its stopping execution.

Nope. Don’t know much about that.

Probably, that is what usually cause white screen.

I’v fiddled with the code , checked all the brackets , changed CController to Controller , changed the double dashes to single , but I still get the white screen.

Does it matte that there is no filter or access rules for the view in the controller? (I guess no , but still)

Is it enough to add those 3 files manually to yii , or I needed to do anything else?

Thanks ,

Mark.

I haven’t tested it but bluyell’s code should work just fine.

Try adding the following at the top of your index.php file and see if any errors pop up:

ini_set(‘display_errors’, 1);

error_reporting(E_ALL);

Oh , double facepalm. . . I hate such moments in code writing:

In the controller render didn’t passed a model create , thus i got the error “unknown model”. I suspected that it can be the problem , but when I copy-pasted it from another controller I took one extra bracket thus it didn’t worked.

Anyway , now it works perfectly!

bluyell - thanks for the great and neat solution! I didn’t knew about the existence of the CformModel before.

georaldc - thanks for the help. Do those 2 lines work in any page? Looks quite handy.

Cheers,

Mark.