/** * Joins the related table together for the pending query * * @return $this * @since 2.0.0 **/ public function join() { // We do a left outer join here because we're not trying to limit the primary table's results // This function is primarily used when needing to sort by a field in the joined table $this->model->select($this->model->getQualifiedFieldName('*'))->join($this->related->getTableName(), $this->model->getQualifiedFieldName($this->localKey), $this->related->getQualifiedFieldName($this->relatedKey), 'LEFT OUTER'); return $this; }
/** * ฟังก์ชั่น คิดตั้ง โมดูลและ เมนู * ถ้ามีโมดูลติดตั้งแล้ว คืนค่า ID ของโมดูล * * @param string $owner โฟลเดอร์ของโมดูล * @param string $module ชื่อโมดูล * @param string $title (optional) ข้อความไตเติลบาร์ของโมดูล * @param string $menupos (optional) ตำแหน่งของเมนู (MAINMENU,SIDEMENU,BOTTOMMENU) * @param string $menu (optional) ข้อความเมนู * @return int คืนค่า ID ของโมดูลที่ติดตั้ง, -1 ติดตั้งแล้ว, 0 มีข้อผิดพลาด */ public static function installing($owner, $module, $title, $menupos = '', $menu = '') { if (preg_match('/^[a-z]+$/', $owner) && preg_match('/^[a-z]+$/', $module)) { // model $model = new static(); $db = $model->db(); // ตรวจสอบโมดูลที่ติดตั้งแล้ว $search = $db->createQuery()->from('modules')->where(array('module', $module))->first('id'); if (!$search) { $id = $db->insert($model->getTableName('modules'), array('owner' => $owner, 'module' => $module)); $index = $db->insert($model->getTableName('index'), array('module_id' => $id, 'index' => 1, 'published' => 1)); $db->insert($model->getTableName('index_detail'), array('module_id' => $id, 'id' => $index, 'topic' => $title)); if ($menupos != '' && $menu != '') { $db->insert($model->getTableName('menus'), array('index_id' => $index, 'parent' => $menupos, 'level' => 0, 'menu_text' => $menu, 'menu_tooltip' => $title)); } return $id; } else { return -1; } } return 0; }
/** * Gets the asset id for the object instance * * @return int * @since 2.0.0 **/ public function getId() { // Check for current asset id and compute other vars $current = $this->model->get('asset_id', null); $parentId = $this->getAssetParentId(); $name = $this->getAssetName(); $title = $this->getAssetTitle(); // Get joomla jtable model for assets $asset = \JTable::getInstance('Asset', 'JTable', array('dbo' => \App::get('db'))); $asset->loadByName($name); // Re-inject the asset id into the model $this->model->set('asset_id', $asset->id); if ($asset->getError()) { return false; } // Specify how a new or moved node asset is inserted into the tree if (!$this->model->get('asset_id', null) || $asset->parent_id != $parentId) { $asset->setLocation($parentId, 'last-child'); } // Prepare the asset to be stored $asset->parent_id = $parentId; $asset->name = $name; $asset->title = $title; if ($this->model->assetRules instanceof \JAccessRules) { $asset->rules = (string) $this->model->assetRules; } if (!$asset->check() || !$asset->store()) { return false; } // Register an event to update the asset name once we know the model id if ($this->model->isNew()) { $me = $this; Event::listen(function ($event) use($asset, $me) { $asset->name = $me->getAssetName(); $asset->store(); }, $this->model->getTableName() . '_new'); } // Return the id return (int) $asset->id; }
/** * @param Query|array|bool $args * * @return int * * @throws Exception */ public static function count($args = false) { /* @var ActiveRecord $model */ $query = null; $model = new static(); if ($args && $args instanceof Query) { $query = $args; //$query->from = " (SELECT T.".$model->getPkName()." FROM ".$model->getTableName()." T) TEMP"; if (empty($query->from)) { $query->addFrom($model->getTableName() . ' T'); } $query->select = 'T.' . $model->getPkName(); } elseif ($args && is_array($args)) { if (!isset($args['from'])) { $args['from'] = $model->getTableName() . ' T'; } $query = new Query($args); //$query->select = 'COUNT(*)'; $query->select = 'T.' . $model->getPkName(); } elseif (!$args) { $query = new Query(['from' => $model->getTableName() . ' T', 'select' => 'T.' . $model->getPkName()]); } else { throw new Exception(translate('Bad arguments'), 500, Exception::BADARGUMENTS); } $count_query = new Query(['select' => 'COUNT(*)', 'from' => '(' . $query->toSql() . ') TC']); $stmt = $model->getDB()->prepare($count_query->toSql()); $stmt->execute($query->params); $records = $stmt->fetchColumn(); $stmt->closeCursor(); return $records; }
/** * Get All rows without filter * * @param string|array $where where statement * @return array */ public static final function findAll($where = '1') { $model = new static(); $results = $model->getDbConnection()->fetchAll("SELECT * FROM {$model->getTableName()} WHERE " . $model->sqlWhere($where)); $ret = []; $class = get_called_class(); foreach ($results as $props) { $ret[] = new $class($props); } return $ret; }