lionel1022 2012-12-05 17:45:22 4340次浏览 0条回复 0 0 0

Controller:

$params = array('application_model_id'=>2, 'manager_group_id'=>6, 'member_user_id'=>77);
			$model = CustomApp::model();
			$total = $model->getCustAppNum($params);
			if ($total == 0) {
				$model->application_model_id = 2;
				$model->manager_group_id = 6;
				$model->member_user_id = 77;
				$state = $model->insert();
			} else {
				$state = $model->deleteAllByAttributes($params);
			}

model:

class CustomApp extends CActiveRecord
{
	/**
	 * 初始化数据库连接
	 *
	 * @deprecated 暂无详细描述
	 * @return void
	 * @throws Exception
	 * @access public
	 */
	public function __construct ()
	{
		self::$db = Yii::app()->ocourt;
	}	
		
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CustomApp 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 'custom_app';
	}

	/**
	 * @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('application_model_id, member_user_id, manager_group_id', 'required'),
			array('application_model_id, member_user_id, manager_group_id', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('custom_app_id, application_model_id, member_user_id, manager_group_id', '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(
			'applicationModel' => array(self::BELONGS_TO, 'ApplicationModel', 'application_model_id'),
			'memberUser' => array(self::BELONGS_TO, 'ManagerUser', 'member_user_id'),
			'managerGroup' => array(self::BELONGS_TO, 'ManagerGroup', 'manager_group_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'custom_app_id' => 'Custom App',
			'application_model_id' => 'Application Model',
			'member_user_id' => 'Member User',
			'manager_group_id' => 'Manager Group',
		);
	}

	/**
	 * 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('custom_app_id',$this->custom_app_id);
		$criteria->compare('application_model_id',$this->application_model_id);
		$criteria->compare('member_user_id',$this->member_user_id);
		$criteria->compare('manager_group_id',$this->manager_group_id);

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

插入始终不能成功。提示“active record 由于不是新的, 无法被新增到数据库. ” 不知道什么原因,求大神给予指导。。。

    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册