Model-> save()=>相同的代码不同的控制器,但不具有相同的行为
问题描述:
$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中使用模型的标准代码。 在我的originController中,代码和我的mySql数据库完全一样。但在我的其他控制器中,一切工作正常。Model-> save()=>相同的代码不同的控制器,但不具有相同的行为
有人知道怎么可能?这个“bug”让我疯狂!
谢谢大家:)
更新: $ 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,
));
}
答
更新:问题解决 我mySql分贝上的一些错误的外键。但是,yii并没有说这些。 谢谢大家
请阅读[我可以问什么主题](http://stackoverflow.com/help/on-topic) 和[如何问一个好问题](http://stackoverflow.com/帮助/如何问) 和[完美问题](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) 以及如何创建[最小,完整和可验证的例子](http://stackoverflow.com/help/mcve) – RiggsFolly
你有任何错误? – Matheno
什么都没有。一切工作正常,除了我的数据库没有新的记录。 –