/** * Join against any tables referenced by foreign keys in order to get a * reasonable value to display for them. If the foreign key in the listing * table is nullable, we'll use a LEFT JOIN so that a missing foreign key * value does not exclude the record from the result set. * * We follow a naming convention for these values in result sets: the * foreign key ends with "_id" and the value in the result set is aliased to * that foreign key column name minus "_id". For example, let's say the * foreign key column was "favorite_book_id". In the result set for the * listing query, we'd include an alias of "favorite_book" that pointed * to the "title" column of the referenced "books" table. That way, both * the integer favorite_book_id and the title favorite_book are available * when we render the listing. * * @param Select $select * @return Select */ private function selectForeignKeyValues(Select $select) { $tableAlias = $this->getAlias($this->table->getTableName()); $tableInstances = []; foreach ($this->table->getMetadata('references') as $column => $reference) { $metadata = $this->table->getMetadata('columns', $column); if ($metadata['NULLABLE']) { $join = 'joinLeft'; } else { $join = 'join'; } $refTable = $reference['table']; if (!array_key_exists($refTable, $tableInstances)) { $tableInstances[$refTable] = 0; } $refAlias = $this->getAlias($refTable, $tableInstances[$refTable], $column); $tableInstances[$refTable] += 1; $columnAlias = preg_replace('/_id$/i', '', $column); $titleColumn = $this->findReferenceTitleColumn($column, $reference, $refAlias); $select->{$join}([$refAlias => $refTable], sprintf('%s = %s', $this->db->quoteIdentifier("{$tableAlias}.{$column}"), $this->db->quoteIdentifier("{$refAlias}.{$reference['column']}")), [$columnAlias => $titleColumn]); } return $select; }
/** * Check to see if this field is a foreign key. Calling isType('reference') * will automatically delegate to this method. * * @return boolean */ protected function isTypeReference() { return (bool) $this->table->getMetadata('references', $this->name); }
/** * Get a list of the field names available from this provider. * * @return array */ public function getAllNames() { return array_keys($this->table->getMetadata('columns')); }
/** * Instantiate the row, checking to ensure the data array contains only * those columns present in the table's metadata. * * @param Table $table * @param array $data */ public function __construct(Table $table, array $data = array()) { $this->table = $table; $this->data = $data; $this->columns = $this->table->getRowColumns(); // Apply defaults for new rows if (!count($this->data)) { foreach ($this->columns as $column) { $columnMetadata = $this->table->getMetadata('columns', $column); $default = $columnMetadata['DEFAULT']; // We skip temporal fields to avoid problems with certain defaults (e.g., CURRENT_TIMESTAMP, NOW()) if (!in_array($columnMetadata['GENERIC_TYPE'], ['date', 'time', 'timestamp']) && null !== $default) { $this->data[$column] = $default; } } } // Unset any data values not present in the columns array foreach ($this->data as $column => $value) { if (!in_array($column, $this->columns)) { unset($this->data[$column]); } if ($this->table->hasManyToManyRelationship($column)) { $this->virtualFieldsInitialized[] = $column; } elseif ($this->table->hasEav() && $this->table->getEav()->hasAttribute($column)) { $this->virtualFieldsInitialized[] = $column; } } $this->init(); }
/** * Instantiate the row, checking to ensure the data array contains only * those columns present in the table's metadata. * * @param Table $table * @param array $data */ public function __construct(Table $table, array $data = array()) { $this->table = $table; $this->data = $data; $this->columns = $this->table->getRowColumns(); // Apply defaults for new rows if (!count($this->data)) { foreach ($this->columns as $column) { $default = $this->table->getMetadata('columns', $column)['DEFAULT']; if (null !== $default) { $this->data[$column] = $default; } } } // Unset any data values not present in the columns array foreach ($this->data as $column => $value) { if (!in_array($column, $this->columns)) { unset($this->data[$column]); } if ($this->table->hasManyToManyRelationship($column)) { $this->virtualFieldsInitialized[] = $column; } elseif ($this->table->hasEav() && $this->table->getEav()->hasAttribute($column)) { $this->virtualFieldsInitialized[] = $column; } } $this->initialData = $this->data; $this->init(); }