Horde_Rdo_Mapper::create PHP Method

create() public method

Create a new object in the backend with $fields as initial values.
public create ( array $fields ) : Horde_Rdo_Base
$fields array Array of field names => initial values.
return Horde_Rdo_Base The newly created object.
    public function create($fields)
    {
        // If configured to record creation and update times, set them
        // here. We set updated_at to the initial creation time so it's
        // always set.
        if ($this->_setTimestamps) {
            $time = time();
            $fields['created_at'] = $time;
            $fields['updated_at'] = $time;
        }
        // Filter out any extra fields.
        $fields = array_intersect_key($fields, array_flip($this->tableDefinition->getColumnNames()));
        if (!$fields) {
            throw new Horde_Rdo_Exception('create() requires at least one field value.');
        }
        $sql = 'INSERT INTO ' . $this->adapter->quoteTableName($this->table);
        $keys = array();
        $placeholders = array();
        $bindParams = array();
        foreach ($fields as $field => $value) {
            $keys[] = $this->adapter->quoteColumnName($field);
            $placeholders[] = '?';
            $bindParams[] = $value;
        }
        $sql .= ' (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $placeholders) . ')';
        $id = $this->adapter->insert($sql, $bindParams);
        return $this->map(array_merge($fields, array($this->primaryKey => $id)));
    }

Usage Example

Example #1
0
 /**
  * Creates a property definition in the backend.
  * This wraps folding of the 'parameters' structure 
  *
  * @param array $property  An array with the property definition.
  *                         Keys may be
  *                          property_id Integer (autogenerated)
  *                          property    string The property name
  *                          datatype    string The property type
  *                          parameters  mixed Type definition parameters will be serialized
  *                          unit        string The unit to display
  *                          description string
  *                          priority    integer
  *
  * @return Sesha_Entity_Property  The property created.
  */
 public function create(array $property)
 {
     if (!is_string($property['parameters'])) {
         $property['parameters'] = serialize($property['parameters']);
     }
     if (array_key_exists('property_id', $property) && $property['property_id'] == null) {
         unset($property['property_id']);
     }
     return parent::create($property);
 }