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

Join in model

hi I need select with join in model, example:

SELECT
    u.id, u.name, p.project_name
FROM
    user AS u
    JOIN project AS p ON p.user_id = u.id

how do it without phql?



58.4k
edited Mar '15

Hey man

You can use example below :

/**
 * Returns users which doesn't belong to any organization.
 *
 * @return mixed
 */
public static function getUnassigned()
{
    $query = User::query()
        ->columns(__NAMESPACE__ . '\User.*')
        ->leftJoin(__NAMESPACE__ . '\OrganizationUser', __NAMESPACE__ . '\User.id = ou.idUser', 'ou')
        ->where('ou.idUser IS NULL')
        ->execute();

    if ($query->count()) {
        return $query;
    }

    return false;
}

Also you can refer to here https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Criteria.html