/** * Constructor * Use $db->createResult( $parent, $name ) instead * * @param Database|Result|Row $parent * @param string $name */ function __construct($parent, $name) { if ($parent instanceof Database) { // basic result $this->db = $parent; $this->table = $this->db->getAlias($name); } else { // Row or Result // result referenced to parent $this->parent_ = $parent; $this->db = $parent->getDatabase(); // determine type of reference based on conventions and user hints $fullName = $name; $name = preg_replace('/List$/', '', $fullName); $this->table = $this->db->getAlias($name); $this->single = $name === $fullName; if ($this->single) { $this->key = $this->db->getPrimary($this->getTable()); $this->parentKey = $this->db->getReference($parent->getTable(), $name); } else { $this->key = $this->db->getBackReference($parent->getTable(), $name); $this->parentKey = $this->db->getPrimary($parent->getTable()); } } }
/** * Create a back reference * * @param $key * * @return $this * * @since 1.0.0 */ public function via($key) { if ($this->parent instanceof DatabaseTable) { $this->db->schema()->setReference($this->parent->getTable(), $this->table, $key); } return $this; }
public function pageSetupMasterAccount() { // Get the alert message stream $ms = $this->_app->alerts; // Do not allow registering a master account if one has already been created if (UserLoader::exists($this->_app->config('user_id_master'))) { $ms->addMessageTranslated("danger", "MASTER_ACCOUNT_EXISTS"); $this->_app->redirect($this->_app->urlFor('uri_home')); } $schema = new \Fortress\RequestSchema($this->_app->config('schema.path') . "/forms/register.json"); $validators = new \Fortress\ClientSideValidator($schema, $this->_app->translator); $this->_app->render('common/install/install-master.html', ['page' => ['author' => $this->_app->site->author, 'title' => "Installation | Register Master Account", 'description' => "Set up the master account for your installation of UserFrosting", 'alerts' => $this->_app->alerts->getAndClearMessages()], 'validators' => $validators->formValidationRulesJson(), 'table_config' => Database::getTable('configuration')->name]); }
public function items() { if (!is_array($this->items)) { $query = $this->buildQuery(); $rows = Database::getTable($query); $items = array(); $firstAlias = substr($this->graph['model'], 0, -5); foreach ($rows as $row) { $this->parseRow($row, $items, $firstAlias, $this->graph); } $this->items = isset($items[$firstAlias]) ? array_values($items[$firstAlias]) : array(); } return $this->items; }
public function getProducts() { if ($this->id && empty($this->products)) { $rows = Database::getTable("\n SELECT `id_product`, `quantity`\n FROM `order_items`\n WHERE `id_order` = {$this->id}\n "); $products = array(); foreach ($rows as $row) { $product = new ProductModel($row['id_product']); $product->cart_quantity = $row['quantity']; $product->total_price = $product->price * $product->cart_quantity; $products[] = $product; } $this->products = $products; } return $this->products; }
private static function assignForm($product, $errors) { $categories = Database::getTable("\n SELECT\n `c`.`id` AS `value`,\n `cl`.`name` AS `text`\n FROM `category` AS `c`\n LEFT JOIN `category_lang` AS `cl` ON `cl`.`id_object` = `c`.`id`\n WHERE `c`.`active` = 1 AND `cl`.`id_lang` = 1\n ORDER BY `cl`.`name`\n "); $brands = Database::getTable("\n SELECT\n `id` AS `value`,\n `name` AS `text`\n FROM `brand`\n WHERE `active` = 1\n ORDER BY `name`\n "); $form = new TemplateForm(); $form->addTextField('name', 'Название', $product->name, isset($errors['name']) ? $errors['name'] : null, true); $form->addTextField('articul', 'Артикул', $product->articul, isset($errors['articul']) ? $errors['articul'] : null); $form->addTextField('price', 'Цена', $product->price, isset($errors['price']) ? $errors['price'] : null); $form->addSelectField('id_category', 'Категория', $categories, $product->id_category, isset($errors['id_category']) ? $errors['id_category'] : null, 'Выберите категорию'); $form->addSelectField('id_brand', 'Производитель', $brands, $product->id_brand, isset($errors['id_brand']) ? $errors['id_brand'] : null, 'Выберите произвотеля'); $form->addTextareaField('description', 'Описание', $product->description, null, true, true); $form->addCheckboxField('active', 'Вкл/Выкл', $product->active); if ($product->getId()) { $list = new TemplateList(); $list->fields = array('file' => array('type' => 'image', 'title' => '', 'directory' => 'products', 'size' => '85x84')); $list->itemActions = array('down-image' => array('hint' => 'Опустить', 'icon' => 'down', 'controller' => 'AdminProducts'), 'up-image' => array('hint' => 'Поднять', 'icon' => 'up', 'controller' => 'AdminProducts'), 'delete-image' => array('hint' => 'Удалить', 'icon' => 'delete', 'controller' => 'AdminProducts')); $list->items = $product->images; $form->addUploaderField('image', 'Добавить картинку', App::getLink('AdminProducts', array('action' => 'new-image')), '*.jpg;*.jpeg;*.png', 'Изображения', '3MB', array('product' => $product->getId()), $list); } else { $form->addUploaderField('image', 'Добавить картинку', App::getLink('AdminProducts', array('action' => 'new-image')), '*.jpg;*.jpeg;*.png', 'Изображения', '3MB'); } $form->classes = 'center-form'; self::templateVar('content', $form); }
require_once __DIR__ . '/app/bootstrap.php'; if (!isset($_GET['action'])) { die("No action set."); } if (!isset($_GET['db'])) { die("No db set."); } $action = $_GET['action']; $active_db = $_GET['db']; $active_table = false; if ($action == 'show_menu') { $db = new Database($active_db); $tables = $db->getTables(); if ($active_table == false) { if (isset($tables[0])) { $active_table = $tables[0]; } } $curr_table = $db->getTable($active_table); // Build default menu for active_db. $lm = new ListMenu(); $lm->setItems($tables); $lm->setColumnMax(3); TemplateVars::set('tables', $lm->toString($active_table)); if (count($tables) == 0) { die("<em>This database is empty.</em>"); } require_once './app/templates/partials/menu_display.phtml'; exit; }
/** * @param string $table_name * @return array */ function getForeignKeysToTable($table_name) { return $this->database->getTable($table_name)->getReferrers(); }
private function loadLink($link_name) { if (isset(static::$links[$link_name]) && $this->id) { $link = static::$links[$link_name]; switch ($link['type']) { // Когда модель связана со списком объектов другой модели case LinkType::PRIMARY_KEY: $collection = new Collection($link['model'], $this->id_lang); if (isset($link['order'])) { $collection->order($link['order']); } $where = "`{$link['field']}` = {$this->id}"; if (isset($link['where'])) { $where .= " AND {$link['where']}"; } $collection->where($where); if (isset($link['limit'])) { $collection->limit($link['limit']); } $models = $collection->items(); if (isset($link['limit']) && $link['limit'] == 1) { if (!empty($models)) { $this->models[$link_name] = $models[0]; } else { $this->models[$link_name] = null; } } else { $this->models[$link_name] = $models; } break; // Когда модель связана с одним объектом другой модели // Когда модель связана с одним объектом другой модели case LinkType::FOREIGN_KEY: $collection = new Collection($link['model'], $this->id_lang); $where = "`id` = {$this->{$link['field']}}"; if (!empty($link['where'])) { $where .= " AND {$link['where']}"; } $collection->where($where); $models = $collection->items(); if (!empty($models)) { $this->models[$link_name] = $models[0]; } else { $this->models[$link_name] = null; } break; // Связь многие ко многим через промежуточную таблицу // Связь многие ко многим через промежуточную таблицу case LinkType::TABLE: $ids = Database::getTable("\n SELECT `{$link['field2']}`\n FROM `{$link['table']}`\n WHERE `{$link['field1']}` = {$this->id}\n "); foreach ($ids as $index => $row) { $ids[$index] = $row[$link['field2']]; } $collection = new Collection($link['model'], $this->id_lang); if ($link['order']) { $collection->order($link['order']); } $where = "`id` IN (" . implode(', ', $ids) . ")"; if ($link['where']) { $where .= " AND {$link['where']}"; } $collection->where($where); if ($link['limit']) { $collection->limit($link['limit']); } $models = $collection->items(); if ($link['limit'] == 1) { if (!empty($models)) { $this->models[$link_name] = $models[0]; } else { $this->models[$link_name] = null; } } else { $this->models[$link_name] = $models; } break; } } }