Beispiel #1
0
 /**
  * Adds a specific field
  *
  * @param string $name
  * @param string $value
  * @param ApiInterface $entity
  * @param string $alias
  *
  * @return void
  */
 private function addSearchField($name, $value, ApiInterface $entity, $alias)
 {
     $operator = "=";
     if (preg_match("/[<|>]\$/", $name)) {
         $operator = substr($name, -1) . "=";
         $name = substr($name, 0, -1);
         $fieldTypes = $entity->getFieldTypes();
         $fieldTypesWhiteList = [ApiInterface::FIELD_TYPE_DATE, ApiInterface::FIELD_TYPE_DATE_TIME, ApiInterface::FIELD_TYPE_DOUBLE, ApiInterface::FIELD_TYPE_INT];
         if (!in_array($fieldTypes[$name], $fieldTypesWhiteList)) {
             throw new Exception('Cannot perform ' . $operator . ' search on any fields that are not in ' . implode(", ", $fieldTypesWhiteList) . ': ' . $name . ' has a type of ' . $fieldTypes[$name], 400);
         }
     }
     $sql = $alias . '.' . $name;
     if (is_array($value)) {
         $attributes = [];
         foreach ($value as $subValue) {
             $entity->writeAttribute($name, $subValue);
             $attributes[] = $entity->readAttribute($name);
         }
         $this->getCriteria()->inWhere($sql, $attributes);
     } else {
         $entity->writeAttribute($name, $value);
         $attribute = $entity->readAttribute($name);
         $params = [];
         if (is_null($attribute)) {
             $sql .= " IS NULL";
         } else {
             $attribute .= '';
             //Make sure to convert to string, for Date and DateTime
             if (preg_match('@(^|[^\\\\])%@', $attribute)) {
                 $operator = ' LIKE ';
             }
             $sql .= $operator . '?0';
             $params[] = $attribute;
         }
         $this->getCriteriaHelper()->andWhere($sql, $params);
     }
 }