Here is the updated models:
<?php
use Phalcon\Mvc\Model,
Phalcon\Db\Column;
class Student extends Model
{
public function initialize()
{
$this->belongsTo("group_id", "StudentGroup", "id");
}
public static function searchStudents($search = "")
{
$where = "CONCAT(Grade.name, ' ', StudentGroup.name, ' - ', Student.lastname1, ' ', Student.lastName2, ', ', Student.name) LIKE CONCAT('%', :chain:,'%')";
$params = array(
"chain" => $search
);
$types = array(
Column::BIND_PARAM_STR
);
$students = Student::query()
->join("StudentGroup", "Student.group_id = StudentGroup.id")
->join("Grade", "StudentGroup.grade_id = Grade.id")
->where($where)
->bind($params)
->bindTypes($types)
->execute();
$results = array();
foreach ($students as $student)
{
$results[] = array(
"id" => $student->id,
"chain" => ($student->group->grade->name . " " .
$student->group->description . " - " .
$student->lastName1 . " " .
$alumno->lastName2 . ", " .
$student->name)
);
}
return $results;
}
}
<?php
use Phalcon\Mvc\Model;
class StudentGroup extends Model
{
public function initialize()
{
$this->belongsTo('grade_id', 'Grade', 'id');
$this->hasMany('id', 'Student', 'group_id');
}
}
<?php
use Phalcon\Mvc\Model;
class Grade extends Model
{
public function initialize()
{
$this->hasMany('id', 'StudentGroup', 'grade_id');
}
}
Also be careful when any of those columns are null a CONCAT willl return null.