Пример #1
0
 /**
  * Build an InternalQueryHelper to select models
  *
  * @param array $fields     - selected fields
  * @param array $where      - associative array ex:
  *                          simple criteria       array('idMarque' => 1)
  *                          custom operator       array('idMarque' => array('operator' => '<=','value' => '5'))
  *                          raw SQL without operator       array('idMarque' => array('IN (5,6,4)')
  * @param array $order      - associative array ex:array('libMarque'=>'ASC')
  * @param int   $limitStart - int
  * @param int   $limitEnd   - int
  *
  * @return InternalQueryHelper
  */
 protected static function buildSelectQuery($fields = array('*'), $where = array(), $order = array(), $limitStart = null, $limitEnd = null)
 {
     // get the formatted model mysql table name with database name
     $modelTableName = static::formatTableNameMySQL();
     // be sure that "*" is prefixed with model table name
     foreach ($fields as &$oneField) {
         if ($oneField == "*") {
             $oneField = $modelTableName . ".*";
             break;
         }
     }
     // create and helper to build a sql query
     $helper = new InternalQueryHelper();
     // prefix columns with model table name
     $where = $helper->prefixWhereWithTable($where, $modelTableName);
     $orders = $helper->prefixOrderWithTable($order, $modelTableName);
     // starting build select
     $helper->select($fields)->from($modelTableName);
     // check one to one relation with auto get fields
     // and append necessary fields to select
     $nbRelation = 0;
     foreach (static::$_relations as $uneRelation) {
         if ($uneRelation['typeRelation'] == self::ONE_TO_ONE && count($uneRelation['autoGetFields']) > 0) {
             // prefix fields
             foreach ($uneRelation['autoGetFields'] as &$oneField) {
                 $oneField = 'rel' . $nbRelation . "." . $oneField;
             }
             // add fields to select
             $helper->select($uneRelation['autoGetFields']);
             // add query join corresponding to the relation
             $helper->leftJoin($uneRelation['classRelation']::formatTableNameMySQL() . ' rel' . $nbRelation, 'rel' . $nbRelation . '.`' . $uneRelation['targetField'] . '` = ' . $modelTableName . '.`' . $uneRelation['sourceField'] . '`');
             // increment relation count used in prefix
             $nbRelation++;
         }
     }
     // build where clause
     $helper->buildWhereFromArray($where);
     // build order clause
     foreach ($orders as $orderField => $orderVal) {
         $helper->orderBy($orderField, $orderVal);
     }
     // build limit clause
     $helper->limit($limitStart, $limitEnd);
     return $helper;
 }