public function toArrayWithImmediateRelationships(RelationalTableGateway $TableGateway) { if ($this->table !== $TableGateway->getTable()) { throw new \InvalidArgumentException('The table of the gateway parameter must match this row\'s table.'); } $entry = $this->toArray(); $schemaArray = TableSchema::getSchemaArray($this->table); $aliasColumns = $TableGateway->filterSchemaAliasFields($schemaArray); // Many-to-One list($entry) = $TableGateway->loadManyToOneRelationships($schemaArray, [$entry]); // One-to-Many, Many-to-Many $entry = $TableGateway->loadToManyRelationships($entry, $aliasColumns); return $entry; }
public function __construct(Acl $acl, AdapterInterface $adapter) { parent::__construct($acl, self::$_tableName, $adapter); self::$defaultEntriesSelectParams = array('orderBy' => 'id', 'orderDirection' => 'DESC', 'fields' => '*', 'id' => -1, 'search' => null, STATUS_COLUMN_NAME => null); }
/** * 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); }
/** * 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]; }
public static function getTable($tbl_name) { $acl = Bootstrap::get('acl'); $zendDb = Bootstrap::get('ZendDb'); if (!self::canGroupViewTable($tbl_name)) { return false; } $sql = "SELECT T.TABLE_NAME AS id,\n T.TABLE_NAME AS table_name,\n CREATE_TIME AS date_created,\n TABLE_COMMENT AS comment,\n ifnull(hidden,0) as hidden,\n ifnull(single,0) as single,\n is_junction_table,\n user_create_column,\n user_update_column,\n date_create_column,\n date_update_column,\n footer,\n TABLE_ROWS AS count\n FROM INFORMATION_SCHEMA.TABLES T\n LEFT JOIN directus_tables DT ON (DT.table_name = T.TABLE_NAME)\n WHERE T.TABLE_SCHEMA = :schema AND T.TABLE_NAME = :table_name"; $sth = $zendDb->query($sql); $parameterContainer = new ParameterContainer(); $parameterContainer->offsetSet(':table_name', $tbl_name, ParameterContainer::TYPE_STRING); $parameterContainer->offsetSet(':schema', $zendDb->getCurrentSchema(), ParameterContainer::TYPE_STRING); $result = $sth->execute($parameterContainer); $info = $result->current(); if ($info) { $info['hidden'] = (bool) $info['hidden']; $info['single'] = (bool) $info['single']; $info['footer'] = (bool) $info['footer']; $info['is_junction_table'] = (bool) $info['is_junction_table']; } $relationalTableGateway = new RelationalTableGateway($acl, $tbl_name, $zendDb); $info = array_merge($info, $relationalTableGateway->countActiveOld()); $info['columns'] = self::getSchemaArray($tbl_name); $directusPreferencesTableGateway = new DirectusPreferencesTableGateway($acl, $zendDb); $currentUser = Auth::getUserInfo(); $info['preferences'] = $directusPreferencesTableGateway->fetchByUserAndTable($currentUser['id'], $tbl_name); return $info; }
public static function getTable($tbl_name) { $acl = Bootstrap::get('acl'); $zendDb = Bootstrap::get('ZendDb'); // TODO: getTable should return an empty object // or and empty array instead of false // in any given situation that the table // can be find or used. if (!self::canGroupViewTable($tbl_name)) { return false; } $info = SchemaManager::getTable($tbl_name); if (!$info) { return false; } if ($info) { $info['count'] = (int) $info['count']; $info['date_created'] = DateUtils::convertToISOFormat($info['date_created'], 'UTC', get_user_timezone()); $info['hidden'] = (bool) $info['hidden']; $info['single'] = (bool) $info['single']; $info['footer'] = (bool) $info['footer']; } $relationalTableGateway = new RelationalTableGateway($acl, $tbl_name, $zendDb); $info = array_merge($info, $relationalTableGateway->countActiveOld()); $info['columns'] = self::getSchemaArray($tbl_name); $directusPreferencesTableGateway = new DirectusPreferencesTableGateway($acl, $zendDb); $currentUser = Auth::getUserInfo(); $info['preferences'] = $directusPreferencesTableGateway->fetchByUserAndTable($currentUser['id'], $tbl_name); return $info; }
public function __construct(Acl $acl, AdapterInterface $adapter) { parent::__construct($acl, self::$_tableName, $adapter); self::$defaultEntriesSelectParams = ['orderBy' => 'id', 'orderDirection' => 'DESC', 'fields' => '*', 'id' => -1, 'search' => null, 'status' => null]; }