FOF30\Model\DataModel\Relation\BelongsToMany::getCountSubquery PHP Méthode

getCountSubquery() public méthode

Returns the count subquery for DataModel's has() and whereHas() methods.
public getCountSubquery ( string $tableAlias = null ) : JDatabaseQuery
$tableAlias string The alias of the local table in the query. Leave blank to use the table's name.
Résultat JDatabaseQuery
    public function getCountSubquery($tableAlias = null)
    {
        /** @var DataModel $foreignModel */
        $foreignModel = $this->getForeignModel();
        $foreignModel->setIgnoreRequest(true);
        $db = $foreignModel->getDbo();
        if (empty($tableAlias)) {
            $tableAlias = $this->parentModel->getTableName();
        }
        $query = $db->getQuery(true)->select('COUNT(*)')->from($db->qn($foreignModel->getTableName()) . ' AS ' . $db->qn('reltbl'))->innerJoin($db->qn($this->pivotTable) . ' AS ' . $db->qn('pivotTable') . ' ON(' . $db->qn('pivotTable') . '.' . $db->qn($this->pivotForeignKey) . ' = ' . $db->qn('reltbl') . '.' . $db->qn($foreignModel->getFieldAlias($this->foreignKey)) . ')')->where($db->qn('pivotTable') . '.' . $db->qn($this->pivotLocalKey) . ' =' . $db->qn($tableAlias) . '.' . $db->qn($this->parentModel->getFieldAlias($this->localKey)));
        return $query;
    }

Usage Example

Exemple #1
0
    /**
     * @group           BelongsToMany
     * @group           BelongsToManyGetCountSubquery
     * @covers          FOF30\Model\DataModel\Relation\BelongsToMany::getCountSubquery
     */
    public function testGetCountSubquery()
    {
        $model = new Groups(static::$container);
        $relation = new BelongsToMany($model, 'Parts');
        $result = $relation->getCountSubquery();
        $check = '
SELECT COUNT(*)
FROM `#__fakeapp_parts` AS `reltbl`
INNER JOIN `#__fakeapp_parts_groups` AS `pivotTable` ON(`pivotTable`.`fakeapp_part_id` = `reltbl`.`fakeapp_part_id`)
WHERE `pivotTable`.`fakeapp_group_id` =`#__fakeapp_groups`.`fakeapp_group_id`';
        $this->assertInstanceOf('\\JDatabaseQuery', $result, 'BelongsToMany::getCountSubquery Should return an instance of Query');
        $this->assertEquals($check, (string) $result, 'BelongsToMany::getCountSubquery Failed to return the correct query');
    }