<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\Web\Classes\Db;

class DBWorkHLAusbildungsSchabloneDet extends \DBWorkGenerated
{
  public function __construct($pDBnr = 1, $pNewInstance = false)
  {
    parent::__construct($pDBnr, $pNewInstance);

    $this->_dataModulFields = array('id', 'ausbildung_schablone_id', 'fach', 'jahr',
      'fremdsprache', 'agbart', 'anzahl', 'anzahlart', 'noteja', 'zeitraum');

    $this->select = array_map(
      \FunctionOperations::prefixString('hlasdet.'),
      $this->_dataModulFields
    );
  }

  public function getNewObject()
  {
    return new \Kufer\Web\Classes\Datamodul\HLAusbildungsSchabloneDet();
  }

  public function getTableName(): string
  {
    return 'hlausb_schabl_det';
  }

  public function getAllQuery(): \SelectQueryBuilder
  {
    return $this->getSelectQueryBuilder()
      ->select($this->select)
      ->from($this->getTableName(), 'hlasdet')
      ->paramRaw('table', 'hlasdet');
  }

  public function findByFilterQuery(\Kufer\Web\Classes\Dbfilter\HLAusbildungSchablonenDetSQLFilter $filter): \SelectQueryBuilder
  {
    $condition = $filter->createSql($this->dboperation);

    return $this->getAllQuery()
      ->and($condition);
  }

  
  public function findByFilter(\Kufer\Web\Classes\Dbfilter\HLAusbildungSchablonenDetSQLFilter $filter): array
  {
    $builder = $this->findByFilterQuery($filter);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRows();
  }

  
  public function findFirstByFilter(\Kufer\Web\Classes\Dbfilter\HLAusbildungSchablonenDetSQLFilter $filter): \Kufer\Web\Classes\Datamodul\HLAusbildungsSchabloneDet
  {
    $builder = $this->findByFilterQuery($filter);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getFirstRow();
  }

  
  public function countByFilter(\Kufer\Web\Classes\Dbfilter\HLAusbildungSchablonenDetSQLFilter $filter): int
  {
    $builder = $this->findByFilterQuery($filter)
      ->select('COUNT(*) AS anzahl');

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQLForInnerQuery());

    $this->dboperation->selectrow(0);

    return (int)$this->getValue('anzahl', \DatamodulVarType::TYPE_INT);
  }
}