コード例 #1
0
 /**
  * Fetch related, foreign rows for one record's ManyToMany relationships.
  * @param  string $table_name
  * @param  string $foreign_table
  * @param  string $junction_table
  * @param  string $junction_key_left
  * @param  string $junction_key_right
  * @param  string $column_equals
  * @return array                      Foreign rowset
  */
 public function loadManyToManyRelationships($table_name, $foreign_table, $junction_table, $junction_key_left, $junction_key_right, $column_equals)
 {
     $foreign_table_pk = TableSchema::getTablePrimaryKey($foreign_table);
     $foreign_join_column = "{$foreign_table}.{$foreign_table_pk}";
     $junction_join_column = "{$junction_table}.{$junction_key_right}";
     $junction_comparison_column = "{$junction_table}.{$junction_key_left}";
     $junction_table_pk = TableSchema::getTablePrimaryKey($junction_table);
     $junction_id_column = "{$junction_table}." . $junction_table_pk;
     // Less likely name collision:
     $junction_id_column_alias = "directus_junction_id_column_518d31856e131";
     $junction_sort_column_alias = "directus_junction_sort_column_518d318e3f0f5";
     $junctionSelectColumns = array($junction_id_column_alias => $junction_table_pk);
     $sql = new Sql($this->adapter);
     $select = $sql->select();
     // If the Junction Table has a Sort column, do eet.
     // @todo is this the most efficient way?
     // @hint TableSchema#getUniqueColumnName
     $junctionColumns = TableSchema::getAllNonAliasTableColumnNames($junction_table);
     if (in_array('sort', $junctionColumns)) {
         $junctionSelectColumns[$junction_sort_column_alias] = "sort";
         $select->order($junction_sort_column_alias);
     }
     $select->from($foreign_table)->join($junction_table, "{$foreign_join_column} = {$junction_join_column}", $junctionSelectColumns)->where(array($junction_comparison_column => $column_equals))->order("{$junction_id_column} ASC");
     // Only select the fields not on the currently authenticated user group's read field blacklist
     $columns = TableSchema::getAllNonAliasTableColumnNames($foreign_table);
     $select->columns($columns);
     $ForeignTable = new RelationalTableGateway($this->acl, $foreign_table, $this->adapter);
     $results = $ForeignTable->selectWith($select);
     $results = $results->toArray();
     $foreign_data = array();
     $columns = TableSchema::getAllNonAliasTableColumns($foreign_table);
     foreach ($results as $row) {
         $row = $this->parseRecordValuesByMysqlType($row, $columns);
         $junction_table_id = (int) $row[$junction_id_column_alias];
         unset($row[$junction_id_column_alias]);
         $entry = array($junction_table_pk => $junction_table_id);
         if (in_array('sort', $junctionColumns)) {
             // @TODO: check why is this a string instead of an integer.
             $entry['sort'] = (int) $row[$junction_sort_column_alias];
             unset($row[$junction_sort_column_alias]);
         }
         $entry['data'] = $row;
         $foreign_data[] = $entry;
     }
     return array('rows' => $foreign_data);
 }
コード例 #2
0
 /**
  * Fetch related, foreign rows for one record's ManyToMany relationships.
  *
  * @param  string $table_name
  * @param  string $foreign_table
  * @param  string $junction_table
  * @param  string $junction_key_left
  * @param  string $junction_key_right
  * @param  string $column_equals
  * @param  string $parentField
  * @param  int    $level
  *
  * @return array                      Foreign rowset
  */
 public function loadManyToManyRelationships($table_name, $foreign_table, $junction_table, $junction_key_left, $junction_key_right, $column_equals, $parentField = null, $level = 0)
 {
     $foreign_table_pk = TableSchema::getTablePrimaryKey($foreign_table);
     $foreign_join_column = $foreign_table . '.' . $foreign_table_pk;
     $junction_join_column = $junction_table . '.' . $junction_key_right;
     $junction_comparison_column = $junction_table . '.' . $junction_key_left;
     // =============================================================================
     // HOTFIX: prevent infinite circle loop
     // =============================================================================
     if ($parentField && $this->hasToManyCallStack($parentField, $foreign_table)) {
         return $column_equals;
     }
     if ($parentField !== null) {
         $this->addToManyCallStack($level, $parentField, $foreign_table);
     }
     $junction_table_pk = TableSchema::getTablePrimaryKey($junction_table);
     $junction_id_column = $junction_table . '.' . $junction_table_pk;
     // Less likely name collision:
     $junction_id_column_alias = 'directus_junction_id_column_518d31856e131';
     $junction_sort_column_alias = 'directus_junction_sort_column_518d318e3f0f5';
     $junctionSelectColumns = [$junction_id_column_alias => $junction_table_pk];
     $sql = new Sql($this->adapter);
     $select = $sql->select();
     // If the Junction Table has a Sort column, do eet.
     // @todo is this the most efficient way?
     // @hint TableSchema#getUniqueColumnName
     $junctionColumns = TableSchema::getAllNonAliasTableColumnNames($junction_table);
     if (in_array('sort', $junctionColumns)) {
         $junctionSelectColumns[$junction_sort_column_alias] = 'sort';
         $select->order($junction_sort_column_alias);
     }
     $select->from($foreign_table)->join($junction_table, $foreign_join_column . '=' . $junction_join_column, $junctionSelectColumns)->where([$junction_comparison_column => $column_equals])->order($junction_id_column . ' ASC');
     // Only select the fields not on the currently authenticated user group's read field blacklist
     $columns = TableSchema::getAllNonAliasTableColumnNames($foreign_table);
     $select->columns($columns);
     $ForeignTable = new RelationalTableGateway($this->acl, $foreign_table, $this->adapter);
     $results = $ForeignTable->selectWith($select);
     $results = $results->toArray();
     $foreign_data = [];
     $columns = TableSchema::getAllNonAliasTableColumns($foreign_table);
     foreach ($results as $row) {
         $row = $recordData = SchemaManager::parseRecordValuesByType($row, $columns);
         $junction_table_id = (int) $row[$junction_id_column_alias];
         unset($row[$junction_id_column_alias]);
         $entry = [$junction_table_pk => $junction_table_id];
         if (in_array('sort', $junctionColumns)) {
             // @TODO: check why is this a string instead of an integer.
             $entry['sort'] = (int) $row[$junction_sort_column_alias];
             unset($row[$junction_sort_column_alias]);
         }
         $schemaArray = TableSchema::getSchemaArray($foreign_table);
         $alias_fields = $this->filterSchemaAliasFields($schemaArray);
         // (fmrly $alias_schema)
         $row = $this->loadToManyRelationships($row, $alias_fields, $parentField, $level + 1);
         $entry['data'] = $row;
         $foreign_data[] = $entry;
     }
     return ['rows' => $foreign_data];
 }