This are the two classes: User and MediaLink and the usage:
class User extends Core
{
public $iOriginId;
public $dtCreated;
public $dtUpdated;
public $dtLastLogin;
public $dtLogin;
public $iStatus;
public $sEmail;
public $sPassword;
public $sFirstName;
public $sPrefix;
public $sLastName;
public $sUserName;
public $iBadLoginCount;
public $dtLocked;
public $sPasswordReset;
public $bMenuState;
public $sTokenKey;
public $sToken;
public $sSource;
public function initialize() {
parent::initialize();
$this->modelsManager->addHasOne(
$this,
['sSource', 'id'],
'\Cms\Media\Models\Inzite\MediaLink',
['sModel', 'iModelId'],
['alias' => 'medialink']
);
}
}
class MediaLink extends Core {
public $dtCreated;
public $dtUpdated;
public $iMediaId;
public $sModel;
public $iModelId;
public $iWidth;
public $iHeight;
public $iCropX;
public $iCropWidth;
public $iCropY;
public $iCropHeight;
public $iScale;
public $sSource;
public function initialize() {
parent::initialize();
$this->modelsManager->addHasOne(
$this,
'iMediaId',
__NAMESPACE__ . '\Media',
'id',
['alias' => 'model']
);
$this->modelsManager->addHasOne(
$this,
['sModel', 'iModelId'],
'\Cms\User\Models\Inzite\User',
['sSource', 'id'],
['alias' => 'user']
);
}
}
Usage:
$oUser = User::findFirst(1);
$oUser->getMediaLink();
That has no results. Phalcon is creating a query with this WHERE stament:
WHERE `medialink`.`sModel` = NULL AND `medialink`.`iModelId` = NULL LIMIT 1
When I define the relation as below, it works fine, but then the result is not specific enough.
$this->modelsManager->addHasOne(
$this,
'sSource',
'\Cms\Media\Models\Inzite\MediaLink',
'sModel',
['alias' => 'medialink']
);
or
$this->modelsManager->addHasOne(
$this,
'id',
'\Cms\Media\Models\Inzite\MediaLink',
'iModelId',
['alias' => 'medialink']
);