From 70fba4d267b833ed4c1ba74282fb4f5a600fb26d Mon Sep 17 00:00:00 2001 From: paolo Date: Tue, 24 May 2016 16:56:14 +0200 Subject: [PATCH] - Added property hasSequence to class DB_Model. This property is used to indicate if the represented table has a primary key that uses a sequence (true, otherwise false. Default is true). - Method insert of DB_Model now checks the property hasSequence. If it is true it uses the method insert_id() to return the primary key of the row just inserted. For any other values the method returns the primary key inserted coping it directly from the parameters. - Set the property hasSequence to false in the constructor of model Prestudentstatus --- application/core/DB_Model.php | 25 +++++++++++++------ .../models/crm/Prestudentstatus_model.php | 5 ++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/application/core/DB_Model.php b/application/core/DB_Model.php index 6591e7288..e0c318961 100644 --- a/application/core/DB_Model.php +++ b/application/core/DB_Model.php @@ -4,6 +4,8 @@ class DB_Model extends FHC_Model { protected $dbTable; // Name of the DB-Table for CI-Insert, -Update, ... protected $pk; // Name of the PrimaryKey for DB-Update, Load, ... + protected $hasSequence; // False if this table has a composite primary key that is not using a sequence + // True if this table has a primary key that uses a sequence protected $acl; // Name of the PrimaryKey for DB-Update, Load, ... function __construct($dbTable = null, $pk = null) @@ -11,6 +13,7 @@ class DB_Model extends FHC_Model parent::__construct(); $this->dbTable = $dbTable; $this->pk = $pk; + $this->hasSequence = true; $this->load->database(); $this->acl = $this->config->item('fhc_acl'); } @@ -34,19 +37,27 @@ class DB_Model extends FHC_Model // DB-INSERT if ($this->db->insert($this->dbTable, $data)) { + // If the table has a primary key that uses a sequence + if ($this->hasSequence === true) + { + return $this->_success($this->db->insert_id()); + } // Avoid to use method insert_id() from CI because it forces to have a sequence // and doesn't return the primary key when it's composed by more columns - $primaryKeysArray = array(); - - foreach ($this->pk as $key => $value) + else { - if(isset($data[$value])) + $primaryKeysArray = array(); + + foreach ($this->pk as $key => $value) { - $primaryKeysArray[$value] = $data[$value]; + if (isset($data[$value])) + { + $primaryKeysArray[$value] = $data[$value]; + } } + + return $this->_success($primaryKeysArray); } - - return $this->_success($primaryKeysArray); } else return $this->_error($this->db->error(), FHC_DB_ERROR); diff --git a/application/models/crm/Prestudentstatus_model.php b/application/models/crm/Prestudentstatus_model.php index dbc6f3abe..8f6b57a41 100644 --- a/application/models/crm/Prestudentstatus_model.php +++ b/application/models/crm/Prestudentstatus_model.php @@ -1,7 +1,7 @@ dbTable = 'public.tbl_prestudentstatus'; $this->pk = array('ausbildungssemester', 'studiensemester_kurzbz', 'status_kurzbz', 'prestudent_id'); + $this->hasSequence = false; } -} +} \ No newline at end of file