Merge pull request #18987 from atm-gauthier/NEW_more_param_on_function_deleteByParentField

NEW : we need to be able to put more filters on deleteByParentField() function
This commit is contained in:
Laurent Destailleur 2021-10-16 19:11:52 +02:00 committed by GitHub
commit 1bee543eef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View File

@ -9056,10 +9056,15 @@ abstract class CommonObject
$className = str_replace('@', '', $deleteFromObject[0]);
$filePath = $deleteFromObject[1];
$columnName = $deleteFromObject[2];
$TMoreSQL = array();
$more_sql = $deleteFromObject[3];
if (!empty($more_sql)) {
$TMoreSQL['customsql'] = $more_sql;
}
if (dol_include_once($filePath)) {
$childObject = new $className($this->db);
if (method_exists($childObject, 'deleteByParentField')) {
$result = $childObject->deleteByParentField($this->id, $columnName);
$result = $childObject->deleteByParentField($this->id, $columnName, $TMoreSQL);
if ($result < 0) {
$error++;
$this->errors[] = $childObject->error;
@ -9141,10 +9146,12 @@ abstract class CommonObject
*
* @param int $parentId Parent Id
* @param string $parentField Name of Foreign key parent column
* @param array $filter an array filter
* @param string $filtermode AND or OR
* @return int <0 if KO, >0 if OK
* @throws Exception
*/
public function deleteByParentField($parentId = 0, $parentField = '')
public function deleteByParentField($parentId = 0, $parentField = '', $filter = array(), $filtermode = "AND")
{
global $user;
@ -9157,6 +9164,23 @@ abstract class CommonObject
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql .= " WHERE ".$parentField." = ".(int) $parentId;
// Manage filters
$sqlwhere = array();
if (count($filter) > 0) {
foreach ($filter as $key => $value) {
if ($key == 'customsql') {
$sqlwhere[] = $value;
} elseif (strpos($value, '%') === false) {
$sqlwhere[] = $key." IN (".$this->db->sanitize($this->db->escape($value)).")";
} else {
$sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
}
}
}
if (count($sqlwhere) > 0) {
$sql .= " AND (".implode(" ".$filtermode." ", $sqlwhere).")";
}
$resql = $this->db->query($sql);
if (!$resql) {
$this->errors[] = $this->db->lasterror();

View File

@ -154,7 +154,7 @@ class Job extends CommonObject
// * If name matches '@ClassNAme:FilePathClass;ParentFkFieldName' it will
// * call method deleteByParentField(parentId, ParentFkFieldName) to fetch and delete child object
// */
// protected $childtablesoncascade = array('hrm_jobdet');
protected $childtablesoncascade = array("@SkillRank:hrm/class/skillrank.class.php:fk_object:objecttype='job'");
// /**
// * @var JobLine[] Array of subtable lines