質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

0回答

1332閲覧

FuelPHP veiwファイルがなぜか複数込んでしまいormでクエリが重複して発行されてしまう。

amaguri

総合スコア227

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2017/07/03 05:03

編集2017/07/03 08:43

viewファイルにてviewmodel.phpのViewModelを継承しています。
このindex.phpファイルがなぜか複数回読み込んでしまいormで発行したクエリが重複してしまっているのですが
原因がわからず困っています。
クエリの重複確認は
code Profilerにて確認済みです。
他のファイルでも同様なことが起きているのでいます。

コントローラー

public function action_index() { parent::$_view->set('content', ViewModel::forge('admin/member/index')->set("self_admin",self::$_admin)); parent::$_view->set('title', 'タイトル | '.'サイト名'); return parent::$_view; }

モデル

viewmodel.php abstract class ViewModel { /** * Factory for fetching the ViewModel * * @param string ViewModel classname without View_ prefix or full classname * @param string Method to execute * @return ViewModel */ public static function forge($viewmodel, $method = 'view', $auto_filter = null, $view = null) { // if no custom view is given, make it equal to the viewmodel name is_null($view) and $view = $viewmodel; // strip any extensions from the view name to determine the viewmodel to load $viewmodel = \Inflector::words_to_upper(str_replace( array('/', DS), '_', strpos($viewmodel, '.') === false ? $viewmodel : substr($viewmodel, 0, -strlen(strrchr($viewmodel, '.'))) )); // determine the viewmodel namespace from the current request context $namespace = \Request::active() ? ucfirst(\Request::active()->module) : ''; // list of possible viewmodel classnames, start with the namespaced one $classes = array($namespace.'\\View_'.$viewmodel); // add the global version if needed empty($namespace) or $classes[] = 'View_'.$viewmodel; /** * Add non View_ prefixed classnames to the list, for BC reasons * * @deprecated 1.6 */ $classes[] = $namespace.'\\'.$viewmodel; // and add the global version of that if needed empty($namespace) or $classes[] = $viewmodel; // check if we can find one foreach ($classes as $class) { if (class_exists($class)) { return new $class($method, $auto_filter, $view); } } throw new \OutOfBoundsException('ViewModel "'.reset($classes).'" could not be found.'); } /** * @var string method to execute when rendering */ protected $_method; /** * @var string|View view name, after instantiation a View object */ protected $_view; /** * @var bool whether or not to use auto filtering */ protected $_auto_filter; /** * @var Request active request during ViewModel creation for proper context */ protected $_active_request; protected function __construct($method, $auto_filter = null, $view = null) { $this->_auto_filter = $auto_filter; $this->_view === null and $this->_view = $view; class_exists('Request', false) and $this->_active_request = \Request::active(); if (empty($this->_view)) { // Take the class name and guess the view name $class = get_class($this); $this->_view = strtolower(str_replace('_', DS, preg_replace('#^([a-z0-9_]*\\\\)?(View_)?#i', '', $class))); } $this->set_view(); $this->_method = $method; } /** * Returns the View object associated with this Viewmodel * * @return View */ public function get_view() { return $this->_view; } /** * Construct the View object */ protected function set_view() { $this->_view instanceOf View or $this->_view = \View::forge($this->_view); } /** * Returns the active request object. * * @return Request */ protected function request() { return $this->_active_request; } /** * Executed before the view method */ public function before() {} /** * The default view method * Should set all expected variables upon itself */ public function view() {} /** * Executed after the view method */ public function after() {} /** * Fetches an existing value from the template * * @return mixed */ public function & __get($name) { return $this->get($name); } /** * Gets a variable from the template * * @param string */ public function & get($key = null, $default = null) { if (is_null($default) and func_num_args() === 1) { return $this->_view->get($key); } return $this->_view->get($key, $default); } /** * Sets and sanitizes a variable on the template * * @param string * @param mixed */ public function __set($key, $value) { return $this->set($key, $value); } /** * Sets a variable on the template * * @param string * @param mixed * @param bool|null */ public function set($key, $value = null, $filter = null) { is_null($filter) and $filter = $this->_auto_filter; $this->_view->set($key, $value, $filter); return $this; } /** * Magic method, determines if a variable is set. * * isset($view->foo); * * @param string variable name * @return boolean */ public function __isset($key) { return isset($this->_view->$key); } /** * Assigns a value by reference. The benefit of binding is that values can * be altered without re-setting them. It is also possible to bind variables * before they have values. Assigned values will be available as a * variable within the view file: * * $this->bind('ref', $bar); * * @param string variable name * @param mixed referenced variable * @param bool Whether to filter the var on output * @return $this */ public function bind($key, &$value, $filter = null) { $this->_view->bind($key, $value, $filter); return $this; } /** * Change auto filter setting * * @param null|bool change setting (bool) or get the current setting (null) * @return void|bool returns current setting or nothing when it is changed */ public function auto_filter($setting = null) { if (func_num_args() == 0) { return $this->_view->auto_filter(); } return $this->_view->auto_filter($setting); } /** * Add variables through method and after() and create template as a string */ public function render() { if (class_exists('Request', false)) { $current_request = Request::active(); Request::active($this->_active_request); } $this->before(); $this->{$this->_method}(); $this->after(); $return = $this->_view->render(); if (class_exists('Request', false)) { Request::active($current_request); } return $return; } /** * Auto-render on toString */ public function __toString() { try { return $this->render(); } catch (\Exception $e) { \Error::exception_handler($e); return ''; } } }

viewファイル

<?php class View_Admin_Member_Index extends ViewModel { /** * Prepare the view data, keeping this in here helps clean up * the controller. * * @return void */ public function view() { $orm = Model_Member::getSearchOrm($this->search_cond); $orm->order_by("id","desc"); $orm->related("house"); // ページャオプション $option = Model_Member::getSearchOption($this->search_cond); // pager $disp = 30; $total = $orm->count(); $pager = new Pager($total, '/admin/member',$disp,$option); $orm->rows_limit($disp)->rows_offset($pager->getOffset()); // 問い合わせルート $channel_list = Model_ClubMember::getSelectChannelList("----"); $this->clubmembers = $orm->get(); $this->pager = $pager; $this->channel_list = $channel_list } }

追記
読みこみ回数は

if (!isset($_SESSION['count'])) { $_SESSION['count'] = 1; } else { $_SESSION['count']++; } var_dump($_SESSION['count']."回読み込み");

にて
数えました

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fagai

2017/07/03 07:31

viewファイルが3回読み込まれていると分かったのはどうしてですか? また、同じ質問をするのはやめたほうが良いです。
amaguri

2017/07/03 07:44

if (!isset($_SESSION['count'])) { $_SESSION['count'] = 1; } else { $_SESSION['count']++; } var_dump($_SESSION['count']."回読み込み");にて読み込まれた回数を数えました
fagai

2017/07/03 08:12 編集

それだけではクエリが重複してしまっているか確認できないと思うのですが。。。対象のviewメソッドの中で Log::debug("view method loaded."); などを書いてログに3回出力されますか?
amaguri

2017/07/03 08:41

記載漏れですみません、fuelphpのcode Profilerにてクエリが重複されているには確認済みです。
amaguri

2017/07/03 08:42

同じクエリが3回発行されており、ページ読み込みの回数をカウントしたら3回でした。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問