/** * Creates the model file */ private function createModel() { $fileName = $this->configSettings['pathTo']['models'] . $this->nameOf("modelName") . ".php"; if (\File::exists($fileName)) { $this->updateModel($fileName); $this->model->exists = true; return; } $fileContents = "protected \$table = '" . $this->model->getTableName() . "';\n"; if (!$this->model->hasTimestamps()) { $fileContents .= "\tpublic \$timestamps = false;\n"; } if ($this->model->hasSoftdeletes()) { $fileContents .= "\tprotected \$softDelete = true;\n"; } $properties = ""; foreach ($this->model->getProperties() as $property => $type) { $properties .= "'{$property}',"; } $properties = rtrim($properties, ","); $fileContents .= "\tprotected \$fillable = array(" . $properties . ");\n"; $fileContents = $this->addRelationships($fileContents); $template = $this->configSettings['useRepository'] ? "model.txt" : "model-no-repo.txt"; $this->makeFileFromTemplate($fileName, $this->configSettings['pathTo']['templates'] . $template, $fileContents); $this->addModelLinksToLayoutFile(); }
/** * For relationships that have been removed, this is the reverse to add them * * @param Relation[] $removedRelationships * @param string $tableName * @return string */ private function rollbackForRemovedProperties($removedRelationships, $tableName) { $functionContents = ""; foreach ($removedRelationships as $relation) { if ($relation->getType() == "belongsToMany") { $functionContents .= "\t\tSchema::create('" . $relation->getPivotTableName($this->model) . "', function(Blueprint \$table) {\n"; $functionContents .= "\t\t\t\$table->integer('" . $relation->getForeignKeyName() . "')->unsigned();\n"; $functionContents .= "\t\t\t\$table->integer('" . $this->model->lower() . "_id')->unsigned();\n"; $functionContents .= "\t\t\t\$table->foreign('" . $relation->getForeignKeyName() . "')->references('id')->on('" . $relation->getRelatedModelTableName() . "');\n"; $functionContents .= "\t\t\t\$table->foreign('" . $this->model->lower() . "_id')->references('id')->on('" . $this->model->getTableName() . "');\n"; $functionContents .= "\t\t});\n"; } else { $functionContents .= "\t\tSchema::table('" . $tableName . "', function(Blueprint \$table) {\n"; $functionContents .= "\t\t\t\$table->integer('" . $relation->getForeignKeyName() . "')->unsigned();\n"; $functionContents .= "\t\t\t\$table->foreign('" . $relation->getForeignKeyName() . "')->references('id')->on('" . $relation->getRelatedModelTableName() . "');\n"; $functionContents .= "\t\t});\n"; } } return $functionContents; }
/** * Push an objet to a has many association * * @param mixed $obj object * * @return pushed */ public function push($obj) { if (!$obj) { return; } $cls = get_called_class(); $escape = Driver::$escape_char; $value = array_key_exists(self::getPK(), $this->_data) ? $this->_data[self::getPK()] : null; $other_cls = get_class($obj); $other_pk = $other_cls::getPK(); $other_value = $obj->get($other_pk); $table = Model::getTableName($other_cls); $foreign = self::hasManyForeignKey(Inflections::pluralize(strtolower($other_cls))); // if the current object exists ... if (!is_null($value)) { $obj->set(strtolower($foreign), $value); // if the pushed object is still not saved if (is_null($other_value)) { if (!$obj->save()) { return false; } $other_value = $obj->get($other_pk); } $foreign = self::$_mapping[$other_cls][$foreign]; $other_pk = self::$_mapping[$other_cls][$other_pk]; $sql = "update {$escape}{$table}{$escape} set {$escape}{$foreign}{$escape}={$value} where {$escape}{$other_pk}{$escape}={$other_value}"; $stmt = self::query($sql); $rst = $stmt->rowCount() == 1; self::closeCursor($stmt); return $rst; } // if current object does not exists ... if (is_null($value)) { $this->_pushLater($obj); } }
/** +---------------------------------------------------------- * 连接查询列表显示 * 进行列表过滤 +---------------------------------------------------------- * @access protected +---------------------------------------------------------- * @param Model $model 数据对象 * @param HashMap $map 过滤条件 * @param string $sortBy 排序 * @param boolean $asc 是否正序 +---------------------------------------------------------- * @return void +---------------------------------------------------------- * @throws ThinkExecption +---------------------------------------------------------- */ protected function _listLink($model, $map, $option = array(), $sortBy = '', $asc = false) { extract($option); $field || ($field = "*"); $table || ($table = $model->getTableName()); //$table = "{$this->trueTableName} j"; //$r = $this->table($table)->field($field)->join($join)->where($map)->count(); //dump($r); //return $r; //排序字段 默认为主键名 if (isset($_REQUEST['_order'])) { $order = $_REQUEST['_order']; } else { $order = !empty($sortBy) ? $sortBy : $model->getPk(); } //排序方式默认按照倒序排列 //接受 sost参数 0 表示倒序 非0都 表示正序 //$setOrder = setOrder(array(array('viewCount', 'a.view_count'), 'a.id'), $orderBy, $orderType, 'a'); if (isset($_REQUEST['_sort'])) { $sort = $_REQUEST['_sort'] ? 'asc' : 'desc'; } else { $sort = $asc ? 'asc' : 'desc'; } //取得满足条件的记录数 if ($sql) { $sqlCount = getCountSql($sql); //处理map查询条件 $count = $db->query($sqlCount); } else { $pk = $model->getPk(); $count = $model->table($table)->field($field)->join($join)->where($map)->count($pk); } if ($count > 0) { import("ORG.Util.XPage"); //创建分页对象 if (!empty($_REQUEST['listRows'])) { $listRows = $_REQUEST['listRows']; } else { $listRows = ''; } $p = new XPage($count, $listRows); //echo C('PAGE_STYLE');exit; //$s = rand(1,25);echo $s; $p->style = C('PAGE_STYLE'); //设置风格 //分页查询数据 if ($sql) { //处理map查询条件 $voList = $model->query(sql . "`" . $order . "` " . $sort . $p->firstRow . ',' . $p->listRows); } else { $voList = $model->table($table)->field($field)->join($join)->where($map)->order("`" . $order . "` " . $sort)->limit($p->firstRow . ',' . $p->listRows)->select(); } //echo $model->getlastsql(); //高亮关键字 if (C('highLightKeyword') && $_REQUEST['keyword']) { $keyword = $_REQUEST['keyword']; foreach ($voList as $k => $v) { $voList[$k]['jtitle'] = hightLightKeyword($v['jtitle'], $keyword); $voList[$k]['request'] = hightLightKeyword($v['request'], $keyword); $voList[$k]['ctitle'] = hightLightKeyword($v['ctitle'], $keyword); } } //分页跳转的时候保证查询条件 foreach ($map as $key => $val) { if (!is_array($val)) { $p->parameter .= "{$key}=" . urlencode($val) . "&"; } } //分页显示 $page = $p->show(); //列表排序显示 $sortImg = $sort; //排序图标 $sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示 $sort = $sort == 'desc' ? 1 : 0; //排序方式 //模板赋值显示 $this->assign('list', $voList); $this->assign('sort', $sort); $this->assign('order', $order); $this->assign('sortImg', $sortImg); $this->assign('sortType', $sortAlt); $this->assign("page", $page); } Cookie::set('_currentUrl_', __SELF__); return; }
/** * Prepares this model for any other existing Model. * * The original does not have to use the FluentBuilderTrait. * * @param Model $model * * @return DynamicFluentModel */ public function withModel(Model $model) { return $this->withTable($model->getTableName(), $model->getPrimaryKeyName(), $model->getColumns()); }
private function getMinPriceByHotGoods() { $model = new Model('goods as g'); $table_goods = $model->getTableName("goods"); $table_prom_goods = $model->getTableName("prom_goods"); $tableNameStr = $table_goods . " AS g," . $table_prom_goods . " AS p"; $queryFeilds = "MIN(g.sell_price) AS minprice ,MIN(g.prom_id) AS promid ,p.end_time AS endtime"; $queryWhere = "is_online = 0 AND store_nums > 0 AND g.prom_id = p.id AND p.start_time <= NOW() AND p.end_time >= NOW()"; $groupFeilds = "prom_id"; $this->assign('minprice', $model->customQuery($tableNameStr, $queryFeilds, $queryWhere, $groupFeilds, null, null, null)); }