yii的 方便以及高效让不少的开发者开始加入yii 项目的开发,我在使用yii的时候有个十分好用的一个脚手架gii。假设你配置好了配置文件。
配置的如下:
'modules'=>array(
// uncomment the following to enable the Gii tool
/**/
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'demo',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters'=>array('127.0.0.1','::1'),
),
//后台管理模块admin
'admin'=>array(
),
);
url 访问都方式:
http://localhost/index.php?r=gii/登陆新的Gii模块
http://127.0.0.1/yii/demos/test/index.php?r=Demoyii/list
http://127.0.0.1/yii/demos/test/index.php?r=test(列表)
http://127.0.0.1/yii/demos/test/index.php?r=test/admin(管理)
echo Yii::app()->getId(); 调用系统的方法
2 action 控制器的操作
public function actionList()
{
$user=new User();
$post=$user->find();
foreach($post as $key=>$value)
{
echo '<pre>';
print_r($key);
echo '</pre>';
}
}
<?php
/**
*This is the model class for table "tbl_user".
*
*The followings are the available columns in table 'tbl_user':
*@property integer $id
*@property string $username
*@property string $password
*@property string $salt
*@property string $email
*@property string $profile
*
*The followings are the available model relations:
*@property TblPost[] $tblPosts
*/
class User extends CActiveRecord
{
/**
* Returns the static model of the specified ARclass.
* @param string $className active record classname.
* @return User the static model class
*/
publicstatic function model($className=__CLASS__)
{
returnparent::model($className);
}
/**
* @return string the associated database tablename
*/
publicfunction tableName()
{
return'tbl_user';
}
/**
* @return array validation rules for modelattributes.
*/
publicfunction rules()
{
//NOTE: you should only define rules for those attributes that
//will receive user inputs.
returnarray(
array('username,password, salt, email', 'required'),
array('username,password, salt, email', 'length', 'max'=>128),
array('profile','safe'),
//The following rule is used by search().
//Please remove those attributes that should not be searched.
array('id,username, password, salt, email, profile', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
publicfunction relations()
{
//NOTE: you may need to adjust the relation name and the related
//class name for the relations automatically generated below.
returnarray(
'tblPosts'=> array(self::HAS_MANY, 'TblPost', 'author_id'),
);
}
/**
* @return array customized attribute labels(name=>label)
*/
publicfunction attributeLabels()
{
returnarray(
'id'=> 'ID',
'username'=> 'Username',
'password'=> 'Password',
'salt'=> 'Salt',
'email'=> 'Email',
'profile'=> 'Profile',
);
}
/**
* Retrieves a list of models based on thecurrent search/filter conditions.
* @return CActiveDataProvider the dataprovider that can return the models based on the search/filter conditions.
*/
publicfunction search()
{
//Warning: Please modify the following code to remove attributes that
//should not be searched.
$criteria=newCDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('username',$this->username,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('salt',$this->salt,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('profile',$this->profile,true);
returnnew CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
publicfunction all_list()
{
$this->_model=User::model()->find();
return $this->_model;
}
}
第二种代码的方式:
public function Actiondemo()
{
$sql='SELECT * FROM tbl_post';
$projects=Yii::app()->db->createCommand($sql)->queryAll();
print_r($projects);
}
关于查询的语句
public function getLastNewsByType($type_id = 1)
{
$type_id = (int)$type_id;
$results = News::model()->findAll('type_id = :type_id and status_id=1 order by create_timeDESC limit 0, 7',array(':type_id'=>$type_id));
return $results;
//$result = News::model()->findAll('type_id =:type_id and status_id order by create_timedesc limit 0,7',array(':type_id=>$type_id'));
}