yii gii的使用

       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'));
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dudochen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值