XF 2.2 Entity Framework - Relationship on non-same values

stromb0li

Well-known member
I'm trying to wrap my head around entity framework today. I see you can specify a relationship like this:

PHP:
        $structure->relations = [
            'User' => [
                'entity' => 'XF:User',
                'type' => self::TO_ONE,
                'conditions' => 'user_id',
                'primary' => true,
            ]
        ];
        $structure->defaultWith = [
            'User'
        ];

However, if the table I have has a column called created_by_user_id, how do I specify the relationship is user -> user_id, but user_id maps to created_by_user_id?
 
You can specify more advanced conditions used a nested array syntax. String values prefixed with $ are evaluated as column names.

PHP:
$structure->relations = [
    'User' => [
        'entity' => 'XF:User',
        'type' => self::TO_ONE,
        'conditions' => [['user_id', '=', '$created_by_user_id']],
        'primary' => true,
    ]
];
 
You can specify more advanced conditions used a nested array syntax. String values prefixed with $ are evaluated as column names.

PHP:
$structure->relations = [
    'User' => [
        'entity' => 'XF:User',
        'type' => self::TO_ONE,
        'conditions' => [['user_id', '=', '$created_by_user_id']],
        'primary' => true,
    ]
];
Perfect! If I had two joins like from_user and to_user; would it look like this to get both user's info?

PHP:
$structure->relations = [
    'FromUser' => [
        'entity' => 'XF:User',
        'type' => self::TO_ONE,
        'conditions' => [['user_id', '=', '$from_user_id']],
        'primary' => true,
    ],
    'ToUser' => [
        'entity' => 'XF:User',
        'type' => self::TO_ONE,
        'conditions' => [['user_id', '=', '$to_user_id']],
        'primary' => true,
    ]
];
 
Back
Top Bottom