->delete()

->delete() – Delete items from table

Synopsis

int $DB_DataObject->delete ( boolean $useWhere )

Description

Deletes data from the database, either using primary key or based on a whereAdd() method call. By default the delete will base its query on the set variables, however if you wish to use the whereAdd() method you should set the $useWhere parameter to DB_DATAOBJECT_WHEREADD_ONLY.

Parameter

  • boolean $use_where - use the whereAdd() conditions (by default, delete will only use primary keys)

Return value

int number of rows affected or FALSE on failure

Throws

Possible PEAR_Error values
Error code Error message Meaning Solution
DB_* "*" see PEAR::DB see PEAR::DB
DB_DATAOBJECT_ERROR_NODATA "delete: No Data specifed for query $condition"    

Note

This function can not be called statically.

Example

Simple Delete

<?php
$person 
= new DataObjects_Person;
$person->get(12);
$person->delete();

$person       = new DataObjects_Person;
$person->name 'test';
$person->age  21;
$person->delete();

$person = new DataObjects_Person;
$person->whereAdd('age < 21');
$person->delete(DB_DATAOBJECT_WHEREADD_ONLY);
?>

Resulting SQL

<?php
SELECT 
FROM person WHERE person.id 12
DELETE FROM person WHERE 
person.id 12 )

DELETE FROM person WHERE person.name 'test' ) AND ( person.age 21 )

DELETE FROM person WHERE age 21 )
?>
Update objects variables into database (Previous) Advanced Filters - ::
query(), ::
SelectAdd(), ::
whereAdd(), ::
Limit(), ::
OrderBy(), ::
GroupBy(), (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: tmilos1@gmail.com
Caution is needed, if we have a multicolumn primary key.
eg. primary key for person is (name, age)

$person = new DataObjects_Person;
$person->name = 'test';
$person->age = $some_age;
$person->delete();

If $some_age is not set, then the $person->delete() method will delete all person's with the name 'test'.
In the case where the primary key is one column, we are protected from this kind of errors with raised error 'delete: No condition specifed for query'.