Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

hasMany and belongsTo

Hello guys,

I have:

<?php 

namespace Midas\Models\Proccess;

use Phalcon\Mvc\Model;

class Proccess extends Model {

    public $cod_processo;
    public $nome;
    public $data_criacao;

    public function initialize() {
        $this->setSource('proccess');
        $this->hasMany("cod_processo", "\Midas\Models\Proccess\ProccessActivity", "cod_processo");
    }

    public function getSource() {
        return 'proccess';
    }
}

and

<?php 

namespace Midas\Models\Proccess;

use Phalcon\Mvc\Model;

class ProccessActivity extends Model {

    public $cod_atividade;
    public $cod_processo;
    public $nome;
    public $cliente;
    public $onde;
    public $quando;
    public $porque;
    public $tempo;
    public $como;
    public $ordem;

    public function initialize() {
        $this->setSource('proccess_activity');
        $this->hasMany("cod_atividade", "\Midas\Models\Proccess\ActivityResponsible", "cod_atividade");
        $this->belongsTo("cod_processo", "\Midas\Models\Proccess\Proccess", "cod_processo");
    }

    public function getSource() {
        return 'proccess_activity';
    }
}

but I can not just recover:

$proccess = Proccess::findFirst($id);           
$proccessActivity = $proccess->getProccessActivity();

following documentation: https://docs.phalconphp.com/uk/latest/reference/models.html

i need help!!!! please!

Use phalcon-devtools for generation models.

 phalcon model --name=locale_source --camelize --mapcolumn --get-set --namespace=Midas\\Models

Because there is no such alias as ProccessActivity

use:

$proccessActivity = $proccess->getCodProcesso();
or
$proccessActivity = $proccess->cod_processo;

But because you defined property it can be a problem, try to remove it or add method like this:

public function getCodProcesso()
{
    return $this->getRelated('cod_processo')
}

I think I got it wrong.

well, the documentation says:

The first parameter indicates the field of the local model used in the relationship; the second indicates the name of the referenced model and the third the field name in the referenced model. You could also use arrays to define multiple fields in the relationship.

does not mention alias.

anyway, I will check only on Thursday, as tomorrow is a holiday and the company's repository is local. Thank you for now.



99.5k
Accepted
answer
edited Sep '16

Oh you didn't even set alias so change it like this:

$this->hasMany("cod_processo", "\Midas\Models\Proccess\ProccessActivity", "cod_processo", ['alias' => 'proccessActivity']);

And it should work fine. Also better to call it processActivites and method getProcessActivities

https://docs.phalconphp.com/pl/latest/reference/models.html#aliasing-relationships

perfect!!!!!!

now:

<?php 

namespace Midas\Models\Process;

use Phalcon\Mvc\Model;

class Process extends Model {

    public $cod_processo;
    public $nome;
    public $data_criacao;

    public function initialize() {
        $this->setSource('process');
        $this->hasMany("cod_processo", "\Midas\Models\Process\ProcessActivity", "cod_processo", ["alias" => "ProcessActivity"]);
    }

    public function getSource() {
        return 'process';
    }
}

and:

$process = Process::findFirst($id);
$processActivity = $process->getProcessActivity();

working!!!! thanks!