我创建了一个类,它将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 :: class与模型Category类非常相似。
答案 0 :(得分:1)
我看到的一个问题是你获取结果并构建一个类来包含你可以使用PDO :: FETCH_CLASS的数据,它们以更优化的方式执行相同操作。
但是像Michael J.V.所说的那样你应该对你的应用程序进行一些监控,以发现问题的确切原因(可能有多个并且不容易被眼睛捕获)