We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

get related records

hi

how to get a related records?

eg:

$parameters["order"] = "Pessoa.nome";
$pessoas = Pessoa::find($parameters);

foreach ($pessoas as $pessoa) {
    $usuario = $pessoa->getUsuario();

    echo $usuario->getLogin(); //Fatal error: Call to undefined method Phalcon\Mvc\Model\Resultset\Simple::getName() in...

}

//..............

foreach ($pessoas as $pessoa) {
    $usuario = $pessoa->getUsuario();

    echo $usuario->login; //Undefined property: Phalcon\Mvc\Model\Resultset\Simple::$login in...

}
  • phalcon 2.0.2
  • php 5.6
  • apache 2.4
  • postgres 9.4


4.0k
edited Jul '15

If your relation name is usuario, try

echo $pesoa->usuario->getLogin();

I tried this too, but it did not work



4.0k

Could you please provide more the code (model and relationship configuration)?

// pessoa.php

<?php

class Pessoa extends ModelBase //ModelBamble
{

    /**
     *
     * @var integer
     */
    protected $id_pessoa;

    /**
     *
     * @var string
     */
    protected $nome;

    /**
     *
     * @var string
     */
    protected $data_nasc;

    /**
     *
     * @var string
     */
    protected $sexo;

    /**
     *
     * @var string
     */
    protected $pai;

    /**
     *
     * @var string
     */
    protected $mae;

    /**
     *
     * @var integer
     */
    protected $cod_foto;

    /**
     *
     * @var double
     */
    protected $cpf;

    /**
     *
     * @var integer
     */
    protected $cod_cidade;

    /**
     * Method to set the value of field id_pessoa
     *
     * @param integer $id_pessoa
     * @return $this
     */
    public function setIdPessoa($id_pessoa)
    {
        $this->id_pessoa = $id_pessoa;

        return $this;
    }

    /**
     * Method to set the value of field nome
     *
     * @param string $nome
     * @return $this
     */
    public function setNome($nome)
    {
        $this->nome = $nome;

        return $this;
    }

    /**
     * Method to set the value of field data_nasc
     *
     * @param string $data_nasc
     * @return $this
     */
    public function setDataNasc($data_nasc)
    {
        $this->data_nasc = $data_nasc;

        return $this;
    }

    /**
     * Method to set the value of field sexo
     *
     * @param string $sexo
     * @return $this
     */
    public function setSexo($sexo)
    {
        $this->sexo = $sexo;

        return $this;
    }

    /**
     * Method to set the value of field pai
     *
     * @param string $pai
     * @return $this
     */
    public function setPai($pai)
    {
        $this->pai = $pai;

        return $this;
    }

    /**
     * Method to set the value of field mae
     *
     * @param string $mae
     * @return $this
     */
    public function setMae($mae)
    {
        $this->mae = $mae;

        return $this;
    }

    /**
     * Method to set the value of field cod_foto
     *
     * @param integer $cod_foto
     * @return $this
     */
    public function setCodFoto($cod_foto)
    {
        $this->cod_foto = $cod_foto;

        return $this;
    }

    /**
     * Method to set the value of field cpf
     *
     * @param double $cpf
     * @return $this
     */
    public function setCpf($cpf)
    {
        $this->cpf = $cpf;

        return $this;
    }

    /**
     * Method to set the value of field cod_cidade
     *
     * @param integer $cod_cidade
     * @return $this
     */
    public function setCodCidade($cod_cidade)
    {
        $this->cod_cidade = $cod_cidade;

        return $this;
    }

    /**
     * Returns the value of field id_pessoa
     *
     * @return integer
     */
    public function getIdPessoa()
    {
        return $this->id_pessoa;
    }

    /**
     * Returns the value of field nome
     *
     * @return string
     */
    public function getNome()
    {
        return $this->nome;
    }

    /**
     * Returns the value of field data_nasc
     *
     * @return string
     */
    public function getDataNasc()
    {
        return $this->data_nasc;
    }

    /**
     * Returns the value of field sexo
     *
     * @return string
     */
    public function getSexo()
    {
        return $this->sexo;
    }

    /**
     * Returns the value of field pai
     *
     * @return string
     */
    public function getPai()
    {
        return $this->pai;
    }

    /**
     * Returns the value of field mae
     *
     * @return string
     */
    public function getMae()
    {
        return $this->mae;
    }

    /**
     * Returns the value of field cod_foto
     *
     * @return integer
     */
    public function getCodFoto()
    {
        return $this->cod_foto;
    }

    /**
     * Returns the value of field cpf
     *
     * @return double
     */
    public function getCpf()
    {
        return $this->cpf;
    }

