Cake\ORM\Query::leftJoinWith PHP Метод

leftJoinWith() публичный Метод

This function will add entries in the contain graph. ### Example: Get the count of articles per user $usersQuery ->select(['total_articles' => $query->func()->count('Articles.id')]) ->leftJoinWith('Articles') ->group(['Users.id']) ->autoFields(true); You can also customize the conditions passed to the LEFT JOIN: Get the count of articles per user with at least 5 votes $usersQuery ->select(['total_articles' => $query->func()->count('Articles.id')]) ->leftJoinWith('Articles', function ($q) { return $q->where(['Articles.votes >=' => 5]); }) ->group(['Users.id']) ->autoFields(true); This will create the following SQL: SELECT COUNT(Articles.id) AS total_articles, Users.* FROM users Users LEFT JOIN articles Articles ON Articles.user_id = Users.id AND Articles.votes >= 5 GROUP BY USers.id It is possible to left join deep associations by using dot notation ### Example: Total comments in articles by 'markstory' $query ->select(['total_comments' => $query->func()->count('Comments.id')]) ->leftJoinWith('Comments.Users', function ($q) { return $q->where(['username' => 'markstory']); ) ->group(['Users.id']); Please note that the query passed to the closure will only accept calling select, where, andWhere and orWhere on it. If you wish to add more complex clauses you can do it directly in the main query.
public leftJoinWith ( string $assoc, callable $builder = null )
$assoc string The association to join with
$builder callable a function that will receive a pre-made query object that can be used to add custom conditions or selecting some fields
    public function leftJoinWith($assoc, callable $builder = null)
    {
        $result = $this->eagerLoader()->matching($assoc, $builder, ['joinType' => 'LEFT', 'fields' => false]);
        $this->_addAssociationsToTypeMap($this->repository(), $this->typeMap(), $result);
        $this->_dirty();
        return $this;
    }