/** * testFindField method * * @return void */ public function testFindField() { $this->loadFixtures('User'); $TestModel = new User(); $TestModel->id = 1; $result = $TestModel->field('user'); $this->assertEquals('mariano', $result); $result = $TestModel->field('User.user'); $this->assertEquals('mariano', $result); $TestModel->id = false; $result = $TestModel->field('user', array('user' => 'mariano')); $this->assertEquals('mariano', $result); $TestModel->order = null; $result = $TestModel->field('COUNT(*) AS count', true); $this->assertEquals(4, $result); $result = $TestModel->field('COUNT(*)', true); $this->assertEquals(4, $result); }
/** * ブログ記事を取得する * * @param array $options * @return array * @access protected */ protected function _getBlogPosts($options = array()) { // listCountの処理 (num が優先) // TODO num に統一する if (!empty($options['listCount'])) { if (empty($options['num'])) { $options['num'] = $options['listCount']; } } // named の 処理 $named = array(); if (!empty($this->request->params['named'])) { $named = $this->request->params['named']; } if (!empty($named['direction'])) { $options['direction'] = $named['direction']; unset($named['direction']); } if (!empty($named['num'])) { $options['num'] = $named['num']; unset($named['num']); } if (!empty($named['page'])) { $options['page'] = $named['page']; unset($named['page']); } if (!empty($named['sort'])) { $options['sort'] = $named['sort']; unset($named['sort']); } $_conditions = array(); if (!empty($this->request->params['named'])) { if (!empty($options['conditions'])) { $_conditions = array_merge($options['conditions'], $this->request->params['named']); } else { $_conditions = $this->request->params['named']; } } elseif (!empty($options['conditions'])) { $_conditions = $options['conditions']; } unset($options['conditions']); $_conditions = array_merge(array('category' => null, 'tag' => null, 'year' => null, 'month' => null, 'day' => null, 'id' => null, 'keyword' => null, 'author' => null), $_conditions); $options = array_merge(array('direction' => $this->blogContent['BlogContent']['list_direction'], 'num' => $this->blogContent['BlogContent']['list_count'], 'page' => 1, 'sort' => 'posts_date'), $options); extract($options); $expects = array('BlogContent', 'BlogCategory', 'User', 'BlogTag'); $conditions = array('BlogPost.blog_content_id' => $this->contentId); // カテゴリ条件 if ($_conditions['category']) { $category = $_conditions['category']; $categoryId = $this->BlogCategory->field('id', array('BlogCategory.blog_content_id' => $this->contentId, 'BlogCategory.name' => $category)); if ($categoryId === false) { $categoryIds = ''; } else { $categoryIds = array(0 => $categoryId); // 指定したカテゴリ名にぶら下がる子カテゴリを取得 $catChildren = $this->BlogCategory->children($categoryId); if ($catChildren) { $catChildren = Hash::extract($catChildren, '{n}.BlogCategory.id'); $categoryIds = am($categoryIds, $catChildren); } } $conditions['BlogPost.blog_category_id'] = $categoryIds; } // タグ条件 if ($_conditions['tag']) { $tag = $_conditions['tag']; if (!is_array($tag)) { $tag = array($tag); } foreach ($tag as $key => $value) { $tag[$key] = urldecode($value); } $tags = $this->BlogPost->BlogTag->find('all', array('conditions' => array('BlogTag.name' => $tag), 'recursive' => 1)); if (isset($tags[0]['BlogPost'][0]['id'])) { $ids = Hash::extract($tags, '{n}.BlogPost.{n}.id'); $conditions['BlogPost.id'] = $ids; } else { return array(); } } // キーワード条件 if ($_conditions['keyword']) { $keyword = $_conditions['keyword']; if (preg_match('/\\s/', $keyword)) { $keywords = explode("\\s", $keyword); } else { $keywords = array($keyword); } foreach ($keywords as $key => $value) { $keywords[$key] = urldecode($value); $conditions['or'][]['BlogPost.name LIKE'] = '%' . $value . '%'; $conditions['or'][]['BlogPost.content LIKE'] = '%' . $value . '%'; $conditions['or'][]['BlogPost.detail LIKE'] = '%' . $value . '%'; } } // 年月日条件 if ($_conditions['year'] || $_conditions['month'] || $_conditions['day']) { $year = $_conditions['year']; $month = $_conditions['month']; $day = $_conditions['day']; $db = ConnectionManager::getDataSource($this->BlogPost->useDbConfig); $datasouce = strtolower(preg_replace('/^Database\\/Bc/', '', $db->config['datasource'])); switch ($datasouce) { case 'mysql': case 'csv': if ($year) { $conditions["YEAR(BlogPost.posts_date)"] = $year; } if ($month) { $conditions["MONTH(BlogPost.posts_date)"] = $month; } if ($day) { $conditions["DAY(BlogPost.posts_date)"] = $day; } break; case 'postgres': if ($year) { $conditions["date_part('year',BlogPost.posts_date) = "] = $year; } if ($month) { $conditions["date_part('month',BlogPost.posts_date) = "] = $month; } if ($day) { $conditions["date_part('day',BlogPost.posts_date) = "] = $day; } break; case 'sqlite': if ($year) { $conditions["strftime('%Y',BlogPost.posts_date)"] = $year; } if ($month) { $conditions["strftime('%m',BlogPost.posts_date)"] = sprintf('%02d', $month); } if ($day) { $conditions["strftime('%d',BlogPost.posts_date)"] = sprintf('%02d', $day); } break; } } //author条件 if ($_conditions['author']) { $author = $_conditions['author']; App::uses('User', 'Model'); $user = new User(); $userId = $user->field('id', array('User.name' => $author)); $conditions['BlogPost.user_id'] = $userId; } if ($_conditions['id']) { $conditions["BlogPost.no"] = $_conditions['id']; $expects[] = 'BlogComment'; $this->BlogPost->hasMany['BlogComment']['conditions'] = array('BlogComment.status' => true); $num = 1; } unset($_conditions['author']); unset($_conditions['category']); unset($_conditions['tag']); unset($_conditions['keyword']); unset($_conditions['year']); unset($_conditions['month']); unset($_conditions['day']); unset($_conditions['id']); unset($_conditions['page']); unset($_conditions['num']); unset($_conditions['sort']); unset($_conditions['direction']); if ($_conditions) { // とりあえず BlogPost のフィールド固定 $conditions = array_merge($conditions, $this->postConditions(array('BlogPost' => $_conditions))); } // プレビューの場合は公開ステータスを条件にしない if (!$this->preview) { $conditions = array_merge($conditions, $this->BlogPost->getConditionAllowPublish()); } $this->BlogPost->expects($expects, false); $order = "BlogPost.{$sort} {$direction}"; // 毎秒抽出条件が違うのでキャッシュしない $this->paginate = array('conditions' => $conditions, 'fields' => array(), 'order' => $order, 'limit' => $num, 'recursive' => 1, 'cache' => false); return $this->paginate('BlogPost'); }