Consulta Model Codeigniter

0

Hello everyone I have a question the question is, I have in the database 3 tables student, tutor and person. Student has foreign keys to person and tutor, my question is, I have problems to bring the name of the tutor, I need the name of the student but also the name of the tutor that is not bringing me, that data is in the person table I put the query I can help me. Thank you!

$this->db->select('alumno.* , persona.name as alumnopersona_name,persona.dni as alumnopersona_dni,persona.cuil as persona_cuil ,curso.name as curso_name, estado_alumno.name as estadoalumno_name, escuela.name as escuela_name, tutor.persona_id as tutorpersona, persona.name as tutorpersona_name');
    $this->db->from('alumno');
    $this->db->join('persona', 'alumno.persona_id = persona.id', 'left');
    $this->db->join('tutor', 'alumno.tutor_id=tutor.id');                   
    $this->db->join('curso','alumno.curso_id= curso.id','left');
    $this->db->join('escuela','alumno.escuela_id= escuela.id','left');
    $query = $this->db->get();


     if ($query->num_rows()>0) {
         return $query->result();
     }else
     {
        return FALSE;
     }
    
asked by Pao 12.09.2018 в 14:56
source

1 answer

0

$ this- > db-> select ( 'student.* , persona.name as studentpersonname_name , persona.dni as studentpersona_dni, persona.cuil as persona_cuil, curso.name as curso_name, estado_alumno.name as estadoalumno_name, escuela.name as escuela_name, tutor.persona_id as tutorpersona, persona.name as tutorpersona_name ') ;

You are telling him to take the field persona.name 2 times but with different aliases and he is only extracting you 1.

It is best to make a subquery to take on behalf of the tutor and the student separately:

$this->db->select('alumno.* , SELECT (persona.name WHERE persona.id = alumno.id) as alumnopersona_name,persona.dni as alumnopersona_dni,persona.cuil as persona_cuil ,curso.name as curso_name, estado_alumno.name as estadoalumno_name, escuela.name as escuela_name, tutor.persona_id as tutorpersona, SELECT (persona.name WHERE persona.id = tutor.id) as tutorpersona_name');
$this->db->from('alumno');
$this->db->join('persona', 'alumno.persona_id = persona.id', 'left');
$this->db->join('tutor', 'alumno.tutor_id=tutor.id');                   
$this->db->join('curso','alumno.curso_id= curso.id','left');
$this->db->join('escuela','alumno.escuela_id= escuela.id','left');
    
answered by 10.10.2018 в 10:54