JTable/canDelete
From Joomla! Documentation
< API16:JTable
The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.
Description[edit]
Generic check for whether dependancies exist for this object in the database schema
Syntax[edit]
canDelete($pk=null, $joins=null)
Parameter Name | Default Value | Description |
---|---|---|
$pk | null | An optional primary key value check the row for. If not set the instance property value is used. |
$joins | null | An optional array to compiles standard joins formatted like: [label => 'Label', name => 'table name' , idfield => 'field', joinfield => 'field'] |
Returns[edit]
boolean True on success.
Defined in[edit]
libraries/joomla/database/table.php
Importing[edit]
jimport( 'joomla.database.table' );
Source Body[edit]
public function canDelete($pk = null, $joins = null)
{
// Initialise variables.
$k = $this->_tbl_key;
$pk = (is_null($pk)) ? $this->$k : $pk;
// If no primary key is given, return false.
if ($pk === null) {
return false;
}
if (is_array($joins)) {
// Get a query object.
$query = $this->_db->getQuery(true);
// Setup the basic query.
$query->select('`'.$this->_tbl_key.'`');
$query->from('`'.$this->_tbl.'`');
$query->where('`'.$this->_tbl_key.'` = '.$this->_db->quote($this->$k));
$query->group('`'.$this->_tbl_key.'`');
// For each join add the select and join clauses to the query object.
foreach($joins as $table) {
$query->select('COUNT(DISTINCT '.$table['idfield'].') AS '.$table['idfield']);
$query->join('LEFT', $table['name'].' ON '.$table['joinfield'].' = '.$k);
}
// Get the row object from the query.
$this->_db->setQuery((string) $query, 0, 1);
$row = $this->_db->loadObject();
// Check for a database error.
if ($this->_db->getErrorNum()) {
$this->setError($this->_db->getErrorMsg());
return false;
}
$msg = array();
$i = 0;
foreach($joins as $table) {
$k = $table['idfield'] . $i;
if ($obj->$k) {
$msg[] = JText::_($table['label']);
}
$i++;
}
if (count($msg)) {
$this->setError("noDeleteRecord" . ": " . implode(', ', $msg));
return false;
} else {
return true;
}
}
return true;
}
Examples[edit]
Code Examples[edit]