⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Set relation one to many issue



joshmmo
  • profile picture
  • Member

Posted 18 November 2013 - 19:31 PM

I have the following tables:

 

http://postimg.org/image/7n6ifo3lj/

 

My code:

    public function question_management()
    {
    	$question_management_crud = new grocery_CRUD();
    	$question_management_crud->set_theme('datatables');
    	$question_management_crud->set_table('question');
    
    	$question_management_crud->display_as('question_id','Question Text');
    	$question_management_crud->set_subject('Question');
    
    	$question_management_crud->set_relation('question_id', 'option', 'option_text');
    
    	$output = $question_management_crud->render();
    		
    	$this->_table_output($output);
    }

I want a question page that shows all the options associated with a question. I would also like it when they click edit or "Add Question" they can create options for the question on the same page.

 

Example Question: Do you like fruit?

 

Example Options for above question:

Yes,

No,

 

As you can see the options table has an option_number which says which option comes first, and option text (Yes or No). A very simple survey generator.

 

On my results, all I see is a list of questions. There is no option information showing per question. What am I doing wrong? And how can I make it so you can create as many options as you want on the question creation form.

 

Output screenshot:

http://postimg.org/image/44lyxey7z/

 

SQL for my tables:

-- -----------------------------------------------------
-- Table `survey`.`question`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `survey`.`question` (
  `question_id` INT NOT NULL AUTO_INCREMENT,
  `question_text` VARCHAR(255) NULL,
  PRIMARY KEY (`question_id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `survey`.`option`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `survey`.`option` (
  `option_id` INT NOT NULL AUTO_INCREMENT,
  `question_id` INT NOT NULL,
  `option_number` INT NOT NULL,
  `option_text` TEXT NULL,
  INDEX `fk_option_question1_idx` (`question_id` ASC),
  PRIMARY KEY (`option_id`),
  UNIQUE INDEX `uk_question_option_number_key` (`question_id` ASC, `option_number` ASC),
  CONSTRAINT `fk_option_question1`
    FOREIGN KEY (`question_id`)
    REFERENCES `survey`.`question` (`question_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

Thank you so much for the help in advanced. I have been stuck on this issue will no luck finding the answer.


edramirez
  • profile picture
  • Member

Posted 19 November 2013 - 16:44 PM

Looks like a master-detail setup. Check out my posts on master-detail so you could have an idea on how to accomplish that.

 

And oh yes, don't forget to click the Like button.  B)

 

Ed Ramirez