<?php

namespace MGModule\DNSManager2\models\testGroup\testItem;
use MGModule\DNSManager2 as main;
use MGModule\DNSManager2\mgLibs;
use MGModule\DNSManager2\models\testGroup\simpleItem;

/**
 * Description of repository
 *
 * @author Michal Czech <michael@modulesgarden.com>
 */
class repository extends \MGModule\DNSManager2\mgLibs\models\repository{
    public function getModelClass() {
        return __NAMESPACE__.'\testItem';
    }
    
    public function get() {      
        $sql = "
            SELECT
                ".mgLibs\MySQL\query::formatSelectFields(testItem::fieldDeclaration(),'B')."
                ,count(S.`". simpleItem\simpleItem::getProperyColumn('id')."`) as simpleNum
            FROM
                ".testItem::tableName()." B
            LEFT JOIN
                ".simpleItem\simpleItem::tableName()." S
                ON
                    S.`". simpleItem\simpleItem::getProperyColumn('testItemID')."` = B.`".testItem::getProperyColumn('id')."`
        ";

        $conditionParsed = mgLibs\MySQL\query::parseConditions($this->_filters,$params,'B');

        if($conditionParsed)
        {
            $sql .= " WHERE ".$conditionParsed;
        }
        
        $sql .= " GROUP BY `B`.`".testItem::getProperyColumn('id')."` ";

        
        $sql .= mgLibs\MySQL\query::formarLimit($this->_limit, $this->_offest);

        $result = mgLibs\MySQL\query::query($sql,$params);
        
        $output = array();
        
        $class = $this->getModelClass();
        
        while($row = $result->fetch())
        {
            $output[] = new $class($row['id'],$row);
        }

        return $output;
    }
}
