⚠ 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

how to create a model relationing three tables?



Marcos Cabrera

Marcos Cabrera
  • profile picture
  • Member

Posted 09 September 2013 - 17:34 PM

tengo 3 tablas:

1er tabla - reparaciones con id primary key= reparacion_id y foreign key = equipo_id

 

2da tabla - equipos con id primary key = equipo_id y foreign key= cliente_id

 

3er tabla - clientes con id primary key= cliente_id

 

quiero relacionar estas 3 tablas porque solo quiero mostrar el nombre del cliente sin que modifique nada en la bd

 

 

 

i have three tables:

1st table - reparaciones with id primary key= reparacion_id y foreign key = equipo_id

 

2nd table - equipos with id primary key = equipo_id y foreign key= cliente_id

 

3rd table - clientes with id primary key= cliente_id

 

 

i want to relation this 3 tables because i want to show the name of client without modifying anything in the db

 

[attachment=673:tablas.jpg]

 

[attachment=674:programa.png]

 

[attachment=675:CODIGO.png]

 

thanks


Marcos Cabrera

Marcos Cabrera
  • profile picture
  • Member

Posted 12 September 2013 - 16:53 PM

solve the problem with this post:http://bit.ly/1en6ToD

the resulting code is this:

public function reparaciones_management()
	{
		try{
			$crud = new grocery_CRUD();
			$crud->set_theme('flexigrid');
			$crud->set_table('reparaciones');
			$crud->set_subject('reparaciones');
			$crud->required_fields('equipo_id','empleado_id','estado');
		    $crud->set_relation('equipo_id','equipos','{num_serie} tipo: {tipo} ');
			$crud->set_relation('empleado_id','empleados','nombres');
			$crud->field_type('detalles', 'text');
			$crud->columns('reparacion_id','equipo_id','empleado_id','fecha_ingreso','estado','detalles','nombres cliente');
			$crud->callback_column('nombres cliente',array($this,'getNombresClientes'));
			$output = $crud->render();
			$this->_reparacion_output($output);
			
		}catch(Exception $e){
			show_error($e->getMessage().' --- '.$e->getTraceAsString());
		}
	}
	
	function getNombresClientes($primary_key,$row) {
        $sql = "SELECT nombres
				FROM clientes
				WHERE cliente_id
				IN (
				SELECT cliente_id
				FROM equipos
				INNER JOIN reparaciones ON reparaciones.equipo_id = equipos.equipo_id where equipos.equipo_id=$row->equipo_id)";
        $result = $this->db->query($sql)->row();
        $title = $result->nombres;
        		
        return $title;
}