2012-10-02 140 views
4
$model=new Event('create'); 
    $model->attributes=$_POST['Event']; 
    if($model->save()){ 
    $pkg = new Package(); 
    $pkg->attributes=$_POST['Package']; 
    $pkg->event_id = $model->id; 
    $pkg->save(); 
    } 

事件模型被正確保存與事件的所有POST變量,而包只有event_id設置,但沒有設置屬性(它們都是NULL)。我究竟做錯了什麼?yii沒有保存模型

注意:Event對象已經由我的前任編寫,Package對象是我做的一個新增加。

編輯:全包類

class Package extends CActiveRecord 
    { 
    public static function model($className=__CLASS__) 
    { 
    return parent::model($className); 
    } 
public function tableName() 
    { 
    return 'tbl_package'; 
    } 
public function rules() 
    { 
    // NOTE: you should only define rules for those attributes that 
    // will receive user inputs. 
    return array(
     // The following rule is used by search(). 
     // Please remove those attributes that should not be searched. 
     array('', 'safe', 'on'=>'search'), 
     array('', 'numerical'), 
    ); 
    }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(
     'event' => array(self::BELONGS_TO, 'Event', 'id') 
    ); 
    }public function attributeLabels() 
    { 
    return array(
     'event_id'  => 'Event ID', 
     'package_name' => 'Package name', 
     'event_banner' => 'Event Banner', 
     'ad_placement' => 'Ad Placement in Event Program', 
     'logo_on_step' => 'Logo on Step & Repeat', 
     'use_evt_pics' => 'Usage of Event Pictures', 
     'exhibition' => 'Exhibition Booth/Space', 
     'inc_press'  => 'Inclusion in Press', 
     'print_ads'  => 'Insertion in Print Ads', 
     'online_ads' => 'Insertion in Online Ads', 
     'attendee_bags' => 'Attendee Bags', 
     'charging_st' => 'Charging Stations', 
     'cups'   => 'Coffee/Water Cups', 
     'distr_items' => 'Distributable Items', 
     'lanyards'  => 'Lanyards', 
     'napkins'  => 'Napkins', 
     'notebooks'  => 'Notebooks', 
     'pens'   => 'Pens', 
     'seat_covers' => 'Seat Covers', 
     'snack_pack' => 'Snack Packaging', 
     'water_bottles' => 'Water Bottles' 
    ); 
    } public function search() 
    { 
    // Warning: Please modify the following code to remove attributes that 
    // should not be searched. 

    $criteria=new CDbCriteria; 
    $criteria->compare('event_banner',$this->event_banner); 
    $criteria->compare('package_name',$this->package_name); 
    $criteria->compare('ad_placement',$this->ad_placement); 
    $criteria->compare('logo_on_step',$this->logo_on_step); 
    $criteria->compare('use_evt_pics',$this->use_evt_pics); 
    $criteria->compare('exhibition',$this->exhibition); 
    $criteria->compare('inc_press',$this->inc_press); 
    $criteria->compare('print_ads',$this->print_ads); 
    $criteria->compare('online_ads',$this->online_ads); 
    $criteria->compare('attendee_bags',$this->attendee_bags); 
    $criteria->compare('charging_st',$this->charging_st); 
    $criteria->compare('cups',$this->cups); 
    $criteria->compare('distr_items',$this->distr_items); 
    $criteria->compare('lanyards',$this->lanyards); 
    $criteria->compare('napkins',$this->napkins); 
    $criteria->compare('notebooks',$this->notebooks); 
    $criteria->compare('pens',$this->pens); 
    $criteria->compare('seat_covers',$this->seat_covers); 
    $criteria->compare('snack_pack',$this->snack_pack); 
    $criteria->compare('water_bottles',$this->water_bottles); 

    return new CActiveDataProvider('SponsorshipPackage', array(
     'criteria'=>$criteria, 
    )); 
    } 
} 
+0

我相信有一些東西是你設定的,它不是通過對驗證缺失。你可以在兩種模式中發佈你的驗證嗎? – Elbek

+0

我已經添加了Package的規則,它們生成了 – faboolous

+0

Package類中的字段是什麼?這個'array('','numerical')'不是正確的規則。你可以用你的整個Package.php類更新嗎? – Elbek

回答

9

如果你想通過設置屬性:

$pkg->attributes=$_POST['Package']; 

那麼你必須設置規則,可以這樣設置任何屬性。

你需要的東西是這樣的:

public function rules() 
{ 
    // NOTE: you should only define rules for those attributes that 
    // will receive user inputs. 
    return array(
     array('package_name', 'required'), 
     array('package_name', 'length', 'max' => 255), 
     // or 
     array('package_name', 'length', 'max' => 255, 'on' => 'insert'), 
     // at least (when no validation is required): 
     array('package_name', 'safe'), 
     // ... 
     // and so on.. 
    ); 
} 

你需要規則,你要這樣設置的任何屬性。

如果沒有規則屬性設置,你就可以將其值設置只能通過$pkg->attribute_name = $value;

規則像array('', 'safe', 'on'=>'search'),array('', 'numerical'),什麼也不做(事業屬性列表爲空)。

閱讀更多有關驗證here

1
 class Package extends CActiveRecord 
     { 
     public static function model($className=__CLASS__) 
     { 
     return parent::model($className); 
     } 
    public function tableName() 
     { 
     return 'tbl_package'; 
     } 
    public function rules() 
     { 
     // NOTE: you should only define rules for those attributes that 
     // will receive user inputs. 
     return array(
      // The following rule is used by search(). 
      // Please remove those attributes that should not be searched. 
      array('ad_placement, logo_on_step, ...', 'safe', 'on'=>'search'), //note put here all attrs name that you feel those attrs needs to be assigned from POST like: `$pkg->attributes=$_POST['Package'];` 
array('event_id', 'numerical'), 

     ); 
     }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(
      'event' => array(self::BELONGS_TO, 'Event', 'id') 
     ); 
     }public function attributeLabels() 
     { 
     return array(
      'event_id'  => 'Event ID', 
      'package_name' => 'Package name', 
      'event_banner' => 'Event Banner', 
      'ad_placement' => 'Ad Placement in Event Program', 
      'logo_on_step' => 'Logo on Step & Repeat', 
      'use_evt_pics' => 'Usage of Event Pictures', 
      'exhibition' => 'Exhibition Booth/Space', 
      'inc_press'  => 'Inclusion in Press', 
      'print_ads'  => 'Insertion in Print Ads', 
      'online_ads' => 'Insertion in Online Ads', 
      'attendee_bags' => 'Attendee Bags', 
      'charging_st' => 'Charging Stations', 
      'cups'   => 'Coffee/Water Cups', 
      'distr_items' => 'Distributable Items', 
      'lanyards'  => 'Lanyards', 
      'napkins'  => 'Napkins', 
      'notebooks'  => 'Notebooks', 
      'pens'   => 'Pens', 
      'seat_covers' => 'Seat Covers', 
      'snack_pack' => 'Snack Packaging', 
      'water_bottles' => 'Water Bottles' 
     ); 
     } 
     .... 
    } 

如果它不工作,那麼你可以嘗試,htere是validate()方法之前save()

$model=new Event('create'); 
    $model->attributes=$_POST['Event']; 
    if($model->validate()){ 
    $model->save(); 
    } 
    else{ 
     echo CHtml::errorSummary($model); 
    } 

這會分不清什麼是錯誤。

切勿直接保存,在保存前進行驗證。

相關問題