Jump to content


JOINING Three (or more) Tables in Grocery CRUD

JOIN SQL TABLE CRUD

  • Please log in to reply
4 replies to this topic

#1 Athar

Athar

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationNew York

Posted 12 January 2017 - 10:43 PM

I have three tables as follow.
-- Table `mydb`.`table1`
CREATE TABLE IF NOT EXISTS `mydb`.`table1` (
  `ID_table1` INT NOT NULL AUTO_INCREMENT,
  `PARENT` VARCHAR(45) NULL,
  PRIMARY KEY (`ID_table1`))
ENGINE = InnoDB;

-- Table `mydb`.`table2`
CREATE TABLE IF NOT EXISTS `mydb`.`table2` (
  `ID_table2` INT NOT NULL AUTO_INCREMENT,
  `ID_table1` INT NOT NULL,
  `CHILD` VARCHAR(45) NULL,
  PRIMARY KEY (`ID_table2`),
  INDEX `table2 [X] table11_idx` (`ID_table1` ASC),
  CONSTRAINT `table2 [X] table11`
    FOREIGN KEY (`ID_table1`)
    REFERENCES `mydb`.`table1` (`ID_table1`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- Table `mydb`.`table3`
CREATE TABLE IF NOT EXISTS `mydb`.`table3` (
  `ID_table3` INT NOT NULL AUTO_INCREMENT,
  `ID_table2` INT NOT NULL,
  `BABY` VARCHAR(45) NULL,
  PRIMARY KEY (`ID_table3`),
  INDEX `table3 [X] table21_idx` (`ID_table2` ASC),
  CONSTRAINT `table3 [X] table21`
    FOREIGN KEY (`ID_table2`)
    REFERENCES `mydb`.`table2` (`ID_table2`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

My controller goes like this:

public function Relations()
{
 $crud = new grocery_CRUD();
 $crud->set_table('table1');
 $crud->set_relation('id_table2','table2','CHILD');
 $crud->set_relation('id_table3','table3','BABY');
 $crud->columns('PARENT','CHILD','BABY');
 $output = $crud->render();
 $this->_example_output($output);
}
What I want is a list of VALUES from these attributes:
 
PARENT    CHILD      BABY
NAME        NAME       NAME       ADD        VIEW       DELETE
NAME        NAME       NAME       ADD        VIEW       DELETE
NAME        NAME       NAME       ADD        VIEW       DELETE
NAME        NAME       NAME       ADD        VIEW       DELETE

 

  • Values for Parents and Children are CRUD on a different controllers respectively.
  • Values for Babies will be CRUD on this controller

I tried different random shots but nothing worked, therefore I don't have even partial result. 

 

Your help is highly appreciated in advance.

 


~ I think I think, therefore; I think I exist.


#2 HACK

HACK

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 16 January 2017 - 12:58 PM

table_1 should have a foreign key of tabel_2 and tabel_3 

 

or you can read  

 

http://www.grocerycr...et_relation_n_n


Edited by HACK, 16 January 2017 - 12:59 PM.


#3 Athar

Athar

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationNew York

Posted 17 January 2017 - 02:29 PM

table_1 should have a foreign key of tabel_2 and tabel_3 or you can read  http://www.grocerycr...et_relation_n_n

 

Hack, I am having difficult time to understand what you mean. Take a look at the attached.

"GREEN" is my current structure, which is pretty simple and normalized. All I need is to be able to operate CRUD on the lowest TABLE3 *and* be able to see values from TABLE1 AND TABLE2 in the list of TABLE3. 

 

what you proposed is the YELLOW scenario. I am big time confused with that structure. Not only its *NOT* normalized, but also not feasible for queries/performance. Please help me understand this.

 

set_relation_n_n(); applies on LINK tables or any structure reflecting the GREY in the picture. That is not applicable in my need.2r3hxqr.png


~ I think I think, therefore; I think I exist.


#4 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 18 January 2017 - 01:51 AM

Practically myfriend Athar, this aint a straight forward stuff that one can deal directly with relations..as the grocerycrud's basic works with a single table.. and can work with the related table..  Only the one which is related to the primary table. What you trying to achieve is a level beyond.. trying to connect to a table which is not related to the primary table but secondary table. This is technically not possible with grocery crud direct methodologies. What you can do for the same is - write a custom model that will help u resolve your situation / scenario .. or write in a column_callback where u will query up the data from 3rd table relatively and display the output.

 

This are the base 2 methods i am aware of .. there may be experts who may share in further / beyond the same.. but to me.. this is the approach. Second one is easy .. but have a drawback.. it will hit the tables / queries .. the number of rows that gets displayed. But it may not be huge in number so it is more lucrative approach for me.



#5 Athar

Athar

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationNew York

Posted 18 January 2017 - 03:44 PM

@Amit, Thanks a lot. 

 
You actually did help clearing up some smoke I had in my coconut. I was under the impression that it was me who was dropping the ball somewhere. Your proposals do make sense and worth to explore further. However, during the process of trial and run, I discovered something that may intrigue others. 
2u8cl0y.png
If the table in the middle layer has a Composite KEY consisting of PK and FK from the 1st table, we CAN pull the value from 1st table in the list of 3rd table. I was successfully able to do that.
 
However, that lead me to hitch. It does not retain the dependency in third table. or at least its not logical in some scenerios. 
 
Whats your say on this?

~ I think I think, therefore; I think I exist.






Also tagged with one or more of these keywords: JOIN, SQL, TABLE, CRUD

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users