简体   繁体   中英

Importing rules and data from different models in yii

How can i pull data from different tables(models) into view in yii . actually i done this with loadModel method. but my question is how can we import the rules into the view too. Here comes my scenario I have a User model and Profile model. The User model contains username and password and Profile model contains userid,name,address etc. so in my profile edit view i need all these data with the rules, username-unique,password,confirm pasword-required, etc., i can implement the required rule to all these, but i dunno how to import the table related rules like unique.

So basically this is a form that takes in two models and displays that data for you to edit and submit.

Simply make the render call to your view and pass both the models. eg $this->render('aview', array('model1'=>$model1, 'model2'=>$model2));

Get your view to display the form elements based upon these models. When you submit simply create new objects for respective models and populate them with the data received. eg

$model1 = new model1;
$model1=>id = id; //id received from the form submit. 
...............

Once you have the models populated you can call validate() on each of them to figure out if the data is according to your rules. If it is you proceed otherwise you display error. I hope this helps unless I missed something in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM