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

Join return Row

Why when i try to make a join, the return class is ROW not the Parceiros class ? I've tried .... Thanks

    $fields = array(
        '\\Apps\\Models\\Parceiros.id',
        '\\Apps\\Models\\Parceiros.url',
        '\\Apps\\Models\\Parceiros.descricao',
        '\\Apps\\Models\\Parceiros.titulo',
        'm.path',
        'm.file'
    );

Short

$fields = array( '\Apps\Models\Parceiros.', 'm.', );

SQL

$select = Parceiros::query() ->leftJoin('\Apps\Models\Midias', 'm.id = \Apps\Models\Parceiros.id_midias', 'm') ->columns($fields);

Return Class

Phalcon\Mvc\Model\Row

edited Mar '15

Because you're requesting more than one instance so it's returning a Phalcon\Mvc\Model\Row composed of the requested instances

So doing a join it always return Phalcon Row, I cannot set the default or change ??

Andres, you do not have any alternative to do it ?

Thanks

edited Mar '15

This does not happen because of the join but due to: ->columns(array( "\Apps\Models\Parceiros.*", "m.*", )), you're requesting all fields from two instances which means returns a Phalcon\Mvc\Model\Row with two properties one per instance requested.

So has some way to access model, without have to do another looping ? Because i have some functions in model so have some way to access the instance model by the row ?

thanks andres

No, you just have to access the property you want:

foreach ($result as $row) {
  echo $row->parceiros->titulo, '<br>';
}

Thanks Andres, the only matter is that use apps\Models\Parceiros so returns a proprerty like it so i cannot access $row->parceiros->titulo or $row->apps\Models\Parceiros->titulo it's normal the return object had the full model name include namespace?

object(Phalcon\Mvc\Model\Row)#121 (4) { ["apps\Models\Parceiros"]=> object(Apps\Models\Parceiros)#103 (20) {

Thanks for the attention and the heplp andres!

Add an alias to the columns: ->columns(array("parceiros" => "Apps\Models\Parceiros.*", "midias" => "m.*"))

Nice solution, but now i have a SQL error

PhalconException: Syntax error, unexpected token AS, near to ' parceiros, m. AS midias FROM [Apps\Models\Parceiros] LEFT JOIN [\Apps\Models\Midias] AS [m] ON m.id = \Apps\Models\Parceiros.id_midias', when parsing: SELECT Apps\Models\Parceiros. AS parceiros, m.* AS midias FROM [Apps\Models\Parceiros] LEFT JOIN [\Apps\Models\Midias] AS [m] ON m.id = \Apps\Models\Parceiros.id_midias (169)

$select = Parceiros::query() ->leftJoin('\Apps\Models\Midias', 'm.id = \Apps\Models\Parceiros.id_midias', 'm') ->columns(array("parceiros" => "Apps\Models\Parceiros.", "midias" => "m."));

edited Mar '15

Andres look like this works but i get parceiros lost if i defined an Alias was not working the sql so i cannot access proprerty of parceiros unless i use toArray()

$item->m ( linked to midias, so i can acess the model midias, but model parceiros get lost ) $item->\Apps\Models\Parceiros cannot use or access because is not allowed to.

Thanks andres, looks like you are de Phalcon creator thanks for it for sure is the best framework i used for 4 years Zend but phalcon really wins!! Conglaturations and thanks for the help!!!!!

$fields = array( '\Apps\Models\Parceiros.', 'm.', );

    $select = Parceiros::query()
           ->leftJoin('\\Apps\\Models\\Midias', 'm.id = \\Apps\\Models\\Parceiros.id_midias', 'm')
           ->columns($fields);
        $execute = $select->execute();
    return ( $row ) ? $execute->getFirst() : $execute;

This works but only with modelsManager where i can set an Alias. Andres if you can explain me why? security, bug, to be developed , my mistake ?!

Thanks but a solution has been found!! =)

$fields = array( 'p.', 'm.', );

    $select = 
        $this->getModelsManager()->createBuilder()
               ->addFrom('\\Apps\\Models\\Parceiros', 'p')
               ->leftJoin('\\Apps\\Models\\Midias', 'm.id = p.id_midias', 'm')
               ->columns($fields);

    $execute = $select->getQuery()->execute();
    return ( $row ) ? $execute->getFirst() : $execute;