Cake\Database\ValueBinder::bind PHP Method

bind() public method

Associates a query placeholder to a value and a type
public bind ( string | integer $param, mixed $value, string | integer $type = 'string' ) : void
$param string | integer placeholder to be replaced with quoted version of $value
$value mixed The value to be bound
$type string | integer the mapped type name, used for casting when sending to database
return void
    public function bind($param, $value, $type = 'string')
    {
        $this->_bindings[$param] = compact('value', 'type') + ['placeholder' => is_int($param) ? $param : substr($param, 1)];
    }

Usage Example

 /**
  * Convert the values into a SQL string with placeholders.
  *
  * @param \Cake\Database\ValueBinder $generator Placeholder generator object
  * @return string
  */
 public function sql(ValueBinder $generator)
 {
     if (empty($this->_values) && empty($this->_query)) {
         return '';
     }
     $i = 0;
     $defaults = array_fill_keys($this->_columns, null);
     foreach ($this->_values as $row) {
         $row = array_merge($defaults, $row);
         foreach ($row as $column => $value) {
             $type = $this->typeMap()->type($column);
             $generator->bind($i++, $value, $type);
         }
     }
     if ($this->query()) {
         return ' ' . $this->query()->sql($generator);
     }
     $placeholders = [];
     $numColumns = count($this->_columns);
     $rowPlaceholders = implode(', ', array_fill(0, $numColumns, '?'));
     $placeholders = array_fill(0, count($this->_values), $rowPlaceholders);
     return sprintf(' VALUES (%s)', implode('), (', $placeholders));
 }
All Usage Examples Of Cake\Database\ValueBinder::bind