2016-03-06 86 views
0

我想用cakephp3插入數據,但它不好。數據的插入在50%時是可以的。我沒有錯誤,這是返回。如何用cakephp插入數據3

 class SeancesTable extends Table 
{ 
    public function initialize(array $config) 
    { 
     //$this->hasMany('Exercices'); 
     $this->hasMany('Series'); 
     $this->belongsToMany('Exercices'); 
     $this->addBehavior('TimesTamp'); 
    } 
} 

class SeriesTable extends Table 
{ 
    public function initialize(array $config) 
    { 
     parent::initialize($config); 

     $this->table('series'); 
     $this->displayField('id'); 
     $this->primaryKey('id'); 

     $this->belongsTo('Seances'); 
     //$this->hasOne('Seances'); 

    } 

    public function add(){ 

     $requestData = [ 
      'Seances' =>[ 
       'commentaire' => 'test cake 33', 
       'date_seance' => '2016-02-27', 
       'user_id' => 1, 
       'Series' => [ 
        ['nb_rep' => 17, 'charge' => 520, 'exercice_id'=>1], 
        ['nb_rep' => 27, 'charge' => 600] 
       ] 
      ] 
     ]; 

     $seance = $this->Seances->newEntity($requestData,[ 
      'associated' => ['Series'] 
     ]); 
     $this->Seances->save($seance); 
    } 

在我的數據庫中,數據Seances是插入的,但不是系列數據。 我該怎麼辦?

感謝和抱歉我的英語!

回答

0

修改這樣

$requestData = [ 
      'commentaire' => 'test cake 33', 
      'date_seance' => '2016-02-27', 
      'user_id' => 1, 
      'Series' => [ 
       ['nb_rep' => 17, 'charge' => 520, 'exercice_id'=>1], 
       ['nb_rep' => 27, 'charge' => 600] 
      ] 
    ]; 

$seance = $this->Seances->newEntity($requestData,[ 
      'associated' => ['Series'] 
     ]); 

$this->Seances->save($seance); 

代碼時保存的hasMany協會,ORM預計在多個實體 陣列,強調該協會名稱的版本。閱讀 更多..​​3210