Relationships between Models

Hello! I want to understand how to deal with relationships in the model. Did everything as in this example: http://docs.phalconphp.com/en/1.0.0/reference/models.html#relationships-between-models

class ConnexionData extends Model {

    public $id;
    public $clinic_id;
    public $disease_id;

    public function getSource() {
        return "connexion";
    }

    public function initialize() {
        $this->belongsTo("clinic_id", "NameSpace\Models\ClinicsData", "id", array(
            'alias' => 'clinics',
            'reusable' => true
        ));
        $this->belongsTo("disease_id", "NameSpace\Models\DiseaseData", "id", array(
            'alias' => 'disease',
            'reusable' => true
        ));
    }

}

class ClinicsData extends Model {

    public $id;
    public $name;

    public function getSource() {
        return "clinics";
    }

    public function initialize() {
        $this->hasMany("id", "NameSpace\Models\ConnexionData", "clinic_id", array(
            'alias' => 'сonnexion'
        ));
    }


class DiseaseData extends Model {

    public $id;
    public $name;

    public function getSource() {
        return "disease";
    }

    public function initialize() {
        $this->hasMany("id", "NameSpace\Models\ConnexionData", "disease_id", array(
            'alias' => 'сonnexion'
        ));
    }

I make a call from the action:

$services = ClinicsData::getClinicDiseaseData($id);
$this->view->setVar("services", $services);

public function getClinicDiseaseData($id) {
        $clinic = ClinicsData::find($id);
        foreach ($clinic->connexionData as $connexionData) {
            $results[] = $connexionData->diseasesData->name;
        }
        return $results;
    }

But the data does not take from the table (Postgres DB).

What could be wrong?



404

Is getClinicDiseaseData is a method of ClinicsData Model? It's not clear from your example.

If yes, then it should be static, not public method. If no, i assume that it is a part of your controller and it should be called as: ```php $services = $this->getClinicDiseaseData($id); ```

One other thing. As you made aliases for related tables, so use them:

public function getClinicDiseaseData($id) {
  $clinic = ClinicsData::findFirst($id);
  $results = array();
  foreach ($clinic->connexion as $connexionData) {
    $results[] = $connexionData->disease->name;
  }
  return $results;
}
edited Oct '14

Yes, getClinicDiseaseData is a method of ClinicsData Model.

I want to use the connection table, but it returns an empty array :(

Change method on

static function getClinicDiseaseData($id) { ... }

but I still get an empty array



404

Just add some debug messages in getClinicDiseaseData.

public function getClinicDiseaseData($id) {
  $clinic = ClinicsData::findFirst($id);
  var_dump($clinic->count()); // count of clinic rows. Should be one, but better be sure, that it is not 0.
  var_dump($clinic->connexion->count()); // count of related connexion rows.
  $results = array();
  foreach ($clinic->connexion as $connexionData) {
    $results[] = $connexionData->disease->name;
  }
  var_dump(count($results));
  die(); // extremely, but fast =)
  return $results;
}

Not the best debug messages, but they will point you to the problem place.

edited Oct '14

If you write your hands request, it returns the desired data, and through ORM - empty ...

select 
c.name,
d.*
from clinics c
inner join connexion cn on cn.clinic_id = c.id
inner join disease d on d.id = cn.disease_id 
where c.id = 2

Maybe it is better to manually write requests?

P.S. var_dump($clinic->connexion->count()); return Fatal error, and $clinic->connexion - empty.



404

Now i'm confused completely...

How your tables are named? From your Model's names they should be: clinicsdata, connexiondata and disease_data. But from your plain sql query it seems that they are: clinics, connexion and disease.

If second case, then your models should be: Clinics, Connexion and Disease. And you can drop aliases from initialization.