Model-> save()=>相同的代码不同的控制器,但没有相同的行为

时间:2017-02-13 09:45:30

标签: php yii

$myModel = new CustomModel();
$myModel->myVal = 'foo';
$myModel->anotherVal = 'bar';
var_dump($myModel);              //return a CustomModel object
var_dump($myModel->validate());  //return true
var_dump($myModel->getErrors()); //return an empty array
var_dump($myModel->save());      //return true
var_dump($myModel->save(false)); //return true

这是在Yii中使用Model的标准代码。 在我的originController中,代码与我的mySql DB中的代码完全相同。但在我的otherController中,一切正常。

有人知道怎么可能吗?这个“虫子”让我发疯了!

谢谢大家:)

更新: $ myModel->保存(假) 已经检查和相同的行为

CustomModel.php

<?php

/**
* This is the model class for table "CustomTable".
*
* The followings are the available columns in table 'CustomTable':
* @property integer $customModel_id
* @property integer $customModel_legaldocid
* @property integer $customModel_userid
* @property string $customModel_datelastview
* @property string $customModel_dateaccepted
*/
class CustomModel extends CAActiveRecord
{
    /**
    * Returns the static model of the specified AR class.
    * @param string $className active record class name.
    * @return LegalDocumentRead 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 'CustomTable';
}

/**
 * @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('customModel_id, customModel_legaldocid, customModel_userid', 'numerical', 'integerOnly'=>true),
        array('customModel_datelastview, customModel_dateaccepted', 'safe'),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('customModel_id, customModel_legaldocid, customModel_userid, customModel_datelastview, customModel_dateaccepted', '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(
        'customModel_id' => 'customModel',
        'customModel_legaldocid' => 'customModel Legaldocid',
        'customModel_userid' => 'customModel Userid',
        'customModel_datelastview' => 'customModel Datelastview',
        'customModel_dateaccepted' => 'customModel Dateaccepted',
    );
}

/**
 * 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('customModel_id',$this->customModel_id);
    $criteria->compare('customModel_legaldocid',$this->customModel_legaldocid);
    $criteria->compare('customModel_userid',$this->customModel_userid);
    $criteria->compare('customModel_datelastview',$this->customModel_datelastview,true);
    $criteria->compare('customModel_dateaccepted',$this->customModel_dateaccepted,true);

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

2 个答案:

答案 0 :(得分:0)

这可能与您在CustomModel中有一些在测试中不满意的验证规则有关。

尝试使用

 $myModel->save(false);

 var_dump($myModel->save(false));    

并检查您的数据库。如果您看到新记录而不是检查验证规则或消息错误..

答案 1 :(得分:0)

更新:问题解决了 我的mySql数据库上有一些错误的外键。但是,yii并没有对此发表任何意见。 谢谢大家