Relationship data is null.

Hi all.
Please help me.

Model Relationships.

# User Model
user_id int PRI
icon_id int
...

# UserIcon Model
user_id int PRI
icon_id int PRI
...

# Icon Model
icon_id int PRI
...

PHP Code.

# User Model Class
public function initialize()
{
    parent::initialize();
    $this->hasOne(
        ['user_id', 'icon_id'],
        '\Application\Models\Icon',
        ['user_id', 'icon_id'],
        ['alias' => 'UserIcon'],
    );
}
$user->UserIcon //false

Execution result is false

Execute Query Log.

SELECT
...
FROM user_icon
WHERE user_id = 1 AND icon_id = NULL;

'icon_id' on the MySQL is not null.
Why?

Versions.

  • mysql Ver 14.14 Distrib 5.6.27, for Linux (x86_64) using EditLine wrapper
  • PHP 5.6.15
  • Phalcon DevTools (2.0.8)


10.6k

I've not seen relationships like that. I would write:

    $this->hasOne('id',Icon,'user_id');

Then when you query it:

    $icon = $user->getIcon();