Code Duplication    Length = 58-58 lines in 2 locations

Alpha/Model/ActiveRecordProviderMySQL.php 1 location

@@ 968-1025 (lines=58) @@
965
            }
966
967
            try {
968
                foreach ($properties as $propObj) {
969
                    $propName = $propObj->name;
970
971
                    if ($this->BO->getPropObject($propName) instanceof Relation) {
972
                        $prop = $this->BO->getPropObject($propName);
973
974
                        // handle the saving of MANY-TO-MANY relation values
975
                        if ($prop->getRelationType() == 'MANY-TO-MANY' && count($prop->getRelatedOIDs()) > 0) {
976
                            try {
977
                                try {
978
                                    // check to see if the rel is on this class
979
                                    $side = $prop->getSide(get_class($this->BO));
980
                                } catch (IllegalArguementException $iae) {
981
                                    $side = $prop->getSide(get_parent_class($this->BO));
982
                                }
983
984
                                $lookUp = $prop->getLookup();
985
986
                                // first delete all of the old RelationLookup objects for this rel
987
                                try {
988
                                    if ($side == 'left') {
989
                                        $lookUp->deleteAllByAttribute('leftID', $this->BO->getOID());
990
                                    } else {
991
                                        $lookUp->deleteAllByAttribute('rightID', $this->BO->getOID());
992
                                    }
993
                                } catch (\Exception $e) {
994
                                    throw new FailedSaveException('Failed to delete old RelationLookup objects on the table ['.$prop->getLookup()->getTableName().'], error is ['.$e->getMessage().']');
995
                                }
996
997
                                $OIDs = $prop->getRelatedOIDs();
998
999
                                if (isset($OIDs) && !empty($OIDs[0])) {
1000
                                    // now for each posted OID, create a new RelationLookup record and save
1001
                                    foreach ($OIDs as $oid) {
1002
                                        $newLookUp = new RelationLookup($lookUp->get('leftClassName'), $lookUp->get('rightClassName'));
1003
                                        if ($side == 'left') {
1004
                                            $newLookUp->set('leftID', $this->BO->getOID());
1005
                                            $newLookUp->set('rightID', $oid);
1006
                                        } else {
1007
                                            $newLookUp->set('rightID', $this->BO->getOID());
1008
                                            $newLookUp->set('leftID', $oid);
1009
                                        }
1010
                                        $newLookUp->save();
1011
                                    }
1012
                                }
1013
                            } catch (\Exception $e) {
1014
                                throw new FailedSaveException('Failed to update a MANY-TO-MANY relation on the object, error is ['.$e->getMessage().']');
1015
1016
                                return;
1017
                            }
1018
                        }
1019
1020
                        // handle the saving of ONE-TO-MANY relation values
1021
                        if ($prop->getRelationType() == 'ONE-TO-MANY') {
1022
                            $prop->setValue($this->BO->getOID());
1023
                        }
1024
                    }
1025
                }
1026
            } catch (\Exception $e) {
1027
                throw new FailedSaveException('Failed to save object, error is ['.$e->getMessage().']');
1028

Alpha/Model/ActiveRecordProviderSQLite.php 1 location

@@ 983-1040 (lines=58) @@
980
            }
981
982
            try {
983
                foreach ($properties as $propObj) {
984
                    $propName = $propObj->name;
985
986
                    if ($this->BO->getPropObject($propName) instanceof Relation) {
987
                        $prop = $this->BO->getPropObject($propName);
988
989
                        // handle the saving of MANY-TO-MANY relation values
990
                        if ($prop->getRelationType() == 'MANY-TO-MANY' && count($prop->getRelatedOIDs()) > 0) {
991
                            try {
992
                                try {
993
                                    // check to see if the rel is on this class
994
                                    $side = $prop->getSide(get_class($this->BO));
995
                                } catch (IllegalArguementException $iae) {
996
                                    $side = $prop->getSide(get_parent_class($this->BO));
997
                                }
998
999
                                $lookUp = $prop->getLookup();
1000
1001
                                // first delete all of the old RelationLookup objects for this rel
1002
                                try {
1003
                                    if ($side == 'left') {
1004
                                        $lookUp->deleteAllByAttribute('leftID', $this->BO->getOID());
1005
                                    } else {
1006
                                        $lookUp->deleteAllByAttribute('rightID', $this->BO->getOID());
1007
                                    }
1008
                                } catch (Exception $e) {
1009
                                    throw new FailedSaveException('Failed to delete old RelationLookup objects on the table ['.$prop->getLookup()->getTableName().'], error is ['.$e->getMessage().']');
1010
                                }
1011
1012
                                $OIDs = $prop->getRelatedOIDs();
1013
1014
                                if (isset($OIDs) && !empty($OIDs[0])) {
1015
                                    // now for each posted OID, create a new RelationLookup record and save
1016
                                    foreach ($OIDs as $oid) {
1017
                                        $newLookUp = new RelationLookup($lookUp->get('leftClassName'), $lookUp->get('rightClassName'));
1018
                                        if ($side == 'left') {
1019
                                            $newLookUp->set('leftID', $this->BO->getOID());
1020
                                            $newLookUp->set('rightID', $oid);
1021
                                        } else {
1022
                                            $newLookUp->set('rightID', $this->BO->getOID());
1023
                                            $newLookUp->set('leftID', $oid);
1024
                                        }
1025
                                        $newLookUp->save();
1026
                                    }
1027
                                }
1028
                            } catch (Exception $e) {
1029
                                throw new FailedSaveException('Failed to update a MANY-TO-MANY relation on the object, error is ['.$e->getMessage().']');
1030
1031
                                return;
1032
                            }
1033
                        }
1034
1035
                        // handle the saving of ONE-TO-MANY relation values
1036
                        if ($prop->getRelationType() == 'ONE-TO-MANY') {
1037
                            $prop->setValue($this->BO->getOID());
1038
                        }
1039
                    }
1040
                }
1041
            } catch (Exception $e) {
1042
                throw new FailedSaveException('Failed to save object, error is ['.$e->getMessage().']');
1043
            }