/** * Augment the provided Select object with a comma-separated list of values for this * many-to-many relationship, using the name parameter as the name of the value in * the resultset. * * @param Select $select * @param string $name * @return Select */ public function augmentSelect(Select $select, $name) { $anchorColumn = $select->quoteWithAlias($this->sourceTable->getTableName(), $this->getSourceColumnName()); $titleColumn = $this->findReferenceTitleColumn(); $expr = new Expr("ARRAY_TO_STRING(\n ARRAY(\n SELECT {$titleColumn}\n FROM {$this->getReferenceTableName()} ref\n JOIN {$this->xrefTableName} xref\n ON xref.{$this->xrefReferenceColumnName} = ref.{$this->getReferenceColumnName()}\n WHERE xref.{$this->xrefAnchorColumnName} = {$anchorColumn}\n ORDER BY {$titleColumn}\n ),\n ', '\n )"); return $select->columns([$name => $expr]); }
/** * Augment the provided Select object with all the EAV attribute values from this * definition. * * @param Select $select * @return Select * @throws Select */ public function augmentSelect(Select $select) { $db = $this->table->getAdapter(); $id = current($this->table->getPrimaryKey()); $rootTableAlias = $select->quoteWithAlias($this->table->getTableName(), $id); foreach ($this->getAttributes() as $attribute) { $alias = 'eav_' . $attribute['attribute_id']; $table = $this->table->getTableName() . $this->valueTablePrefix . $attribute['backend_type']; $select->joinLeft([$alias => $table], $db->quoteInto("{$alias}.{$id} = {$rootTableAlias} AND {$alias}.attribute_id = ?", $attribute['attribute_id']), [$alias => 'value']); } return $select; }
/** * Augment the provided Select object with a comma-separated list of values for this * many-to-many relationship, using the name parameter as the name of the value in * the resultset. * * @param Select $select * @param string $name * @return Select */ public function augmentSelect(Select $select, $name) { $anchorColumn = $select->quoteWithAlias($this->sourceTable->getTableName(), $this->getSourceColumnName()); $titleColumn = $this->findReferenceTitleColumn(); $driver = $select->getAdapter()->getDriver(); if ($driver instanceof \Dewdrop\Db\Driver\Pdo\Pgsql) { $expr = new Expr("ARRAY_TO_STRING(\n ARRAY(\n SELECT {$titleColumn}\n FROM {$this->getReferenceTableName()} ref\n JOIN {$this->xrefTableName} xref\n ON xref.{$this->xrefReferenceColumnName} = ref.{$this->getReferenceColumnName()}\n WHERE xref.{$this->xrefAnchorColumnName} = {$anchorColumn}\n ORDER BY {$titleColumn}\n ),\n ', '\n )"); } else { $expr = new Expr("(SELECT\n GROUP_CONCAT({$titleColumn} SEPARATOR ', ')\n FROM {$this->getReferenceTableName()} ref\n JOIN {$this->xrefTableName} xref\n ON xref.{$this->xrefReferenceColumnName} = ref.{$this->getReferenceColumnName()}\n WHERE xref.{$this->xrefAnchorColumnName} = {$anchorColumn}\n ORDER BY {$titleColumn}\n )"); } return $select->columns([$name => $expr]); }
public function __construct(Table $table, ActivityLog $activityLog = null, Inflector $inflector = null) { $this->table = $table; $this->inflector = $inflector ?: Pimple::getResource('inflector'); $tableName = $table->getTableName(); $inflectedName = $this->inflector->singularize($this->inflector->hyphenize($tableName)); if (!$tableName) { $className = get_class($table); throw new Exception("Cannot create activity log handle for {$className} because no table name is set."); } $this->setActivityLog($activityLog ?: Pimple::getResource('activity-log'))->setName($inflectedName)->setModel($table)->addAlias($tableName); parent::__construct(); }
/** * 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; }
/** * Get the group name for this field, typically the table name, unless an * alternative has been set. * * @return string */ public function getGroupName() { return $this->groupName ?: $this->table->getTableName(); }
/** * A shortcut for linkByQueryString() that uses Table object get the model * and query string param name. * * @param Table $table * @return RowEditor * @throws Exception */ public function linkTableByQueryString(Table $table) { $primaryKey = $table->getPrimaryKey(); if (1 !== count($primaryKey)) { throw new Exception('Can only use linkTableByQueryString() when a single primary key column is present.'); } return $this->linkByQueryString($table->getTableName(), current($primaryKey)); }