Fix deletion for class build with modulebuilder
This commit is contained in:
parent
62a5869db4
commit
e020597ba7
@ -7274,16 +7274,45 @@ abstract class CommonObject
|
|||||||
/**
|
/**
|
||||||
* Delete object in database
|
* Delete object in database
|
||||||
*
|
*
|
||||||
* @param User $user User that deletes
|
* @param User $user User that deletes
|
||||||
* @param bool $notrigger false=launch triggers after, true=disable triggers
|
* @param bool $notrigger false=launch triggers after, true=disable triggers
|
||||||
* @return int <0 if KO, >0 if OK
|
* @param int $forcechilddeletion 0=no, 1=Force deletion of children
|
||||||
|
* @return int <=0 if KO, >0 if OK
|
||||||
*/
|
*/
|
||||||
public function deleteCommon(User $user, $notrigger = false)
|
public function deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
|
||||||
{
|
{
|
||||||
$error=0;
|
$error=0;
|
||||||
|
|
||||||
$this->db->begin();
|
$this->db->begin();
|
||||||
|
|
||||||
|
if ($forcechilddeletion)
|
||||||
|
{
|
||||||
|
foreach($this->childtables as $table)
|
||||||
|
{
|
||||||
|
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.$table.' WHERE '.$this->fk_element.' = '.$this->id;
|
||||||
|
$resql = $this->db->query($sql);
|
||||||
|
if (! $resql)
|
||||||
|
{
|
||||||
|
$this->error=$this->db->lasterror();
|
||||||
|
$this->errors[]=$this->error;
|
||||||
|
$this->db->rollback();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elseif (! empty($this->fk_element) && ! empty($this->childtables)) // If object has childs linked with a foreign key field, we check all child tables.
|
||||||
|
{
|
||||||
|
$objectisused = $this->isObjectUsed($this->id);
|
||||||
|
if (! empty($objectisused))
|
||||||
|
{
|
||||||
|
dol_syslog(get_class($this)."::deleteCommon Can't delete record as it has some child", LOG_WARNING);
|
||||||
|
$this->error='ErrorRecordHasChildren';
|
||||||
|
$this->errors[]=$this->error;
|
||||||
|
$this->db->rollback();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (! $error) {
|
if (! $error) {
|
||||||
if (! $notrigger) {
|
if (! $notrigger) {
|
||||||
// Call triggers
|
// Call triggers
|
||||||
|
|||||||
@ -55,6 +55,16 @@ class EmailCollector extends CommonObject
|
|||||||
*/
|
*/
|
||||||
public $picto = 'generic';
|
public $picto = 'generic';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int Field with ID of parent key if this field has a parent
|
||||||
|
*/
|
||||||
|
public $fk_element = 'fk_emailcollector';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array Array of child tables (child tables to delete before deleting a record)
|
||||||
|
*/
|
||||||
|
protected $childtables=array('emailcollector_emailcollectorfilter', 'emailcollector_emailcollectoraction');
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 'type' if the field format.
|
* 'type' if the field format.
|
||||||
@ -382,7 +392,7 @@ class EmailCollector extends CommonObject
|
|||||||
*/
|
*/
|
||||||
public function delete(User $user, $notrigger = false)
|
public function delete(User $user, $notrigger = false)
|
||||||
{
|
{
|
||||||
return $this->deleteCommon($user, $notrigger);
|
return $this->deleteCommon($user, $notrigger, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -58,9 +58,6 @@ class EmailCollectorAction extends CommonObject
|
|||||||
public $picto = 'emailcollectoraction@emailcollector';
|
public $picto = 'emailcollectoraction@emailcollector';
|
||||||
|
|
||||||
|
|
||||||
public $fk_element = 'fk_emailcollector';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 'type' if the field format.
|
* 'type' if the field format.
|
||||||
* 'label' the translation key.
|
* 'label' the translation key.
|
||||||
|
|||||||
@ -336,6 +336,7 @@ class MyObject extends CommonObject
|
|||||||
public function delete(User $user, $notrigger = false)
|
public function delete(User $user, $notrigger = false)
|
||||||
{
|
{
|
||||||
return $this->deleteCommon($user, $notrigger);
|
return $this->deleteCommon($user, $notrigger);
|
||||||
|
//return $this->deleteCommon($user, $notrigger, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user