Showing posts with label basics. Show all posts
Showing posts with label basics. Show all posts

Saturday, October 15, 2011

dropdown yii



lets consider two tables Employee and Department, Employee column field Department-id refers to Department etc.. and you want to display a dropdrop rather than textfield in the create view. 


protected/views/Department/_form.php


beginWidget('CActiveForm', array(
'id'=>'employee-form',
'enableAjaxValidation'=>false,
)); ?>
Fields with * are required.
errorSummary($model); ?>

labelEx($model,'departmentId'); ?>
textField($model,'departmentId'); ?>
                dropDownList($model,'departmentId', CHtml::listData(TestDepartment::model()->findAll(), 'id', 'departmentName')); ?>
error($model,'departmentId'); ?>

labelEx($model,'firstName'); ?>
textField($model,'firstName',array('size'=>20,'maxlength'=>20)); ?>
error($model,'firstName'); ?>

labelEx($model,'lastName'); ?>
textField($model,'lastName',array('size'=>40,'maxlength'=>40)); ?>
error($model,'lastName'); ?>

labelEx($model,'email'); ?>
textField($model,'email',array('size'=>60,'maxlength'=>60)); ?>
error($model,'email'); ?>

labelEx($model,'ext'); ?>
textField($model,'ext'); ?>
error($model,'ext'); ?>

labelEx($model,'hireDate'); ?>
textField($model,'hireDate'); ?>
error($model,'hireDate'); ?>

labelEx($model,'leaveDate'); ?>
textField($model,'leaveDate'); ?>
error($model,'leaveDate'); ?>

isNewRecord ? 'Create' : 'Save'); ?>

endWidget(); ?>


Show more records per page Yii

to display more records in CGrid in Yii simply add one line to your model :)


protected/models/Product.php


/*** Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.*/


public function search()
{
// Warning: Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;


$criteria->compare('Product_Code',$this->Product_Code,true);
$criteria->compare('Name',$this->Name,true);
$criteria->compare('Short_Description',$this->Short_Description,true);



return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,  
                'pagination'=>array('pageSize'=>20,), //framework/web/CPagination.php
));
}

Thursday, October 13, 2011

Yii change login password change

protected/components/UserIdentity.php


/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
/**
* Authenticates a user.
* The example implementation makes sure if the username and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
* against some persistent user identity storage (e.g. database).
* @return boolean whether authentication succeeds.
*/
public function authenticate()
{
$users=array(
// username => password
'demo'=>'demo',
'admin'=>'admin',
);
if(!isset($users[$this->username]))
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($users[$this->username]!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;
}
}

About

Blogger templates