Grocery CRUD set_relation_n_n添加多行数据

时间:2016-08-04 08:31:54

标签: php mysql codeigniter grocery-crud

我很喜欢杂货店的CI,我想把它用于我正在研究的最新项目,但是我在掌握set_relation_n_n功能的知识时遇到了一些问题。

以下是我的ERD,我想为每个学生ID添加多个课程(crn),因为学生可以在很多课程中注册。 Entity Relationship Diagram

我确实尝试使用set_relation_n_n函数,但是,在尝试添加记录时我无法保存。以下是我得到的代码和结果:

<?php  

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {

    function __construct()
    {
            parent::__construct();

        /* Standard Libraries of codeigniter are required */
        $this->load->database();
        $this->load->helper('url');
        /* ------------------ */ 

        $this->load->library('grocery_CRUD');

    }

    public function index()
    {
        echo "<h1>Login Form Goes here</h1>";//Just an example to ensure that we get into the function
        die();
    }

    public function registration()
    {
        $this->grocery_crud->set_theme('datatables');
        $this->grocery_crud->set_table('reg')
                        ->set_relation('stuid','student','{fname} {lname}')
                        ->set_relation_n_n('class', 'reg','class', 'stuid', 'crn', '{year} {form} {sbjcode}','crn');
        $this->grocery_crud->unset_columns('crn')->fields('stuid','class');
        $output = $this->grocery_crud->render();

        $this->_example_output($output);        
    }

    function _example_output($output = null)
    {
        $this->load->view('main.php',$output);    
    }
}

/* End of file Main.php */
/* Location: ./application/controllers/Main.php */

结果输出 Result Output

以下是该项目的SQL:

-- -----------------------------------------------------
-- Schema schassess
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `schassess` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `schassess` ;

-- -----------------------------------------------------
-- Table `schassess`.`student`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `schassess`.`student` ;

CREATE TABLE IF NOT EXISTS `schassess`.`student` (
  `stuid` INT NOT NULL AUTO_INCREMENT,
  `fname` VARCHAR(100) NULL,
  `lname` VARCHAR(100) NULL,
  PRIMARY KEY (`stuid`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `schassess`.`subject`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `schassess`.`subject` ;

CREATE TABLE IF NOT EXISTS `schassess`.`subject` (
  `sbjcode` VARCHAR(5) NOT NULL,
  `sbjdesc` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`sbjcode`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `schassess`.`class`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `schassess`.`class` ;

CREATE TABLE IF NOT EXISTS `schassess`.`class` (
  `crn` INT NOT NULL AUTO_INCREMENT,
  `year` VARCHAR(4) NOT NULL,
  `form` VARCHAR(45) NOT NULL,
  `sbjcode` VARCHAR(5) NOT NULL,
  PRIMARY KEY (`crn`),
  INDEX `fk_class_subject_idx` (`sbjcode` ASC),
  CONSTRAINT `fk_class_subject`
    FOREIGN KEY (`sbjcode`)
    REFERENCES `schassess`.`subject` (`sbjcode`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `schassess`.`assessment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `schassess`.`assessment` ;

CREATE TABLE IF NOT EXISTS `schassess`.`assessment` (
  `a_id` INT NOT NULL AUTO_INCREMENT,
  `a_desc` VARCHAR(100) NOT NULL,
  `a_maxmark` INT(3) NOT NULL DEFAULT 0,
  `crn` INT NOT NULL,
  PRIMARY KEY (`a_id`),
  INDEX `fk_assessment_class1_idx` (`crn` ASC),
  CONSTRAINT `fk_assessment_class1`
    FOREIGN KEY (`crn`)
    REFERENCES `schassess`.`class` (`crn`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `schassess`.`reg`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `schassess`.`reg` ;

CREATE TABLE IF NOT EXISTS `schassess`.`reg` (
  `idreg` INT NOT NULL AUTO_INCREMENT,
  `stuid` INT NOT NULL,
  `crn` INT NOT NULL,
  PRIMARY KEY (`idreg`),
  INDEX `fk_reg_student1_idx` (`stuid` ASC),
  INDEX `fk_reg_class1_idx` (`crn` ASC),
  CONSTRAINT `fk_reg_student1`
    FOREIGN KEY (`stuid`)
    REFERENCES `schassess`.`student` (`stuid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_reg_class1`
    FOREIGN KEY (`crn`)
    REFERENCES `schassess`.`class` (`crn`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `schassess`.`stuassess`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `schassess`.`stuassess` ;

CREATE TABLE IF NOT EXISTS `schassess`.`stuassess` (
  `stuassessid` INT NOT NULL AUTO_INCREMENT,
  `mark` INT(3) NOT NULL DEFAULT 0,
  `abs` BINARY NULL DEFAULT 0,
  `idreg` INT NOT NULL,
  `a_id` INT NOT NULL,
  PRIMARY KEY (`stuassessid`),
  INDEX `fk_stuassess_reg1_idx` (`idreg` ASC),
  INDEX `fk_stuassess_assessment1_idx` (`a_id` ASC),
  CONSTRAINT `fk_stuassess_reg1`
    FOREIGN KEY (`idreg`)
    REFERENCES `schassess`.`reg` (`idreg`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_stuassess_assessment1`
    FOREIGN KEY (`a_id`)
    REFERENCES `schassess`.`assessment` (`a_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
USE `schassess`;

DELIMITER $$

USE `schassess`$$
DROP TRIGGER IF EXISTS `schassess`.`reg_AINS` $$
USE `schassess`$$
CREATE TRIGGER `reg_AINS` AFTER INSERT ON `reg` FOR EACH ROW
begin
 insert into stuassess (idreg,a_id)
 SELECT (SELECT idreg FROM reg r WHERE r.crn = new.crn and r.stuid = new.stuid), a.a_id FROM  `assessment` a WHERE a.crn = new.crn;
end;$$


DELIMITER ;

0 个答案:

没有答案