Jarves\ConditionOperator::standardConditionToSql PHP Метод

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

public standardConditionToSql ( Condition $condition, array &$params, string $objectKey, array &$usedFieldNames = null ) : string
$condition Jarves\Configuration\Condition
$params array
$objectKey string
$usedFieldNames array
Результат string
    public function standardConditionToSql(Condition $condition, &$params, $objectKey, array &$usedFieldNames = null)
    {
        if (!$condition->getRules()) {
            return '';
        }
        $rules = $condition->getRules();
        //        $tableName = null;
        //        $def = null;
        //
        //        if ($objectKey) {
        //            $def = $this->getJarves()->getObjects()->getDefinition($objectKey);
        //            if ($def) {
        //                $tableName = $def->getTable();
        //            }
        //        }
        //
        //        if (!$tableName) {
        //            $tableName = $objectKey;
        //        }
        if (is_array($rules) && !is_numeric(key($rules))) {
            //array( 'bla' => 'hui' );
            return $this->standardConditionToSql(Condition::create($this->primaryKeyToCondition($rules, $objectKey), $this->jarves), $params, $objectKey, $usedFieldNames);
        }
        if (isset($rules[0]) && is_array($rules[0]) && !is_numeric(key($rules[0]))) {
            //array( array('bla' => 'bla', ... );
            return $this->standardConditionToSql(Condition::create($this->primaryKeyToCondition($rules, $objectKey), $this->jarves), $params, $objectKey, $usedFieldNames);
        }
        if (!is_array($rules[0]) && !$rules[0] instanceof Condition) {
            //array( 1, 2, 3 );
            return $this->standardConditionToSql(Condition::create($this->primaryKeyToCondition($rules, $objectKey), $this->jarves), $params, $objectKey, $usedFieldNames);
        }
        return $this->conditionToSql($condition, $rules, $params, $objectKey, $usedFieldNames);
    }

Usage Example

Пример #1
0
 /**
  * @param ModelCriteria $query
  * @param \Jarves\Configuration\Condition $condition
  * @return \PDOStatement
  * @throws \PDOException
  */
 public function getStm(ModelCriteria $query, Condition $condition = null)
 {
     $params = [];
     $condition2Params = [];
     $id = hexdec(uniqid()) / mt_rand() + mt_rand();
     // check that the columns of the main class are already added (if this is the primary ModelCriteria)
     if (!$query->hasSelectClause() && !$query->getPrimaryCriteria()) {
         $query->addSelfSelectColumns();
     }
     $con = RuntimePropel::getServiceContainer()->getReadConnection($query->getDbName());
     $query->configureSelectColumns();
     $dbMap = RuntimePropel::getServiceContainer()->getDatabaseMap($query->getDbName());
     $db = RuntimePropel::getServiceContainer()->getAdapter($query->getDbName());
     $model = $query->getModelName();
     $tableMap = constant($model . '::TABLE_MAP');
     $query->setPrimaryTableName(constant($tableMap . '::TABLE_NAME'));
     //        $query->find($con);
     if ($condition) {
         $query->where($id . ' = ' . $id);
     }
     $sql = $query->createSelectSql($params);
     $conditionSql = '';
     if ($condition) {
         $condition2Params = $params;
         $conditionSql = $this->conditionOperator->standardConditionToSql($condition, $condition2Params, $this->getObjectKey());
     }
     if ($condition && $conditionSql) {
         $sql = str_replace($id . ' = ' . $id, '(' . $conditionSql . ')', $sql);
     }
     /** @var \PDOStatement $stmt */
     try {
         $stmt = $con->prepare($sql);
     } catch (\PDOException $e) {
         throw new PropelException('Could not execute query ' . $sql, 0, $e);
     }
     $db->bindValues($stmt, $params, $dbMap);
     if ($condition2Params) {
         foreach ($condition2Params as $idx => $v) {
             if (!is_array($v)) {
                 //propel uses arrays as bind values, we with Condition->toSql not.
                 $stmt->bindValue($idx, $v);
             }
         }
     }
     try {
         $stmt->execute();
     } catch (\PDOException $e) {
         throw new \PDOException($e->getMessage() . "\nSQL: {$sql}");
     }
     return $stmt;
 }