    /**
     * Returns the value of field cod_cidade
     *
     * @return integer
     */
    public function getCodCidade()
    {
        return $this->cod_cidade;
    }

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("sch_pessoa");
        $this->hasMany('id_pessoa', 'Usuario', 'id_usuario', \NULL);
        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'pessoa';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return Pessoa[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return Pessoa
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return array(
            'id_pessoa' => 'id_pessoa',
            'nome' => 'nome',
            'data_nasc' => 'data_nasc',
            'sexo' => 'sexo',
            'pai' => 'pai',
            'mae' => 'mae',
            'cod_foto' => 'cod_foto',
            'cpf' => 'cpf',
            'cod_cidade' => 'cod_cidade'
        );
    }

}

<?php

class Usuario extends ModelBamble
{

    /**
     *
     * @var integer
     */
    protected $id_usuario;

    /**
     *
     * @var string
     */
    protected $login;

    /**
     *
     * @var string
     */
    protected $senha;

    /**
     *
     * @var string
     */
    protected $last_login;

    /**
     *
     * @var string
     */
    protected $pre_last_login;

    /**
     *
     * @var integer
     */
    protected $ativo;

    /**
     * Method to set the value of field id_usuario
     *
     * @param integer $id_usuario
     * @return $this
     */
    public function setIdUsuario($id_usuario)
    {
        $this->id_usuario = $id_usuario;

        return $this;
    }

    /**
     * Method to set the value of field login
     *
     * @param string $login
     * @return $this
     */
    public function setLogin($login)
    {
        $this->login = $login;

        return $this;
    }

    /**
     * Method to set the value of field senha
     *
     * @param string $senha
     * @return $this
     */
    public function setSenha($senha)
    {
        $this->senha = $senha;

        return $this;
    }

    /**
     * Method to set the value of field last_login
     *
     * @param string $last_login
     * @return $this
     */
    public function setLastLogin($last_login)
    {
        $this->last_login = $last_login;

        return $this;
    }

    /**
     * Method to set the value of field pre_last_login
     *
     * @param string $pre_last_login
     * @return $this
     */
    public function setPreLastLogin($pre_last_login)
    {
        $this->pre_last_login = $pre_last_login;

        return $this;
    }

    /**
     * Method to set the value of field ativo
     *
     * @param integer $ativo
     * @return $this
     */
    public function setAtivo($ativo)
    {
        $this->ativo = $ativo;

        return $this;
    }

    /**
     * Returns the value of field id_usuario
     *
     * @return integer
     */
    public function getIdUsuario()
    {
        return $this->id_usuario;
    }

    /**
     * Returns the value of field login
     *
     * @return string
     */
    public function getLogin()
    {
        return $this->login;
    }

    /**
     * Returns the value of field senha
     *
     * @return string
     */
    public function getSenha()
    {
        return $this->senha;
    }

    /**
     * Returns the value of field last_login
     *
     * @return string
     */
    public function getLastLogin()
    {
        return $this->last_login;
    }

    /**
     * Returns the value of field pre_last_login
     *
     * @return string
     */
    public function getPreLastLogin()
    {
        return $this->pre_last_login;
    }

    /**
     * Returns the value of field ativo
     *
     * @return integer
     */
    public function getAtivo()
    {
        return $this->ativo;
    }

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("sch_usuario");
        $this->hasMany('id_usuario', 'GrupoUsuario', 'gu_cod_usuario', NULL);
        $this->hasMany('id_usuario', 'Permissao', 'p_cod_usuario', NULL);
        $this->belongsTo('id_usuario', 'Pessoa', 'id_pessoa', array('foreignKey' => true, 'alias' => 'Pessoa'));

        parent::initialize();
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return Usuario[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return Usuario
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return array(
            'id_usuario' => 'id_usuario',
            'login' => 'login',
            'senha' => 'senha',
            'last_login' => 'last_login',
            'pre_last_login' => 'pre_last_login',
            'ativo' => 'ativo'
        );
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'usuario';
    }

}


34.6k
Accepted
answer

It must be a hasOne relation instead of a hasMany relation

Further to what Andres said - you have the relationship set up between Pessoa and Usuario as a hasMany relationship. So a single instance of Pessoa has many related instances of Usuario. Because of this, you can't reference pessoa->usuario, because that refers to only a single instance. Changing the relationship to a hasOne will give you the relationship you want.

On an unrelated note, you don't need to override find() and findFirst() - all you're doing is calling the parent method anyway. Overriding doesn't break anything, it's just unneccessary.

Yes, that's it!

but this was changed? I get the feeling always used the mentioned way...

thanks!!!

thanks for you help!!!

but, in the case of 1-n?

suppose it was a case of one to many.

as it would be done?