public function save_student($params, $id) { $classroom = $this->user_classrooms[$params['classroom_id']]; if ($classroom) { $classroom = $classroom->getClassroom(); if ($classroom->cannotBeEditedBy($this->user)) { return -2; } } $student = $this->ci->doctrine->em->getRepository('Entity\\Student')->find($id); if ($student == null) { $student = new \Entity\Student(); } $student->setName($params['name']); $student->setEmail($params['email']); $student->setPhone($params['phone']); $student->setSexe($params['sexe']); $student->setImg($params['img']); $student->setClassroom($classroom); $this->ci->doctrine->em->persist($student); if (!$id) { $tests = $this->get_classroom_tests($params['classroom_id']); for ($i = 0, $max = count($tests); $i < $max; ++$i) { $mark = new \Entity\Mark(); $mark->setTest($tests[$i]); $mark->setStudent($student); $this->ci->doctrine->em->persist($mark); } } $this->ci->doctrine->em->flush(); return $student; }