class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe=false;
public function rules()
{
return array(
array('username,password','required','on'=>'login,register'),
array('email','required','on'=>'register'),
array('password','authenticate','on'=>'login'),
array('username','length','min'=>2,'max'=>12),
array('password','compare','compareAttribute'=>'password2','on'=>'register')
);
}
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$identity= new UserIdentify($this->username,$this->password);
if($identity->authenticate())
{
$duration=$this->rememberMe?3600*24*30:0;
Yii:app()->user->login($identity,$duration);
}else{
$this->addError('password','wrong password');
}
}
}
public function actionLogin()
{
$form=new LoginForm;
if(isset($_POST['LoginForm')))
{
$form->attributes=$_POST['LoginForm'];
if($form->validate())
{
$this->redirect(Yii:app()->user->returnUrl);
}
}
$this->render('login',array('user'=>$form);
}
}
<div class='yiiForm'>
<?php echo CHtml::beginForm();?>
<?php echo CHTML::errorSummary($user);?>
<div class="simple">
<?php echo CHtml::activeLabel($user,'username');?>
<?php echo CHtml::activeTextField($user,'username');?>
</div>
<div class="simple">
<?php echo CHtml::activeLabel($user,'password');?>
<?php echo CHtml::activePasswordField($user,'password');?>
</div>
<div class="simple">
<?php echo CHtml::activeChecBox($user,'rememberMe');?>
Remember me next time<br>
<?php echo CHtml::activeTextField($user,'username');?>
<?php echo CHtml::submitButton('login');?>
<?php echo CHtml::endForm();?>
</div>
$connection=new CDbConnection($dsn,$username,$password);
//open
$connection->active=true;
//close
$connection->active=false;
$dsn can be like those
1 SQLite: sqlite:/path/to/dbfile
2 MySQL: mysql:host=localhost;dbname=testdb
3 PostgreSQL: pgsql:host=localhost;port=5432;dbname=testdb
4 SQL Server: mssql:host=localhost;dbname=testdb
5 Oracle: oci:dbname=//localhost:1521/testdb
$command=$connection->createCommand($sql);
$rowCount=$command->execute();
$dataReader=$command->query();
$rows=$command->queryAll();
$row=$command->queryRow();
$column=$command->queryColumn();
$value=$command->queryScalar();
$dataReader=$command->query();
while(($row=$dataReader->read())!==false){
}
foreach($dataReader as $row{\
}
$transaction=$connection->beginTransaction();
try
{
$connection->createCommand($sql1)->execute();
$connection->createCommand($sql2)->execute();
//.... other SQL executions
$transaction->commit();
}
catch(Exception $e) // an exception is raised if a query fails
{
$transaction->rollBack();
}
$post=new Post;
$post->title="sample post";
$post->content='post body content';
$post->createTime=new CDbExpression('NOW()');
$post->save();
class Post extends CAciveRecord
{
public static function model($className=_CLASS_)
{
return parent:model($className);
}
}
$post=POST::model->findByPK/findBySql/findByAttributes/find
$posts=POST::model->findAllByPK/findAllBySql/findAllByAttributes/findAll
updateAll/updateByPk
$post=Post::model()->findByPK(10);
$post->delete();
Post::model()->deleteAll($condition,$params);
Post::model()->deleteByPK();
$n=Post::model()->count($condition,$params);
$n=POst::model()->countBySql($sql,$params);
$exists=POSt::model()->exists();
$criteria=new CDbCriteria;
$criteria->select='title';
$criteria->condtion='postID=:postID';
$criteria->params=array(':postID'=>10);
$post=Post::model()->findByPK(10);
$post->title='new post title';
$post->save();
$post=Post:model()->find($criteria);
$post=Post::model()->find(array(
'select'=>'title',
'condition'=>'postID=:postID',
'params'=>array(':postID'=>10),
));
class Post extends CActiveRecord
{
public function relations()
{
return array(
'author'=>array(self::BELONGS TO, 'User', 'authorID'),
'categories'=>array(self::MANY MANY, 'Category', 'PostCategory(postID, categoryID)'),
);
}
}
class User extends CActiveRecord
{
public function relations()
{
return array(
'posts'=>array(self::HAS MANY, 'Post', 'authorID'),
'profile'=>array(self::HAS ONE, 'Profile', 'ownerID'),
);
}
}
$posts=Post::model()->with('author')->findAll();
$posts=Post::model()->with('author','categories')->findAll();
$posts=Post::model()->with(
'author.profile',
'author.posts',
'categories')->findAll();
$posts=Post::model()->with('comments')->findAll(array(
'order'=>'Post.createTime, comments.createTime'
));