onunix 2012-06-05 11:48:18 3744次浏览 10条回复 0 0 0

我希望在控制器中加入一段代码,

if(Model中有个属性叫abc)
(.....)
else
(.....)

Model中有个属性叫abc,怎么写, model是根据数据库表结构自动生成的,也就是数据库中有个字段叫abc,

  • 回复于 2012-06-05 12:47 举报
    $model = new Model();
    if(isset($model->abc))
    {
    ……
    }
    
  • 回复于 2012-06-05 13:31 举报

    hasAttribute

  • 回复于 2012-06-05 13:40 举报

    二楼。。。。。。顶

  • 回复于 2012-06-05 14:11 举报

    最常用的就是二楼的方法了。

  • 回复于 2012-06-05 15:05 举报

    没有起效,为什么没有起效呢?每次 isset($model->During) 都是返回 false,即 $model->During=date('Y-m-01',strtotime('-5 month'))没有执行,这是为什么呢?
    我的controller

    public function actionAdmin()
    {
        $model=new Test('search');
        $model->unsetAttributes();  // clear any default values
    		
        if(isset($model->During))
            $model->During=date('Y-m-01',strtotime('-5 month'));
    		
        if(isset($_GET['Test']))
            $model->attributes=$_GET['Test'];
    
        $this->render('admin',array(
            'model'=>$model,
        ));
    }
    

    我的model

    <?php
    
    /**
     * This is the model class for table "Test".
     *
     * The followings are the available columns in table 'Test':
     * @property integer $a
     * @property string $depOrProfitName
     * @property string $During
     */
    class Test extends CActiveRecord
    {
    	/**
    	 * Returns the static model of the specified AR class.
    	 * @param string $className active record class name.
    	 * @return Test the static model class
    	 */
    	public static function model($className=__CLASS__)
    	{
    		return parent::model($className);
    	}
    
    	/**
    	 * @return string the associated database table name
    	 */
    	public function tableName()
    	{
    		return 'Test';
    	}
    
    	/**
    	 * @return array validation rules for model attributes.
    	 */
    	public function rules()
    	{
    		// NOTE: you should only define rules for those attributes that
    		// will receive user inputs.
    		return array(
    			array('depOrProfitName', 'length', 'max'=>500),
    			array('During', 'safe'),
    			// The following rule is used by search().
    			// Please remove those attributes that should not be searched.
    			array('a, depOrProfitName, During', 'safe', 'on'=>'search'),
    		);
    	}
    
    	/**
    	 * @return array relational rules.
    	 */
    	public function relations()
    	{
    		// NOTE: you may need to adjust the relation name and the related
    		// class name for the relations automatically generated below.
    		return array(
    		);
    	}
    
    	/**
    	 * @return array customized attribute labels (name=>label)
    	 */
    	public function attributeLabels()
    	{
    		return array(
    			'a' => 'A',
    			'depOrProfitName' => 'Dep Or Profit Name',
    			'During' => 'During',
    		);
    	}
    
    	/**
    	 * 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('a',$this->a);
    		$criteria->compare('depOrProfitName',$this->depOrProfitName,true);
    		$criteria->compare('During',$this->During,true);
    
     		return new CActiveDataProvider($this, array(
    			'criteria'=>$criteria,
    		));
    	}
    }
    
  • 回复于 2012-06-05 15:34 举报

    呵呵,首先

    if(isset($model->During))
        $model->During=date('Y-m-01',strtotime('-5 month'));
    

    这段代码 if里返回为false了,if 中的代码肯定不会执行,
    其次,$model->During=date('Y-m-01',strtotime('-5 month')); 想给属性附值,属性必须声明。

  • 回复于 2012-06-05 15:41 举报

    你可以这样写:

    if(!$model->hasAttribute('During'))
    {
        $value = date('Y-m-01',strtotime('-5 month'));
        $model-> setAttribute('During',$value);
    }
    
  • 回复于 2012-06-05 16:27 举报

    应该去掉其中的 !号,改为

    if($model->hasAttribute('During'))
    {
        .....
    }
    

    就OK 了,只要数据库表中有这个During字段,就会用During去过滤。没有During字段,也不会出问题。 我要的就是这种效果。

  • 回复于 2012-06-05 16:37 举报

    噢,那我理解错你的意思了,呵呵

  • 回复于 2012-06-06 13:29 举报
    hasAttribute
    getAttribute
    setAttribute
    

    都是AR的方法。得记下来。

您需要登录后才可以回复。登录 | 立即注册