Wiki : DeleteOperations
Documentation Home :: Categories :: Index :: Recent Changes :: Comments :: Search :: Help :: Login/RegisterDelete Operations
deleteObjectByID
/**
* Delete an object by its ID.
*
* @param tablename The tablename key for the PNTables structure
* @param id The ID of the object to delete
* @param idcolumn The column which contains the ID field (optional) (default='id')
*
* @return The result from the delete operation
*/
function deleteObjectByID ($tablename, $id, $idcolumn='id')
* Delete an object by its ID.
*
* @param tablename The tablename key for the PNTables structure
* @param id The ID of the object to delete
* @param idcolumn The column which contains the ID field (optional) (default='id')
*
* @return The result from the delete operation
*/
function deleteObjectByID ($tablename, $id, $idcolumn='id')
deleteObjectByID ($tablename, $id, $idcolumn='id')
This function will delete one row in the database by the id field. Two examples:
DBUtil::deleteObjectById ('mytable', 4);
DBUtil::deleteObjectById ('customers', $customerid, 'cust_id');
DBUtil::deleteObjectById ('customers', $customerid, 'cust_id');
We can also cheat a little with something like this:
DBUtil::deleteObjectById ('customers', 'USA', 'country');
This would delete multiple rows since the ID field specified is no unique. This provides a nice little shortcut.
deleteObject
/**
* Generate and execute a delete SQL statement for the given object
*
* @param object The object we wish to update
* @param tablename The tablename key for the PNTables structure
* @param where The where clause to use (optional) (default='')
* @param idcolumn The column which contains the ID field (optional) (default='id')
*
* @return The result from the delete operation
*/
function deleteObject ($object, $tablename, $where='', $idcolumn='id')
* Generate and execute a delete SQL statement for the given object
*
* @param object The object we wish to update
* @param tablename The tablename key for the PNTables structure
* @param where The where clause to use (optional) (default='')
* @param idcolumn The column which contains the ID field (optional) (default='id')
*
* @return The result from the delete operation
*/
function deleteObject ($object, $tablename, $where='', $idcolumn='id')
deleteObject ($object, $tablename, $where='', $idcolumn='')
deleteObject uses the $object to build a where clause. This is useful because one can simple parse an object directly from an input form for example to be deleted. No need to build any where clauses (and have to use pnTables).
NOTE You cannot specify an object *and* and where clause together, one must be blank. To use a where clause you should use pnTables and make object an empty array.
// using an object to build the where clause
DBUtil::deleteObject ($obj, 'customers');
// building a where clause
$pntables = pnDBGetTables();
$column = $pntables['customers_column'];
$where = "WHERE $column[name]='Fred' AND $column[country]='Netherlands'";
DBUtil::deleteObject (array(), 'customers', $where);
DBUtil::deleteObject ($obj, 'customers');
// building a where clause
$pntables = pnDBGetTables();
$column = $pntables['customers_column'];
$where = "WHERE $column[name]='Fred' AND $column[country]='Netherlands'";
DBUtil::deleteObject (array(), 'customers', $where);
deleteWhere
/**
* Delete (an) object(s) via a where clause
*
* @param tablename The tablename key for the PNTables structure
* @param where The where-clause to use
*
* @return The result from the delete operation
*/
function deleteWhere ($tablename, $where)
* Delete (an) object(s) via a where clause
*
* @param tablename The tablename key for the PNTables structure
* @param where The where-clause to use
*
* @return The result from the delete operation
*/
function deleteWhere ($tablename, $where)
deleteWhere ($tablename, $where)
This function allows us to create a custom where clause. Remember to use pnTables to get the correct column names:
$pntables = pnDBGetTables();
$column = $pntables['customers_column'];
$where = "WHERE $column[name]='Fred' AND $column[country]='Netherlands'";
DBUtil::deleteWhere ('customers', $where);
$column = $pntables['customers_column'];
$where = "WHERE $column[name]='Fred' AND $column[country]='Netherlands'";
DBUtil::deleteWhere ('customers', $where);
If the $where clause blank it will effectively empty the table.
Previous | Top | Next
CategoryDeveloperDocs
