#22 | Category::findFirst(id=27 AND is_visible = 1 AND is_delete = 0)
/data/app/controllers/IndexController.php (26) <?php
use Phalcon\Http\Response;
class IndexController extends ControllerBase
{
public function initialize()
{
}
public function indexAction()
{
$result = Setting::findFirst("keyword='index_topic_id'");
$this->dispatcher->forward(array(
'controller' => 'index',
'action' => 'show',
'params' => ['topic',$result->value]
));
return false;
}
public function listAction($category_id)
{
//分类信æ¯
$model = new Category();
$categoryResult = $model->findFirst("id=$category_id AND is_visible = 1 AND is_delete = 0");
if(!$categoryResult)
$this->route404();
$table = $categoryResult->module;
if(!in_array($table, ["category","article","album","link","topic"]))
$this->route404();
$this->view->setVar("category",$categoryResult);
//全局设置
$this->getGlobal();
//兄弟分类信æ¯ï¼Œé¢åŒ…屑信æ¯
if($categoryResult->father_id != 0){
$father_id = $categoryResult->father_id;
$brotherResult = $model->find([
"father_id=$father_id AND is_visible = 1 AND is_delete = 0",
"order"=>"weight DESC"
]);
$this->view->setVar("brother",$brotherResult);
$father_path = explode(",", $categoryResult->father_path);
$parentResult = $model->find(" ( id = ".implode(" OR id=", $father_path)." ) AND is_visible = 1 AND is_delete = 0");
$this->view->setVar("parent",$parentResult);
}
//实体列表信æ¯
$Table = ucfirst($table);
if($table == "category"){
$model = new Category();
$result = $model->find([
"father_id = $category_id AND is_visible = 1 AND is_delete = 0",
"order"=>"weight DESC, created_at DESC"
]);
}else{
$model = new $Table();
$result = $model->find([
"category_id = $category_id AND is_visible = 1 AND is_delete = 0",
"order"=>"weight DESC, created_at DESC",
"limit"=>15
]);
//çƒé—¨
$hotResult = $model->find([
"category_id = $category_id AND is_visible = 1 AND is_delete = 0",
"order"=>"view_num DESC, created_at DESC",
"limit"=>15
]);
$this->view->setVar("hotResult", $hotResult);
}
//返回
$this->view->setVar("result", $result);
$this->view->pick($table."/index");
}
public function showAction($object,$object_id)
{
if(!in_array($object, ["category","article","album","link","topic","picture"]))
$this->route404();
//全局设置
$this->getGlobal();
//内容信æ¯
$Table = ucfirst($object);
$model = new $Table();
$result = $model->findFirst("id=$object_id AND is_visible = 1 AND is_delete = 0");
if(!$result)
$this->route404();
//访问é‡++
$result->view_num++;
$result->save();
$category_id = $result->category_id;
//çƒé—¨
$hotResult = $model->find([
"category_id = $category_id AND is_visible = 1 AND is_delete = 0",
"order"=>"view_num DESC, created_at DESC"
]);
$this->view->setVar("hotResult", $hotResult);
//å…¶ä»–
if($object == "album"){
$pictures = Picture::find("album_id=$object_id AND is_visible = 1 AND is_delete = 0");
$this->view->setVar("pictures", $pictures);
}
if($object == "topic"){
$topicCategoryResult = TopicCategory::find(["topic_id = $object_id","order"=>"weight DESC, id ASC"]);
//临时首页模版
$SettingResult = Setting::findFirst("keyword='index_topic_id'");
if($object_id==$SettingResult->value){
$template = "index/index";
$indexHotResult = Article::find([
"is_hot = 1 AND is_visible = 1 AND is_delete = 0",
"order"=>"weight DESC, created_at DESC",
"limit"=>6
]);
$this->view->setVar("index_hot", $indexHotResult);
}
$categories = [];
foreach ($topicCategoryResult as $value) {
$topicTable = ucfirst($value->category_module);
$topicModel = new $topicTable();
$topicCategory_id = $value->category_id;
$columnsOthers = ($topicTable == "Link")?",url":"";
$categories[] =(object)[
"basic" => Category::findFirst("id = $topicCategory_id"),
"list" => $topicModel->find([
"category_id=$topicCategory_id AND is_visible = 1 AND is_delete = 0",
"order"=>"weight DESC, created_at DESC",
"columns"=>"id,title,description,img_dir,created_at".$columnsOthers,
"limit"=>8
])
];
}
$this->view->setVar("categories",$categories);
}
//分类信æ¯
$model = new Category();
$categoryResult = $model->findFirst("id=$category_id AND is_visible = 1 AND is_delete = 0");
if(!$categoryResult)
$this->route404();
$this->view->setVar("category",$categoryResult);
//兄弟分类信æ¯ï¼Œé¢åŒ…屑信æ¯
if($categoryResult->father_id != 0){
$father_id = $categoryResult->father_id;
$brotherResult = $model->find([
"father_id=$father_id AND is_visible = 1 AND is_delete = 0",
"order"=>"weight DESC"
]);
$this->view->setVar("brother",$brotherResult);
$father_path = explode(",", $categoryResult->father_path);
$parentResult = $model->find(" ( id = ".implode(" OR id=", $father_path)." ) AND is_visible = 1 AND is_delete = 0");
$this->view->setVar("parent",$parentResult);
}
//返回
$this->view->setVar("result", $result);
$this->view->pick(isset($template)?$template:$object."/show");
}
public function aliasAction(){
$model = new Alias();
$name = $this->dispatcher->getParam("alias");
$result = $model->findFirst("name='$name'");
if($result){
if($result->object == "category"){
$action = "list";
$params = [$result->object_id];
}else{
$action = "show";
$params = [$result->object,$result->object_id];
}
$this->dispatcher->forward(array(
'controller' => 'index',
'action' => $action,
'params' => $params
));
}else{
echo "not found!";
}
return false;
}
public function searchAction($content=""){
//全局设置
$this->getGlobal();
$content = ($content!="")?$content:$_GET['content'];
$articleResult = Article::find([
"(title LIKE ?1 OR description LIKE ?1 OR content LIKE ?1) AND is_visible = 1 AND is_delete = 0",
"bind" =>[1=>"%$content%"],
"order"=>"weight DESC, created_at DESC",
"limit"=>15
]);
$linkResult = LINK::find([
"(title LIKE ?1 OR description LIKE ?1) AND is_visible = 1 AND is_delete = 0",
"bind" =>[1=>"%$content%"],
"order"=>"weight DESC, created_at DESC",
"limit"=>15
]);
$albumResult = Album::find([
"(title LIKE ?1 OR description LIKE ?1) AND is_visible = 1 AND is_delete = 0",
"bind" =>[1=>"%$content%"],
"order"=>"weight DESC, created_at DESC",
"limit"=>15
]);
$topicResult = Topic::find([
"(title LIKE ?1 OR description LIKE ?1 OR content LIKE ?1) AND is_visible = 1 AND is_delete = 0",
"order"=>"weight DESC, created_at DESC",
"bind" =>[1=>"%$content%"],
"limit"=>15
]);
$this->view->setVar("content",$content);
$this->view->setVar("articles",$articleResult);
$this->view->setVar("links",$linkResult);
$this->view->setVar("albums",$albumResult);
$this->view->setVar("topics",$topicResult);
}
public function moreAction($object){
if(!in_array($object, ["category","article","album","link","topic","picture"]))
$this->jsonResponse(true,[],"object is out");
if(!$this->request->has("page"))
$this->jsonResponse(true,[],"page is missing");
$whereStr = "is_visible = 1 AND is_delete = 0";
$whereBind = [];
//关键è¯è¿‡æ»¤
if($this->request->has("content")){
$content = $this->request->get("content");
if(in_array($object, ["article","topic"])){
$whereStr .= " AND (title LIKE ?1 OR content LIKE ?1)";
}else{
$whereStr .= " AND title LIKE ?1";
}
$whereBind[1] = "%$content%";
}
//分类过滤
if($this->request->has("category_id")){
$category_id = $this->request->get("category_id","int");
$whereStr .= " AND category_id = '$category_id'";
}
//分页
$page = $this->request->get("page","int");
//查找
$Table = ucfirst($object);
$model = new $Table();
$result = $model::find([
$whereStr,
"order"=>"weight DESC, created_at DESC",
"bind" =>$whereBind,
"limit"=>["number" => 15, "offset" => 15*($page-1)]
])->toArray();
$this->jsonResponse(false,$result);
}
public function tagAction()
{
}
public function initAction()
{
$dataAll = [
'Menu'=>[
["title"=>"棋院新闻","object"=>"category", "object_id"=>4,"father_id"=>0],
["title"=>"棋手风采","object"=>"category", "object_id"=>5,"father_id"=>0],
["title"=>"师资介ç»","object"=>"category", "object_id"=>6,"father_id"=>0],
["title"=>"招生信æ¯","object"=>"category", "object_id"=>7,"father_id"=>0],
["title"=>"æˆç»©å…¬å¸ƒ","object"=>"category", "object_id"=>8,"father_id"=>0],
["title"=>"竞赛规程","object"=>"category", "object_id"=>9,"father_id"=>0],
["title"=>"资料下载","object"=>"category", "object_id"=>10,"father_id"=>0],
["title"=>"国际象棋","object"=>"category", "object_id"=>16,"father_id"=>1],
["title"=>"象棋","object"=>"category", "object_id"=>17,"father_id"=>1],
["title"=>"围棋","object"=>"category", "object_id"=>18,"father_id"=>1],
["title"=>"国际象棋","object"=>"category", "object_id"=>19,"father_id"=>2],
["title"=>"象棋","object"=>"category", "object_id"=>20,"father_id"=>2],
["title"=>"围棋","object"=>"category", "object_id"=>21,"father_id"=>2],
["title"=>"国际象棋","object"=>"category", "object_id"=>22,"father_id"=>3],
["title"=>"象棋","object"=>"category", "object_id"=>23,"father_id"=>3],
["title"=>"围棋","object"=>"category", "object_id"=>24,"father_id"=>3],
["title"=>"国际象棋","object"=>"category", "object_id"=>25,"father_id"=>4],
["title"=>"象棋","object"=>"category", "object_id"=>26,"father_id"=>4],
["title"=>"围棋","object"=>"category", "object_id"=>27,"father_id"=>4],
["title"=>"国际象棋","object"=>"category", "object_id"=>28,"father_id"=>5],
["title"=>"象棋","object"=>"category", "object_id"=>29,"father_id"=>5],
["title"=>"围棋","object"=>"category", "object_id"=>30,"father_id"=>5],
["title"=>"国际象棋","object"=>"category", "object_id"=>31,"father_id"=>6],
["title"=>"象棋","object"=>"category", "object_id"=>32,"father_id"=>6],
["title"=>"围棋","object"=>"category", "object_id"=>33,"father_id"=>6]
],
];
foreach ($dataAll as $table => $datas) {
$model = new $table();
if($table == "TopicCategory") $table_str = "topic_category";
else $table_str = $table;
//$model->getReadConnection()->query("TRUNCATE $table_str;");
foreach ($datas as $data) {
$model = new $table();
$model->save($data);
}
}
exit;
$dataAll = [
'User'=>[
["name"=>"admin","password"=>$this->security->hash("123123"),"role_id"=>1],
],
'Category'=>[
["name"=>"代ç ", "module"=>"article","father_id"=>"0","father_path"=>"0"],
["name"=>"综åˆèµ„讯", "module"=>"article","father_id"=>"0","father_path"=>"0"],
["name"=>"最新分享代ç ","module"=>"article","father_id"=>"1","father_path"=>"0,1"],
["name"=>"本周çƒé—¨ä»£ç ","module"=>"article","father_id"=>"1","father_path"=>"0,1"],
["name"=>"链接分类1", "module"=>"link","father_id"=>"0","father_path"=>"0"],
["name"=>"相册分类1", "module"=>"album","father_id"=>"0","father_path"=>"0"],
["name"=>"专题分类1", "module"=>"topic","father_id"=>"0","father_path"=>"0"],
["name"=>"代ç 3","module"=>"article","father_id"=>"3","father_path"=>"0,1,3"],
["name"=>"代ç 4","module"=>"article","father_id"=>"3","father_path"=>"0,1,3"],
],
'Article'=>[
[
"title"=>"JFinal 2.0 å‘布,JAVA æžé€Ÿ WEB+ORM 框架",
"description"=>"JFinal 是本星çƒç¬¬ä¸€ä¸ªæå‡ºæžé€Ÿå¼€å‘ç†å¿µï¼Œä¹Ÿæ˜¯å”¯ä¸€ä¸ªæžé€Ÿå¼€å‘框架。自开æºä»¥æ¥è¿…速获得广大开å‘者的喜爱...",
"img_dir"=>"http://www.oschina.net/img/logo/jfinal.gif?t=1399607809000",
"content"=>"JFinal 是本星çƒç¬¬ä¸€ä¸ªæå‡ºæžé€Ÿå¼€å‘ç†å¿µï¼Œä¹Ÿæ˜¯å”¯ä¸€ä¸ªæžé€Ÿå¼€å‘框架。自开æºä»¥æ¥è¿…速获得广大开å‘者的喜爱,æžé€Ÿå¼€å‘çš„ä¼˜åŠ¿é€æ¥æ·±å…¥äººå¿ƒã€‚由于æžé€Ÿå¼€å‘å¨åŠ›å·¨å¤§ï¼Œæ‰€ä»¥æœ‰äº†ä»¥ä¸‹åœ¨ OSChina 的惊人数æ®ï¼š
1ï¼šé—®ç”æ•°2600个,在OSChina 在收录的37153ä¸ªé¡¹ç›®ä¸æ€»æŽ’å第五ä½ï¼ŒæŽ’å‰å››ä½çš„项目分别是:javaã€androidã€phpã€mysql,å‰å››ä¸ªé¡¹ç›®åœ¨ OSChina 的收录时间比 JFinal è¦æ—©ä¸€åˆ°å››å¹´ï¼Œé—®ç”æ•°é‡å……分表明JFinal是OSChina最活跃的项目
相关链接:http://www.oschina.net/question/tags?catalog=1",
"category_id"=>1,
"created_by"=>1
],
[
"title"=>"Linux Kernel 4.1 å‘布",
"description"=>"",
"content"=>"",
"img_dir"=>"",
"category_id"=>1,
"created_by"=>1
],
[
"title"=>"FreeMarker 2.3.23 RC1 å‘布,Java 模æ¿å¼•擎",
"description"=>"",
"content"=>"",
"img_dir"=>"",
"category_id"=>1,
"created_by"=>1
],
[
"title"=>"ã€æ¯æ—¥ä¸€åšã€‘深入ç†è§£å¦ä¹ Git 常用工作æµ",
"description"=>"",
"content"=>"",
"img_dir"=>"",
"category_id"=>8,
"created_by"=>1
],
[
"title"=>"è®©ä½ çš„ PHP 7 æ›´å¿« (GCC PGO)",
"description"=>"",
"content"=>"",
"img_dir"=>"",
"category_id"=>8,
"created_by"=>1
],
[
"title"=>"解决远程连接mysql很慢的方法(mysql_connect 打开连接慢)",
"description"=>"有次åŒäº‹æå‡ºå¼€å‘使用的mysqlæ•°æ®åº“è¿žæŽ¥å¾ˆæ…¢ï¼Œå› ä¸ºæˆ‘ä»¬çš„mysql开呿•°æ®åº“是å•ç‹¬ä¸€å°æœºå™¨éƒ¨ç½²çš„,所以认为å¯èƒ½æ˜¯ç½‘络连接问题导致的。",
"img_dir"=>"http://chinalove99.net/mall/uploads/2015/01/30/20150130084155EGCQc51e9d44.jpg",
"content"=>"在进行 pingå’ŒrouteåŽå‘现网络通信都是æ£å¸¸çš„,而且在mysql机器上进行本地连接å‘现是很快的,所以网络问题基本上被排除了。以å‰ä¹Ÿé‡åˆ°è¿‡ä¸€æ¬¡è¿™æ ·çš„问题,å¯åŽæ¥å°±ä¸çŸ¥æ€Žä¹ˆçªç„¶å¥½äº†ï¼Œè¿™æ¬¡åˆéé‡è¿™æ ·çš„é—®é¢˜ï¼Œæ‰€ä»¥æƒ³çœ‹çœ‹æ˜¯ä¸æ˜¯mysqlçš„é…置问题。在查询mysql相关文档和网络æœç´¢åŽï¼Œå‘现了一个é…置似乎å¯ä»¥è§£å†³è¿™æ ·çš„问题,就是在mysqlçš„é…置文件ä¸å¢žåР如䏋é…ç½®å‚æ•°ï¼š",
"category_id"=>3,
"created_by"=>1
]
],
'Link'=>[
[
"title"=>"è°·æŒ",
"url"=>"https://google.com",
"category_id"=>5,
"created_by"=>1
],
[
"title"=>"百度",
"url"=>"http://baidu.com",
"category_id"=>5,
"created_by"=>1
]
],
'Album'=>[
[
"title"=>"越往越å—的列车",
"category_id"=>6,
"img_dir"=>"http://img3.douban.com/view/note/large/public/p27433935.jpg",
"created_by"=>1
]
],
'Picture'=>[
[
"title"=>"列车1",
"album_id"=>1,
"dir"=>"http://img3.douban.com/view/note/large/public/p27433935.jpg",
"created_by"=>1
],
[
"title"=>"列车2",
"album_id"=>1,
"dir"=>"http://img4.douban.com/view/note/large/public/p27433936.jpg",
"created_by"=>1
]
],
'Topic'=>[
[
"title"=>"首页",
"category_id"=>7,
"created_by"=>1
],
[
"title"=>"燕山大å¦2015毕业专题",
"category_id"=>7,
"created_by"=>1
]
],
'TopicCategory'=>[
[
"topic_id"=>1,
"category_module"=>'article',
"category_id"=>1
],
[
"topic_id"=>1,
"category_module"=>'link',
"category_id"=>5
]
],
'Alias'=>[
[
"object"=>"category",
"object_id"=>1,
"name"=>"news"
],
[
"object"=>"article",
"object_id"=>1,
"name"=>"bomb"
]
],
'Setting'=>[
[
"name"=>"站点åç§°",
"keyword"=>"site_title",
"value"=>"天铿£‹é™¢",
],
[
"name"=>"站点æè¿°",
"keyword"=>"site_description",
"value"=>"秦皇岛最好的",
],
[
"name"=>"站点关键å—",
"keyword"=>"site_keywords",
"value"=>"秦皇岛,棋院",
],
[
"name"=>"LOGO",
"keyword"=>"logo_dir",
"value"=>"#",
],
[
"name"=>"地å€",
"keyword"=>"address",
"value"=>"海港区",
],
[
"name"=>"邮箱",
"keyword"=>"email",
"value"=>"123@123.com",
],
[
"name"=>"电è¯",
"keyword"=>"phone",
"value"=>"11111111111",
],
[
"name"=>"备案å·",
"keyword"=>"icp_number",
"value"=>"888",
],
[
"name"=>"首页专题",
"keyword"=>"index_topic_id",
"value"=>"1",
],
[
"name"=>"导航分类",
"keyword"=>"nav_category_id",
"value"=>"5",
],
[
"name"=>"勿ƒ…链接分类",
"keyword"=>"favolink_category_id",
"value"=>"5",
]
]
];
foreach ($dataAll as $table => $datas) {
$model = new $table();
if($table == "TopicCategory") $table_str = "topic_category";
else $table_str = $table;
$model->getReadConnection()->query("TRUNCATE $table_str;");
foreach ($datas as $data) {
$model = new $table();
$model->save($data);
}
}
}
}
|