Quantcast
Channel: Editor — DataTables forums
Viewing all articles
Browse latest Browse all 3744

Join() One-to-many relationship

$
0
0

I have two tables: users with id (1,2) and view_users with user_id (1). When I use Join() the result that I get appears as a regular left join relationship and not as a join with one-to-many relationship.

$editor = Editor::inst( $db, 'users', 'id' )
->fields(Field::inst( 'users.id' ), Field::inst( 'users.username' ))
->join(
Join::inst( 'users_view', 'array' )
->join( 'id', 'user_id' )
->fields( Field::inst( 'user_id' ) ->validator( 'Validate::required' ))
);

I expect to get one record with user_id=1 only, but instead I get both of them (without second user_id):

{
    "data": [
        {
            "DT_RowId": "row_1",
            "users": {
                "id": "1",
                "username": "john"
            },
            "users_view": [
                {
                    "user_id": "1"
                }
            ]
        },
        {
            "DT_RowId": "row_2",
            "users": {
                "id": "2",
                "username": "steve"
            },
            "users_view": []
        }
    ],
    "options": []
}

I can use something like this instead:

$editor->where( 'users_view.user_id', $_SESSION['user'] )
->leftJoin( 'users_view', 'users_view.user_id', '=', 'users.id' );

However, this trick won't work in all other cases that I'll need to implement.

How do I specify in the code, using Join() alone that users_view, without user_id, shouldn't appear at all?

Thanks.


Viewing all articles
Browse latest Browse all 3744