yii的一些方法的解析和blog的详细解析(二)
转载自:http://www.yiichina.org/forum/thread-218-1-1.html
--------------------------------------------------------------------------- UserIdentity.php --------------------------------------------------------------------------- class UserIdentity extends CUserIdentity{} 进行对数据库的校验密码且复制给$this->_id=$user->id; 表示用户状态为登陆 注: 任何登陆的编程都要继承此文件,单独创建文件即使继承的类都一样,但yii是不认可的 $identity=new UserIdentity($forms->email,'ethos'); $identity->authenticate(); Yii::app()->user->login($identity); --------------------------------------------------------------------------- LoginForm.php --------------------------------------------------------------------------- 这些在models下用来定义文本框符合各种条件 class LoginForm extends CFormModel{ rules() attributeLabels() authenticate() } function rules(){ array('username', 'email'),//调用js认证 array('password', 'authenticate'),//调用下一个函数 } --------------------------------------------------------------------------- login.php --------------------------------------------------------------------------- 1. //这样给form增加其他属性 <?php echo CHtml::beginForm('',$method='post',array("id"=>"signupForm")); ?> //给其他文本组件增加属性触发js函数 <?php echo CHtml::activeTextField($form,'username',array("class"=>"required","minlength"=>"3")) ?> <?php echo CHtml::submitButton('Login',array ("onclick"=>"testok()")); ?> //testok() 为js函数 <?php echo CHtml::submitButton('Login',array ("onclick"=>"alert(\"sam\");return false;")); ?> <?php echo CHtml::activeLabel($post,$post->label); ?> <?php echo CHtml::activeTextField($post,'label',array('size'=>65,'maxlength'=>128)); ?> <?php echo CHtml::activeTextField($post,'content',array('rows'=>20, 'cols'=>50)); ?> label或者 content是当前表的列名字这个很重要相当于显示数据对象中的某个属性 可以输出数据库的值 <?php foreach($post as $n=>$model): ?> <?php echo CHtml::activeLabel($model,'label'); ?> <?php echo CHtml::textField('firstname','',array("class"=>"required","minlength"=>"3")) ?> <?php endforeach; ?> 注:若需要从数据库传值到input组件需要用CHtml::textField 仅仅是需要录入信息,且后台取得用这个就行 CHtml::textField('username','',array("class"=>"required","minlength"=>"3")); 2. 关于用JQuery验证文本框的改写说明 用jquery的validate插件控制文本框输入格式(http://bassistance.de/jquery-plugins/jquery-plugin-validation/) js中要写 signupForm 为表单id $().ready(function() { // validate the comment form when it is submitted $("#commentForm").validate(); // validate signup form on keyup and submit $("#signupForm").validate({ }); }); php中要添加 要求此字段的输入要求 <?php echo CHtml::activeTextField($form,'username',array("class"=>"required","minlength"=>"3")) ?> 注:jquery.validate.js中 defaults: { messages: {}, groups: {}, rules: {}, errorClass: "errorjs", 原来是error现在时errorjs 不然与yii的错误提示冲突 ===================================================================================== 用rule 存数据库 http://www.yiiframework.com/doc/guide/database.arr 当 数据库的php public function rules() { return array( array('email','length','max'=>40), array('pass','length','max'=>255), array('role', 'required'), array('role, overall_percent, overall_time', 'numerical', 'integerOnly'=>true), ); }在controller时候需要 在rule的required的时候 都需要赋值。 overall_percent,overall_time遵循 role的原则 $profile = new profile; $profile->email=$_POST['email']; $profile->firstname=$_POST['firstname']; $profile->role=0; $profile->overall_time=0; $profile->overall_percent=0; $zipcode = $this->zipPlace($_POST['zipcode']); echo $zipcode->state; var_dump($profile->save()); 注 当一个表存储4次的时候,需要创建4个handle new4次-------------------------------------------------------------------------------------------------- 在登陆验证时候增加额外项给Yii::app()->user->lastTime 在UserIdentity.php中 class UserIdentity extends CUserIdentity { $this->setState('lastTime',$user->lastTime); } 前台就可以调用Yii::app()->user->lastTime 只有这样添加的新值,才能在程序中这样任意重复赋值。默认的值不可,比如Yii::app()->user->username Yii::app()->user->lastTime='1'; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ $form->validate()如何起作用的说明 1. StartForm.php中 我们定义了各个文本框的 rules() 2. 需要在controller中调用, 先声明这个文本框 $forms=new StartForm; // 要把回传函数这些属性给$forms这个对象 $forms->firstname=$_POST['firstname']; $forms->firstname=$_POST['email']; $forms->firstname=$_POST['zipcode']; $forms->firstname=$_POST['perstatus']; //调用php端的文本校验 if($forms->validate()){ XXXXXX } 3. 同理为LoginForm $form=new LoginForm; if(isset($_POST['LoginForm'])) { $form->attributes=$_POST['LoginForm'];//回传函数这些属性给form if($form->validate()){ } } 4. 当进入到validate的时候我们仍然需要增加逻辑来确认每个文本框需要认证的属性 ----------------------------------------------------------------------------------------------------------------------------------------------------- yii的session可以这样设置 1. 程序中可以这样调用 Yii::app()->session[$var]=$value; 2. 若main.php 定义后 'session' => array( 'class' => 'system.web.CDbHttpSession', 'connectionID' => 'db', ), 当运行第一次网站时候系统会建立yiisession的表,session自动存储在库的yiisession表内 3. 而库连接可以这样 'db'=>array( // 'connectionString'=>'Your DSN', 'connectionString'=>'mysql:host=localhost;dbname=testnewtax', 'username'=>'root', 'password'=>'adminzhao', ), 4. 在main.php中 增加参数文件 params.php (这个文件是与main.php平行结构 放到文件夹中) 'params'=>require(dirname(__FILE__).'/params.php'), 在程序里可以这样引用 ---------------------------------------------------------------------------------------- yii 返回的地址也可以这样写 1. Yii::app()->user->returnUrl = Yii::app()->getBaseUrl()."/step/show/id/1"; $this->redirect(Yii::app()->user->returnUrl); 2. $this->redirect(array('step/show','id'=>1)); 3. $this->render('index',array('post'=>$questions)); 4. $this->renderPartial('field_show',array('field'=>$field,'key'=>++$key,)); 注意:有的时候$this->render、$this->redirect在同一个action中应用,程序会报错原因如下 1.当我们创建新的form的时候 我们应用2中方法, 1》FillForm.php class FillForm extends CFormModel 这样可以把这个表单中每个项目用php进行核查是否符合规则,在FillForm.php public function rules()中创建规则 2》直接在继承数据库表时候创建表单的核查规则 Post.php (post是库中的表)class Post extends CActiveRecord 在Post.php 中 public function rules() 中创建规则 2. 在继承CFormModel类后同一个action中就不能同时出现$this->render、$this->redirect,否则会报错 变通办法如下在controller中创建2个action对应一个form public function actionFill() { $form=new FillForm; $this->render('fill',array('form'=>$form)); } public function actionUpdatePass() { if(isset($_POST['password'])) { //XXX $this->redirect(array('step/show','id'=>1)); } else $this->refresh(); } } 在tpl中设置 <?php echo CHtml::beginForm('UpdatePass',$method='post',array("id"=>"fillForm")); ?> ---------------------------------------------------------------------------------------- 弹出窗口 目前有个问题就是弹出的窗口路径是相对于现在的controller的无法调到另外路径下 1. 在ptl文件中 输入 <?php echo CHtml::linkButton('popup',array ("onclick"=>"startAlert()")); ?> 2. js文件 function startAlert(){ window.open('fillpass'); } 3. sitecontroller.php public function actionFillpass(){ $this->render('fillpass'); } 4. 创建fillpass的tpl themes/views/site/fillpass.php -------------------------------------------------------------------------------------------------------------------------------------------------------- 删除按钮 带确认项 --------------------------------------------------------------------------------------------------------------------------------------------------------- 1. 在tpl中 <?php echo CHtml::Button('cancel', array ( 'submit' => 'DeleteUser', 'params' => '', 'confirm'=>'Are you sure?' )) ?> |
-------------我的签名档---------------------
年轻人,还需要多努力!
--------------------------------------------