Horde_Rdo_Mapper::delete PHP Method

delete() public method

Deletes a record from the backend. $object can be either a primary key, an Rdo_Query object, or an Rdo object.
public delete ( string | Horde_Rdo_Base | Horde_Rdo_Query $object ) : integer
$object string | Horde_Rdo_Base | Horde_Rdo_Query The Rdo object, Horde_Rdo_Query, or unique id to delete.
return integer Number of objects deleted.
    public function delete($object)
    {
        if ($object instanceof Horde_Rdo_Base) {
            $key = $this->primaryKey;
            $id = $object->{$key};
            $query = array($key => $id);
        } elseif ($object instanceof Horde_Rdo_Query) {
            $query = $object;
        } else {
            $key = $this->primaryKey;
            $query = array($key => $object);
        }
        $query = Horde_Rdo_Query::create($query, $this);
        $clauses = array();
        $bindParams = array();
        foreach ($query->tests as $test) {
            $clauses[] = $this->adapter->quoteColumnName($test['field']) . ' ' . $test['test'] . ' ?';
            $bindParams[] = $test['value'];
        }
        if (!$clauses) {
            throw new Horde_Rdo_Exception('Refusing to delete the entire table.');
        }
        $sql = 'DELETE FROM ' . $this->adapter->quoteTableName($this->table) . ' WHERE ' . implode(' ' . $query->conjunction . ' ', $clauses);
        return $this->adapter->delete($sql, $bindParams);
    }

Usage Example

Example #1
0
 /**
  * Deletes a stock item from the backend. $object can be either a
  * primary key, an Rdo_Query object, or a Sesha_Entity_Stock object.
  * This also cleans up attached attributes and categories
  *
  * @param string|Sesha_Entity_Stock|Horde_Rdo_Query $object The Rdo object,
  * Horde_Rdo_Query, or unique id to delete.
  *
  * @return integer Number of objects deleted.
  */
 public function delete($object)
 {
     if (!$object instanceof Sesha_Entity_Stock) {
         $object = $this->findOne($object);
     }
     foreach ($object->values as $value) {
         $value->delete();
     }
     $object->removeRelation('categories');
     return parent::delete($object);
 }