user622378
user622378

Reputation: 2336

Very slow when getting data from class

I have created a class, it store the data from mySQL database into class.

The performance is really slow when outputting to the browser, I have to wait like 10 seconds and this is not acceptable. How to solve this problem?

In the controller file:

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

View File:

<?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; ?>

model Category class file:

<?

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 is very similar to model Category class.

Upvotes: 0

Views: 559

Answers (1)

Jonatan Cloutier
Jonatan Cloutier

Reputation: 929

one problem that I see is that you take the result and build a class to contain the data you could use PDO::FETCH_CLASS that does the same but in a more optimize way.

But like Michael J.V. said you should do some monitoring of your application to detect the exact cause of the problem (there might be multiple and not easy to catch with the eyes)

Upvotes: 1

Related Questions