Exemplo n.º 1
0
 /**
  * Determines if the specified array is a valid Activerecord options array.
  *
  * @param array $array An options array
  * @param bool $throw True to throw an Exceptions if not valid
  * @return boolean True if valid otherwise valse
  * @throws {@link Activerecord} if the array contained any invalid options
  */
 public static function isOptionsHash($array, $throw = true)
 {
     $utils = new Utils();
     if ($utils->isHash($array)) {
         $keys = \array_keys($array);
         $diff = \array_diff($keys, self::$valid_options);
         if (!empty($diff) && $throw) {
             throw new ExceptionActiverecord("Unknown key(s): " . \join(', ', $diff));
         }
         $intersect = \array_intersect($keys, self::$valid_options);
         if (!empty($intersect)) {
             return true;
         }
     }
     return false;
 }
Exemplo n.º 2
0
 public function optionsToSql($options)
 {
     $table = \array_key_exists('from', $options) ? $options['from'] : $this->getFullyQualifiedTableName();
     $sql = new SQLBuilder($this->conn, $table);
     if (\array_key_exists('joins', $options)) {
         $sql->joins($this->createJoins($options['joins']));
         // by default, an inner join will not fetch the fields from the joined table
         if (!\array_key_exists('select', $options)) {
             $options['select'] = $this->getFullyQualifiedTableName() . '.*';
         }
     }
     if (\array_key_exists('select', $options)) {
         $sql->select($options['select']);
     }
     if (\array_key_exists('conditions', $options)) {
         if (!Utils::isHash($options['conditions'])) {
             if (\is_string($options['conditions'])) {
                 $options['conditions'] = [$options['conditions']];
             }
             \call_user_func_array([$sql, 'where'], $options['conditions']);
         } else {
             if (!empty($options['mapped_names'])) {
                 $options['conditions'] = $this->mapNames($options['conditions'], $options['mapped_names']);
             }
             $sql->where($options['conditions']);
         }
     }
     if (\array_key_exists('order', $options)) {
         $sql->order($options['order']);
     }
     if (\array_key_exists('limit', $options)) {
         $sql->limit($options['limit']);
     }
     if (\array_key_exists('offset', $options)) {
         $sql->offset($options['offset']);
     }
     if (\array_key_exists('group', $options)) {
         $sql->group($options['group']);
     }
     if (\array_key_exists('having', $options)) {
         $sql->having($options['having']);
     }
     return $sql;
 }
Exemplo n.º 3
0
 private function applyWhereConditions($args)
 {
     require_once 'Expressions.php';
     $num_args = \count($args);
     if ($num_args == 1 && Utils::isHash($args[0])) {
         $hash = \is_null($this->joins) ? $args[0] : $this->prependTableNameToFields($args[0]);
         $e = new Expressions($this->connection, $hash);
         $this->where = $e->toString();
         $this->where_values = Utils::arrayFlatten($e->values());
     } elseif ($num_args > 0) {
         // if the values has a nested array then we'll need to use Expressions to expand the bind marker for us
         $values = \array_slice($args, 1);
         // foreach ($values as $name => &$value) oiginal
         foreach ($values as &$value) {
             if (\is_array($value)) {
                 $e = new Expressions($this->connection, $args[0]);
                 $e->bindValues($values);
                 $this->where = $e->toString();
                 $this->where_values = Utils::arrayFlatten($e->values());
                 return;
             }
         }
         // no nested array so nothing special to do
         $this->where = $args[0];
         $this->where_values =& $values;
     }
 }