繁体   English   中英

从课堂上获取数据时非常慢

[英]Very slow when getting data from class

我创建了一个类,它将来自mySQL数据库的数据存储到类中。

输出到浏览器时,性能确实很慢,我不得不等待10秒钟,这是不可接受的。 如何解决这个问题呢?

在控制器文件中:

$modelCategory = new modelCategory();
       $categories = $modelCategory->findAll('takeawayID = :id', array('id' => $takeawayID));
       if ($categories === false) {
               echo "Query failed";
               return;
       }
$data['categories'] = $categories;

查看文件:

<?php foreach ($categories as $category): ?>
    <div class="menu-container">
        <h2><?php echo $category->name; ?></h2>
        <ul class="menu-list">
    <?php foreach ($category->item as $item): ?>
            <li class="<?php echo $class; ?>">
                <div class="menu-holder">
                    <div class="text-block">
                         <div class="text-holder">
                             <?php if ($item->optionsTotal == 1): ?>
                                <?php foreach($item->options as $option):  ?>
                                   <?php $price = $option->price; ?>
                                   <?php $optionvalue = $option->optionValue; ?>
                                <?php endforeach; ?>
                             <?php endif; ?>
                        <h3>
                        <?php echo $item->name; ?>
                              <?php if ($item->optionsTotal == 1 && $optionvalue != "Regular"): ?>
                                 (<?php echo $optionvalue; ?>)
                               <?php endif; ?>
                         </h3>
                        <?php if ($item->desc != ""): ?>
                           <p> <?php echo $item->desc; ?> </p>
                        <?php endif; ?>
                         </div>
                    </div>
                        </div>
                            </li>
        <?php endforeach; ?>
                            </ul>
                        </div>
<?php endforeach; ?>

模型类别类文件:

<?

class modelCategory extends Model {

    public $id;
    public $desc;
    public $name;
    public $items = null;

    public function findAll($condition, $parameters) {
        $query = "SELECT * FROM categories WHERE " . $condition;
        $statement = self::getDb()->prepare($query);
        if (!$statement->execute($parameters))
            return false;
        return self::createModels($statement->fetchAll());
    }

    public static function createModels($dataAr) {
        $models = array();
        foreach ($dataAr as $data) {
            $category = new modelCategory;
            $category->id = $data['id'];
            $category->desc = $data['description'];
            $category->name = ucwords(strtolower($data['name']));
            $models[] = $category;
        }
        return $models;
    }

    public function getItems() {
        if ($this->items === null)
            $this->items = modelItem::find('category_id = :category_id', array('category_id' => $this->id));
            return $this->items;
    }

    public function __get($key) {
        switch ($key) {
            case 'item':
                return $this->getItems();
        }
    }
}
?>

modelItem ::类与模型Category类非常相似。

我看到的一个问题是,您要获取结果并构建一个类来包含可以使用PDO :: FETCH_CLASS的数据,该类可以执行相同的操作,但方式更优化。

但是就像Michael JV所说的那样,您应该对您的应用程序进行一些监视,以检测出问题的确切原因(可能有很多原因,而且不容易被发现)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM