如何解决这个错误"未定义的变量:id"

时间:2015-05-10 13:32:21

标签: php mysql yii

我是Yii的新手。如何解决这个错误"未定义的变量:id"?

$adminid = adminuser::model()->findByPK($id)->id;
p($adminid);
echo $agent->id;

adminuser.php的代码: -

<?php

/**
 * This is the model class for table "adminuser".
 *
 * The followings are the available columns in table 'adminuser':
 * @property integer $id
 * @property string $Name
 * @property string $Email
 * @property string $Password
 * @property string $DateRegistered
 * @property integer $PermissionsLevel
 * @property integer $Active
 *
 * The followings are the available model relations:
 * @property Contactus[] $contactuses
 * @property Contactus[] $contactuses1
 */
class Adminuser extends CActiveRecord
{
/**
 * @return string the associated database table name
 */
public function tableName()
{
    return 'adminuser';
}

/**
 * @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('Email, PermissionsLevel', 'required'),
        array('PermissionsLevel, Active', 'numerical', 'integerOnly'=>true),
        array('Name, Email, Password', 'length', 'max'=>100),
        array('DateRegistered', 'safe'),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id, Name, Email, Password, DateRegistered, PermissionsLevel, Active', '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(
        'contactuses' => array(self::HAS_MANY, 'Contactus', 'AdminEmailID'),
        'contactuses1' => array(self::HAS_MANY, 'Contactus', 'AdminID'),
    );
}

/**
 * @return array customized attribute labels (name=>label)
 */
public function attributeLabels()
{
    return array(
        'id' => 'ID',
        'Name' => 'Name',
        'Email' => 'Email',
        'Password' => 'Password',
        'DateRegistered' => 'Date Registered',
        'PermissionsLevel' => 'Permissions Level',
        'Active' => 'Active',
    );
}

/**
 * Retrieves a list of models based on the current search/filter conditions.
 *
 * Typical usecase:
 * - Initialize the model fields with values from filter form.
 * - Execute this method to get CActiveDataProvider instance which will filter
 * models according to data in model fields.
 * - Pass data provider to CGridView, CListView or any similar widget.
 *
 * @return CActiveDataProvider the data provider that can return the models
 * based on the search/filter conditions.
 */
public function search()
{
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('Name',$this->Name,true);
    $criteria->compare('Email',$this->Email,true);
    $criteria->compare('Password',$this->Password,true);
    $criteria->compare('DateRegistered',$this->DateRegistered,true);
    $criteria->compare('PermissionsLevel',$this->PermissionsLevel);
    $criteria->compare('Active',$this->Active);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

/**
 * Returns the static model of the specified AR class.
 * Please note that you should have this exact method in all your CActiveRecord descendants!
 * @param string $className active record class name.
 * @return Adminuser the static model class
 */
public static function model($className=__CLASS__)
{
    return parent::model($className);
}

}

1 个答案:

答案 0 :(得分:0)

只需在模型$id之前使用isset()检查findByPk()变量即可。

if(isset($id) && $id>0)
{
  $adminid = adminuser::model()->findByPk($id);
  #check what it returns 
  echo '<pre>';
  print_r($adminid); 
  echo '</pre>';
 #then do your further process
 }else{
  echo "id is not exists";
}