public function getValue($record) { $value = $this->admin->getColumnValue($this->name, $record); list($column, $model) = $this->admin->getChainedModel($this->name, $record); if ($model === null) { return null; } $field = $model->getField($column, false); if ($field) { if (is_a($field, HasManyField::className()) || is_a($field, ManyToManyField::className())) { return null; } else { if (is_a($field, BooleanField::className())) { return $value ? '<i class="icon checkmark"></i>' : ''; } else { if (!empty($field->choices) && array_key_exists($value, $field->choices)) { return $field->choices[$value]; } else { return (string) $value; } } } } else { return (string) $model->{$column}; } }
public static function getFields() { return ['name' => ['class' => CharField::className(), 'default' => 'Product', 'validators' => [function ($value) { if (mb_strlen($value, 'UTF-8') < 3) { return "Minimal length < 3"; } return true; }]], 'price' => ['class' => CharField::className(), 'default' => 0], 'description' => ['class' => TextField::className(), 'null' => true], 'category' => ['class' => ForeignField::className(), 'modelClass' => Category::className(), 'null' => true, 'relatedName' => 'products'], 'lists' => ['class' => ManyToManyField::className(), 'modelClass' => ProductList::className()]]; }
public static function getFields() { return ['customer' => ['class' => ForeignField::className(), 'modelClass' => Customer::className()], 'products' => ['class' => ManyToManyField::className(), 'modelClass' => Product::className()], 'discount' => ['class' => IntField::className(), 'null' => true]]; }
public function getSafeUp() { $lines = []; $added = []; $deleted = []; $fields = $this->getFields(); $lastMigrationFields = $this->getLastMigration(); foreach ($fields as $name => $field) { if (array_key_exists($name, $lastMigrationFields) === false) { $added[$name] = $field; } } if (!empty($lastMigrationFields)) { foreach ($lastMigrationFields as $name => $field) { if (array_key_exists($name, $fields) === false) { $deleted[$name] = $field; continue; } if ($field['hash'] == $fields[$name]['hash']) { continue; } if ($field['sqlType'] != $fields[$name]['sqlType']) { $lines[] = $this->space . '$this->alterColumn("' . $this->_model->tableName() . '", "' . $name . '", "' . $fields[$name]['sqlType'] . '");'; } elseif ($field['sqlType'] == $fields[$name]['sqlType'] && $fields['length'] != $fields[$name]['length']) { $lines[] = $this->space . '$this->alterColumn("' . $this->_model->tableName() . '", "' . $name . '", "' . $fields[$name]['sqlType'] . '");'; } } foreach ($deleted as $name => $field) { $lines[] = $this->space . '$this->dropColumn("' . $this->_model->tableName() . '", "' . $name . '");'; } } if (empty($lastMigrationFields)) { $columns = []; foreach ($this->_model->getFieldsInit() as $name => $field) { if ($field->sqlType() !== false) { if (is_a($field, ForeignField::className())) { $name .= "_id"; } $columns[$name] = $field->sql(); } else { if (is_a($field, ManyToManyField::className())) { /* @var $field \Mindy\Orm\Fields\ManyToManyField */ if ($field->through === null) { $lines[] = $this->space . '$this->createTable("' . $field->getTableName() . '", ' . $this->compileColumns($field->getColumns()) . ', null, true);'; } } } } $lines[] = $this->space . '$this->createTable("' . $this->_model->tableName() . '", ' . $this->compileColumns($columns) . ');'; } else { foreach ($added as $name => $field) { $lines[] = $this->space . '$this->addColumn("' . $this->_model->tableName() . '", "' . $name . '", "' . $field['sqlType'] . '");'; } } return implode("\n", $lines); }
public static function getFields() { return ['name' => ['class' => CharField::className()], 'users' => ['class' => ManyToManyField::className(), 'modelClass' => User::className(), 'through' => Membership::className()]]; }
public static function getFields() { return ['name' => ['class' => CharField::className()], 'products' => ['class' => ManyToManyField::className(), 'modelClass' => Product::className()], 'date_action' => ['class' => DateTimeField::className(), 'required' => false, 'null' => true]]; }
public static function getFields() { return ['username' => ['class' => CharField::className(), 'validators' => [new MinLengthValidator(3), new MaxLengthValidator(20)]], 'password' => ['class' => PasswordField::className(), 'null' => true], 'groups' => ['class' => ManyToManyField::className(), 'modelClass' => Group::className(), 'through' => Membership::className()], 'addresses' => ['class' => HasManyField::className(), 'modelClass' => Customer::className(), 'relatedName' => 'user', 'editable' => false]]; }
public static function getFields() { return ['name' => ['class' => CharField::className()], 'subscribers' => ['class' => ManyToManyField::className(), 'modelClass' => self::className()], 'subscribes' => ['class' => ManyToManyField::className(), 'modelClass' => self::className(), 'reversed' => true]]; }
public function exportCsv(array $data = []) { $qs = $this->getQuerySet($this->getModel()); if (isset($data['models'])) { $qs->filter(['pk__in' => $data['models']]); } $exportData = []; $header = []; $modelsIterator = $qs->batch(100); $noExportFieldsClassName = [ForeignField::className(), HasManyField::className(), ManyToManyField::className(), OneToOneField::className()]; foreach ($modelsIterator as $models) { foreach ($models as $model) { $fields = $model->getFieldsInit(); $line = []; foreach ($fields as $attribute => $field) { if (in_array($field->className(), $noExportFieldsClassName)) { continue; } $verboseName = $field->getVerboseName($model); if (!array_key_exists($verboseName, $header)) { $header[$verboseName] = $verboseName; } $line[] = $model->{$attribute}; } $exportData[] = $line; } } $this->forceCsv($header, $exportData); }