如何使用cakephp 3插入数据

时间:2016-03-06 20:09:02

标签: php cakephp

我想要使用cakephp3插入数据,但它不行。插入的数据可以达到50%。我没有返回的错误。

        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是插入但不是系列数据。 我该怎么办?

谢谢,对不起我的英语!!

1 个答案:

答案 0 :(得分: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需要一组实体   在复数,下划线版本的关联名称。阅读   更多.. Cookbook > Saving Data > Saving hasMay Associations