DboSource::value PHP Method

value() public method

Returns a quoted and escaped string of $data for use in an SQL statement.
public value ( string $data, string $column = null ) : string
$data string String to be prepared for use in an SQL statement
$column string The column datatype into which this data will be inserted.
return string Quoted and escaped data
    public function value($data, $column = null)
    {
        if (is_array($data) && !empty($data)) {
            return array_map(array(&$this, 'value'), $data, array_fill(0, count($data), $column));
        } elseif (is_object($data) && isset($data->type, $data->value)) {
            if ($data->type === 'identifier') {
                return $this->name($data->value);
            } elseif ($data->type === 'expression') {
                return $data->value;
            }
        } elseif (in_array($data, array('{$__cakeID__$}', '{$__cakeForeignKey__$}'), true)) {
            return $data;
        }
        if ($data === null || is_array($data) && empty($data)) {
            return 'NULL';
        }
        if (empty($column)) {
            $column = $this->introspectType($data);
        }
        switch ($column) {
            case 'binary':
                return $this->_connection->quote($data, PDO::PARAM_LOB);
            case 'boolean':
                return $this->_connection->quote($this->boolean($data, true), PDO::PARAM_BOOL);
            case 'string':
            case 'text':
                return $this->_connection->quote($data, PDO::PARAM_STR);
            default:
                if ($data === '') {
                    return 'NULL';
                }
                if (is_float($data)) {
                    return str_replace(',', '.', strval($data));
                }
                if (is_int($data) || $data === '0' || is_numeric($data) && strpos($data, ',') === false && $data[0] != '0' && strpos($data, 'e') === false) {
                    return $data;
                }
                return $this->_connection->quote($data);
        }
    }

Usage Example

Example #1
0
 /**
  * Test that value() quotes set values even when numeric.
  *
  * @return void
  */
 public function testSetValue()
 {
     $column = "set('a','b','c')";
     $result = $this->Dbo->value('1', $column);
     $this->assertEquals("'1'", $result);
     $result = $this->Dbo->value(1, $column);
     $this->assertEquals("'1'", $result);
     $result = $this->Dbo->value('a', $column);
     $this->assertEquals("'a'", $result);
 }
All Usage Examples Of DboSource::value
DboSource