Avsousa
Avsousa

Reputation: 5

Symfony - Try to update a record in DB with some foreign keys

Thanks a lot for recent help. It works really well, but now I am having some problems with this code.

          //Save course record to DB
          $record_course = new course();
          $record_course->setName($courseInfo['name']);
          $record_course->setUrl($courseInfo['url']);
          $record_course->setAcronym($courseInfo['acronym']);
          $record_course->setCourseTypeId($record_courseType->getId());
          $record_course->setStartDate($courseInfo['startDate']);
          $record_course->setCollegeId($record_college->getId());
          $record_course->setPlanUrl($courseInfo['planUrl']);
          $record_course->replace();

As you can see, I try to update a record but now we have some foreign keys. In this case is CourseTypeId and CollegeId. When i try to replace this record i get next message:

"SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (grabmark.course, CONSTRAINT course_course_type_id_course_type_id FOREIGN KEY (course_type_id) REFERENCES course_type (id))"

Anyone can help me please? Sorry, but i am initiate now in symfony and it's my first contact with an MVC Framework. Thanks a lot, Alexandre Sousa

Upvotes: 0

Views: 309

Answers (1)

Parijat Kalia
Parijat Kalia

Reputation: 5085

Alexandre, this is more of a database issue than symfony.

When you attempt to update this record, you are changing CourseTypeId and CollegeId with new values and these according to you are foreign keys. Are the values in these fields (CourseTypeId and CollegeId) present in the table where these keys are being referenced from?

For example:

Lets say you are setting CourseTypeId to 15 and CollegeId to 10, then are the values 15 and 10 also present in the database table from where these foreign keys are being referenced? If not, then the Integrity constraint error you are experiencing shows up. Since you are UPDATING and not really INSERTING, then the values 15 and 10 must be present in the table from where the two foreign key columns are referencing.

Does this help?

Upvotes: 2

Related Questions