Welcome Guest, Not a member yet? Register   Sign In
Update) Why does a simple insert fail?
#1

(This post was last modified: 03-20-2022, 06:58 AM by yoshi.)

## overview
The `m_certification` table is for all credentials.
The `certification` table is managing certification in the school use.
Each table relationship is 1:1.
The `m_certification` table has school_id for use. using to get school information.
But my php code failed for insert.
Why does a simple insert fail?
https://drive.google.com/file/d/1GCiEKXt...p=drivesdk

## detail
Now I'm trying to make a record in the `certification` table then I enter the record by hand, it can be inserted correctly.
https://drive.google.com/file/d/1ipPO9vQ...p=drivesdk

PHP Code:
-- ci4db.certification definition
CREATE TABLE 
`certification` (
  `idint unsigned NOT NULL AUTO_INCREMENT,
  `school_idint unsigned NOT NULL,
  `m_certification_idint unsigned NOT NULL,
  `name_shortvarchar(100NOT NULL,
  `created_atdatetime DEFAULT CURRENT_TIMESTAMP,
  `updated_atdatetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `school_id_m_certification_id` (`school_id`,`m_certification_id`),
  KEY `certification_m_certification_id_foreign` (`m_certification_id`),
  CONSTRAINT `certification_m_certification_id_foreignFOREIGN KEY (`m_certification_id`) REFERENCES `m_certification` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `certification_school_id_foreignFOREIGN KEY (`school_id`) REFERENCES `school` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

PHP Code:
INSERT INTO ci4db.certification
(idschool_idm_certification_idname_shortcreated_atupdated_at)
VALUES(14135'漢検準1級''2022-03-20 11:22:07''2022-03-20 11:22:07'); 

PHP Code:
    public function editPost(): void
    
{
        // guide: check box state update to model
https://codeigniter.com/user_guide/tutorial/create_news_items.html
        $checks $this->request->getPost('checked');
        $nameShort $this->request->getPost('name_short');
    
        
// TODO: Using demo school
        $schoolId SchoolController::SCHOOL_LIST['Demo'];
    
        $model 
model(CertificationModel::class);
        $alreadyExists $model->where('school_id'$schoolId)->findAll();
    
        
// TODO: focus to create!
        $certifications = [];
        foreach ($checks as $mCertificationId => $v) {
            $certification = new CertificationEntity([
                'school_id' => $schoolId,
                'm_certification_id' => $mCertificationId,
                'name_short' => $nameShort[$mCertificationId]
            ]);
            if (!in_array($mCertificationIdarray_column($alreadyExists'm_certification_id'))) {
                $certifications['create'][] = $certification;
            } elseif (isset($mCertificationId)) {
                $certifications['update'][] = $certification;
            } else {
                $certifications['delete'][] = $certification;
            }
        }
        foreach ($certifications['create'] as $record) {
            // dd($record);
            $model->save($record);
        }
        
        redirect
('school/certification/list');
    

## additional report
Looking at the stack trace, there is sloppy SQL.
Despite being converted from Entity, SQL does not contain foreign key information. This certainly causes a foreign key violation.
https://drive.google.com/file/d/1eZQB0Hy...p=drivesdk
Maybe Codeigniter shouldn't use foreign keys?
Reply
#2

Are all fields specified in CertificationModel::$allowedFields?
Reply
#3
Tongue 

(03-20-2022, 05:41 PM)iRedds Wrote: Are all fields specified in CertificationModel::$allowedFields?

Thank you as always. I was able to resolve the SQL execution. I thought that foreign keys were not covered by "permitted items".
Reply




Theme © iAndrew 2016 - Forum software by © MyBB