Пример #1
0
 private static function hasNoCompositeRelatives(Wallop $compObj, $compColumn, $thisRelationTable, $thisId, $thisColumn, array $compIds = null)
 {
     global $database;
     // Initialize the list of ids and the inList
     $ids = array();
     $inList = '(';
     if (isset($compIds)) {
         if (empty($compIds)) {
             return array();
         }
         foreach ($compIds as $compId) {
             $ids[$compId] = true;
             $inList .= $compId . ',';
         }
         $inList = trim($inList, ',') . ')';
     } else {
         // If compIds is not set then query from the database the ids
         $query = "SELECT `{$compColumn}` FROM `{$thisRelationTable}` ";
         $query .= "WHERE `{$thisColumn}` = {$thisId}";
         $result = $database->execQuery($query);
         if (!$result) {
             $staticError = 'Failed to retrieve records of this composite (`' . $thisRelationTable . '`) ';
             $staticError .= 'This was likely due to a mismatch between the database structure and the ';
             $staticError .= 'columns and/or table name you specified in the constructor of this object!';
             self::$staticErrors[] = $staticError;
             return false;
         }
         $rows = $database->getAllRows();
         // Returned an empty result set
         // Just finish the function and return an empty array
         if (!$rows || empty($rows)) {
             return array();
         }
         $i = 0;
         $numRows = count($rows);
         while ($i != $numRows) {
             $row = $rows[$i];
             $ids[$row[$compColumn]] = true;
             $inList .= $row[$compColumn] . ',';
             ++$i;
         }
         $inList = trim($inList, ',') . ')';
         unset($rows);
     }
     // Foreach relation of this composite remove keys from the master array for composites that
     // only have any other composite relations to them
     $compRelationTypes = array(&$compObj->aggregates, &$compObj->composites);
     foreach ($compRelationTypes as $compRelationType) {
         foreach ($compRelationType as $compRelationTableName => $compRelation) {
             $compRelationData = $compObj->findRelationData($compRelationTableName);
             // If this relation has the composite also as a composite relation
             $compRelationObj = new $compRelationData['className']();
             if (isset($compRelationObj->composites[$thisRelationTable])) {
                 // Generate relation columns
                 $relationColumns = $compRelationObj->generateRelationColumns($compRelationTableName, $compObj);
                 if (!$relationColumns) {
                     end($compRelationObj->errors);
                     self::$staticErrors[] = $compRelationObj->errors[key($compRelationObj->errors)];
                     return false;
                 }
                 $compRelationColumn = $relationColumns['this'];
                 $secondCompColumn = $relationColumns['relative'];
                 $query = "SELECT `{$secondCompColumn}` ";
                 $query .= "FROM `{$compRelationTableName}` ";
                 $query .= "WHERE `{$secondCompColumn}` IN {$inList}";
                 // Add a claus to not
                 if ($thisRelationTable == $compRelationTableName) {
                     $query .= " AND `{$compRelationColumn}` != {$thisId}";
                 }
                 $result = $database->execQuery($query);
                 if (!$result) {
                     $staticError = 'Failed to retrieve records of this composite (`';
                     $staticError .= $thisRelationTable . '`) This was likely due to a mismatch between ';
                     $staticError .= 'the database structure and the columns and/or table name you ';
                     $staticError .= 'specified in the constructor of this object!';
                     self::$staticErrors[] = $staticError;
                     return false;
                 }
                 $rows = $database->getAllRows();
                 $rowId = 0;
                 $numRows = count($rows);
                 while ($rowId != $numRows) {
                     $id = $rows[$rowId][$secondCompColumn];
                     unset($ids[$id]);
                     $inList = str_replace_once(',' . $id . ',', ',', $inList, $found);
                     if (!$found) {
                         unset($found);
                         $inList = str_replace_once('(' . $id . ',', '(', $inList, $found);
                         if (!$found) {
                             $inList = str_replace_once(',' . $id . ')', ')', $inList);
                         }
                     }
                     ++$rowId;
                 }
             }
         }
     }
     $outputArray = array();
     foreach ($ids as $id => $unused) {
         $outputArray[] = $id;
     }
     return $outputArray;
 }