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

relation with column not work

My first model class:

class Ads extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->belongsTo(
            'app_id',
            Apps::class,
            'id',
            [
                'alias' => 'app',
                'reusable' => true,
                'params'=>[ // When I use this params, the parent relation not work!!!
                        'columns' => 'id, name, pid',
                ]
            ]
        );
    }
}

The second model class:

class Apps extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->belongsTo(
            'pid',
            self::class,
            'id',
            ['alias' => 'parent','reusable' => true,]
        );
    }
}

If I used params columns in model Ads, the second parent relation not work



10.1k

Can you tell me you db schema? Looks like second relation should be different. You are now linking the model to it self.



2.6k
edited Jul '19

Yes , the second mode is linking the model to it self by pid

ads:

CREATE TABLE `ads` (
    id int(11) unsigned auto_increment primary key,
  `name` varchar(50) NOT NULL DEFAULT '',
  `app_id` int(11) unsigned NOT NULL DEFAULT '0'
) 

apps:

create table `apps`
(
    id int(11) unsigned auto_increment primary key,
    pid int(11) unsigned default 0 not null,
    cp_id int(11) unsigned not null,
    name varchar(20) default '' not null
);


10.1k

Can you try to specify the alias also?

class Ads extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->belongsTo(
            'app_id',
            Apps::class,
            'id',
            [
                'alias' => 'app',
                'reusable' => true,
                'params'=>[ // When I use this params, the parent relation not work!!!
                        'columns' => 'app.id, app.name, app.pid',
                ]
            ]
        );
    }
}

Not sure why you want to get your relation only with some few columns, phalcon when selecting it will return Row object anyway, so not your related class.

Maybe you should just move id, name and pid to separeted table/model like AppsInstances or something? (well at least im figuring it out based on pid column)



2.6k

thrown

Unknown model or alias 'app' (11), when preparing: SELECT app.id, app.name, app.pid FROM

Can you try to specify the alias also?

class Ads extends \Phalcon\Mvc\Model
{
   public function initialize()
   {
       $this->belongsTo(
           'app_id',
           Apps::class,
           'id',
           [
               'alias' => 'app',
               'reusable' => true,
               'params'=>[ // When I use this params, the parent relation not work!!!
                       'columns' => 'app.id, app.name, app.pid',
               ]
           ]
       );
   }
}

Yes, because phalcon when selecting model using find/findFirst and columns will return Row object, instead of full object, not sure what you want to achieve, just remove columns.

Also i think you were already adding this thread. As i was proposing something else, it clearly looks like that you have on apps more data that you don't want to return, maybe just move name and pid to other model like AppsInfo or something like this? or AppsStatus since you have pid

If you are looking for the latest design, you can try here to find ther best solution.