public function insert($pa_options = null) { $vb_we_set_transaction = false; if ($this->inTransaction()) { $o_trans = $this->getTransaction(); } else { $o_trans = new Transaction(); $this->setTransaction($o_trans); $vb_we_set_transaction = true; } $o_db = $o_trans->getDb(); list($vs_target_table, $vs_target_key) = $this->_getTarget(); $vs_rel_table = $this->tableName(); $t_target = $this->getAppDatamodel()->getInstanceByTableName($vs_target_table); $vn_target_id = $this->get($vs_target_key); if (!$t_target->load($vn_target_id)) { // invalid object $this->postError(720, _t("Related %1 does not exist", $t_target->getProperty('NAME_SINGULAR')), "BaseRepresentationRelationship->insert()"); return false; } if (!$this->get('is_primary')) { // force is_primary to be set if no other represention is so marked // is there another rep for this object marked is_primary? $qr_res = $o_db->query("\n\t\t\t\t\tSELECT oxor.relation_id\n\t\t\t\t\tFROM {$vs_rel_table} oxor\n\t\t\t\t\tINNER JOIN ca_object_representations AS o_r ON o_r.representation_id = oxor.representation_id\n\t\t\t\t\tWHERE\n\t\t\t\t\t\toxor.{$vs_target_key} = ? AND oxor.is_primary = 1 AND o_r.deleted = 0\n\t\t\t\t", (int) $vn_target_id); if (!$qr_res->nextRow()) { // nope - force this one to be primary $this->set('is_primary', 1); } $vb_rc = parent::insert($pa_options); if ($vb_we_set_transaction) { $o_trans->commitTransaction(); } return $vb_rc; } else { // unset other reps is_primary field //$o_db->beginTransaction(); $o_db->query("\n\t\t\t\t\tUPDATE {$vs_rel_table}\n\t\t\t\t\tSET is_primary = 0\n\t\t\t\t\tWHERE\n\t\t\t\t\t\t{$vs_target_key} = ?\n\t\t\t\t", (int) $vn_target_id); if (!($vb_rc = parent::insert($pa_options))) { if ($vb_we_set_transaction) { $o_trans->rollbackTransaction(); } } else { if ($vb_we_set_transaction) { $o_trans->commitTransaction(); } } return $vb_rc; } }
/** * */ public function insert($pa_options = null) { if (!$this->get('effective_date', array('getDirectDate' => true))) { $this->set('effective_date', $this->_getMovementDate()); $this->set('source_info', $this->_getStorageLocationInfo()); } return parent::insert($pa_options); }