Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 向Phalcon多对多关系添加项目_Php_Model_Many To Many_Phalcon - Fatal编程技术网

Php 向Phalcon多对多关系添加项目

Php 向Phalcon多对多关系添加项目,php,model,many-to-many,phalcon,Php,Model,Many To Many,Phalcon,我有两个模型,在许多到五月的关系中——让我们以讲师和学生为例 class Lecturer { public function initialize() { $this->hasMany('studentId', 'Model\Entity\LecturerStudent', 'studentId', ['alias' => 'LecturerStudent']); $this->hasManyToMany(

我有两个模型,在许多到五月的关系中——让我们以讲师和学生为例

class Lecturer
{
    public function initialize()
    {
        $this->hasMany('studentId', 'Model\Entity\LecturerStudent', 'studentId', ['alias' => 'LecturerStudent']);
        $this->hasManyToMany(
            'lecturerId',
            'Model\Entity\LecturerStudent',
            'lecturerId',
            'studentId',
            'Model\Entity\Student',
            'studentId',
            ['alias' => 'Students']
        );
    }
}

class LecturerStudent
{
    public function initialize()
    {
        $this->belongsTo('studentId', 'Model\Entity\Student', 'studentId', ['alias' => 'Student']);
        $this->belongsTo('lecturerId', 'Model\Entity\Lecturer', 'lecturerId', ['alias' => 'Lecturer']);
    }
}

class Student
{
    public function initialize()
    {
        $this->hasMany('lecturerId', 'Model\Entity\LecturerStudent', 'lecturerId', ['alias' => 'LecturerStudent']);
    }
}
现在,当我想在讲师中增加学生时,我需要做的就是:

$lecturerA = new Lecturer();
$studentA = new Student();
$studentB = new Student();
$lecturerA->Students = [$studentA, $studentB];
$lecturerA->save();
这一切都如我所料

当我在一个事务中导入多个记录并且需要向关系中添加第二个数组时,应用程序中就会出现问题

因此,在示例中:

$lecturerA = new Lecturer();
$studentA = new Student();
$studentB = new Student();
$lecturerA->Students = [$studentA, $studentB];

... other code doing other things ...

$studentC = new Student();
$studentD = new Student();
$lecturerA->Students = [$studentC, $studentD];

... more code ...

$lecturerA->save();
在这种情况下,只保存在第二次作业中添加给
学生的项目。第一次分配的项目丢失。因此,在我的示例中,只有
studentC
studentD
会写入数据库


在Phalcon中,有没有正确的方法来添加到以前的多对多数组中?在执行导入的类中,我有另一种(messier)方法来实现这一点,但如果有正确的方法,我更愿意使用它。

您需要将students对象存储到临时数组中,因为第二次赋值将覆盖
$teachea->students

$lecturerA = new Lecturer();
$studentA = new Student();
$studentB = new Student();
$tmpStudents = [$studentA, $studentB];

... other code doing other things ...

$studentC = new Student();
$studentD = new Student();
$tmpStudents = $tmpStudents + [$studentC, $studentD];
$lecturerA->Students = $tmpStudents;

... more code ...

$lecturerA->save();
或者您可以在设置学生关系后调用
$讲师->save()
方法。但在非正常情况下,这将导致更高的网络流量,因为每次调用
save()
INSERT
UPDATE
都必须对RDBMS进行查询

$lecturerA = new Lecturer();
$studentA = new Student();
$studentB = new Student();
$lecturerA->Students = [$studentA, $studentB];
$lecturerA->save();

... other code doing other things ...

$studentC = new Student();
$studentD = new Student();
$lecturerA->Students = [$studentC, $studentD];

... more code ...

$lecturerA->save();