ActiveRecord

ساخت وبلاگ
سلام
تو قسمت 11 آموزش ActiveRecord وقتی که میخوایم category ها رو نمایش بدیم واسه من

این  waingرو میده و اجرا نمیشه

نقل قول:


Waing: Invalid argument supplied for foreach() in C:xampphtdocsmvc-hbpappviewssiteindex.php on line 1

فایل mvc-hbpcorelibrariesActiveRecord.php

<?php
class ActiveRecord
{ protected $model; protected $fields = []; protected $isNew; public function __construct() { $this->model = new Model(); foreach($this->fields as $key => $value){ $this->fields[$key] = null; } $this->isNew = true; } public function __get($name) { retu (isset($this->fields[$name]) ? $this->fields[$name] : null); } public function __set($name, $value) { if(array_key_exists($name, $this->fields)){ $this->fields[$name] = $value; } } public function findByPK($id) { $className = get_class($this); $obj = new $className(); $id = $this->model->escape($id); $data = $this->model->arrayQuery("SELECT * FROM `{$this->tableName}` WHERE (`id`={$id})"); if(count($data) > 0){ foreach($data[0] as $fieldName => $value){ $obj->$fieldName = $value; } $obj->isNew = false; } retu $obj; } public function findAll() { $objs = null; $className = get_class($this); $data = $this->model->arrayQuery("SELECT * FROM `{$this->tableName}` ORDER BY `id`"); if(count($data) > 0){ $objs = []; foreach($data as $item ){ $obj = new $className(); foreach($item as $fieldName => $value){ $obj->$fieldName = $value; } $obj->isNew = false; $objs[] = $obj; } } retu $objs; } public function save() { retu ($this->isNew ? $this->insert() : $this->update()); } public function insert() { $fields = array_map([$this->model, 'escape'], $this->fields); $query = "INSERT INTO `{$this->tableName}` VALUES ("; foreach($fields as $field){ $query .= $field . ','; } $query .= substr($query, 0, -1) . ')'; $result = $this->model->query($query); $this->isNew = false; $this->id = $this->model->insertId(); retu $result; } public function update() { $fields = array_map([$this->model, 'escape'], $this->fields); $query = "UPDATE `{$this->tableName}` SET ("; foreach($fields as $key => $value){ if($key == 'id'){ continue; } $query .= "`{$key}` = {$value},"; } $query .= substr($query, 0, -1) . ") WHERE (`id`={$fields['id']})"; $result = $this->model->query($query); $this->isNew = false; retu $result; } public function delete() { $id = $this->model->escape($this->id); $query = "DELETE FROM `{$this->tableName}` WHERE (`id`={$id})"; retu $this->model->query($query); }
}

فایل mvc-hbpcorelibrariesController.php
<?php
class Controller
{ public $layout = 'main'; public function config($name) { retu Loader::load('Configs')->$name; } protected function getParam($paramName, $params) { retu (isset($params[$paramName]) ? $params[$paramName] : null); } protected function loadView($view, $values = [], $useLayout = false) { extract($values); $controller = strtolower(substr(get_class($this), 0, -10)); $viewFile = Base::path() . "/app/views/{$controller}/{$view}.php"; if(!file_exists($viewFile)){ throw new Exception("View '{$view}.php' was not found in 'views/{$controller}' directory."); } ob_start(); require $viewFile; $content = ob_get_clean(); if($useLayout){ $layoutFile = Base::path() . "/app/views/layouts/{$this->layout}.php"; if(!file_exists($layoutFile)){ throw new Exception("Layout '{$this->layout}.php' was not found in 'views/layouts' directory."); } require $layoutFile; } else { echo $content; } } public function render($view, $values = []) { $this->loadView($view, $values, true); } public function renderPartial($view, $values = []) { $this->loadView($view, $values); } public function renderText($text) { echo $text; } public function renderContent($content) { $layoutFile = Base::path() . "/app/views/layouts/{$this->layout}.php"; if(!file_exists($layoutFile)){ throw new Exception("Layout '{$this->layout}.php' was not found in 'views/layouts' directory."); } require $layoutFile; }
}

فایل mvc-hbpappcontrollersSiteController.php
<?php
class SiteController extends Controller
{ public function actionIndex() { $c = new Categories(); $categories = $c->findAll('categories'); $this->render('index', compact('categories')); }
}

فایل mvc-hbpappmodelsCategories.php
<?php
class Categories extends ActiveRecord
{ protected $tableName = 'categories'; protected $fields = [ 'id' => null, 'title' => null, 'urlkey' => null, 'active' => null, ];
}

فایل mvc-hbpappviewssiteindex.php

<?php foreach($categories as $category) : ?> <p><?= Html::encode($category->title) ?></p>
<?php endforeach; ?>

- - , .
.

برنامه نویس...
ما را در سایت برنامه نویس دنبال می کنید

برچسب : نویسنده : خنجی prog بازدید : 241 تاريخ : يکشنبه 20 تير 1395 ساعت: 22:47

خبرنامه