how to operate DB using YII

本文介绍了一个基于Yii框架的登录表单模型类的设计,包括字段验证规则、身份认证流程及视图展示。此外,还详细展示了如何使用Yii框架进行数据库连接与操作,包括SQL执行、数据读取、事务管理等。
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'
));

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值