Model
โมเดลเป็นส่วนที่จัดการเกี่ยวกับข้อมูล ไม่ว่าจะเป็นฐานข้อมูลที่เก็บในเครื่อง หรือฐานข้อมูลภายนอกก็สามารถจัดการ โมเดลยังสามารถรับข้อมูลจากแหล่งอื่นได้ เช่น ผ่าน API บริการเว็บที่ทํางานบนเซิร์ฟเวอร์อื่น โมเดลยังสามารถในการอัพเดตฐานข้อมูลตามความเหมาะสม วัตถุประสงค์ของดมเดลคือการแยกตัวควบคุมและดูจากรายละเอียดของวิธีการรับหรือแก้ไขข้อมูล
การสร้าง Model ใน Component Joomla
สร้างโฟเดอร์ model และไฟล์นามสกุลphp
โค้ดในรูปแบบ Joomla ต้องขึ้นต้นโค้ดต่อไปนี้
โค้ด: เลือกทั้งหมด
<?php
/*
* @package MD Gallery
* @subpackage com_mdgallery
* @author Mindphp Developer Teams
* @copyright Copyright (C) 2015 Mindphp. All rights reserved..
* @license GNU General Public License version 2 or later;
*/
defined('_JEXEC') or die;
โค้ด: เลือกทั้งหมด
class TestModel extends JModelList
{
}
โค้ด: เลือกทั้งหมด
public function __construct($config = array ())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array (
'id', 'a.id',
'name', 'a.name',
'email', 'a.email',
);
}
parent::__construct($config);
}
โค้ด: เลือกทั้งหมด
protected function populateState($ordering='a.name',$direction='asc')
{
$this->setState('filter.search', $this->getUserStateFromRequest($this->context.'.filter.search','filter_search','', 'string'));
parent::populateState($ordering,$direction)
}
โค้ด: เลือกทั้งหมด
protected function getListQuery()
{
$db = $this->getDbo();
$query = $db->getQuery(true);
$query->select('*')->from('#__test AS a');
}
โค้ด: เลือกทั้งหมด
if ($this->getState('filter.search'))
{
$token = $db->q('%' . $db->escape($this->getState('filter.search')) . '%');
$searches = array ();
$searches[] = 'a.name LIKE ' . $token;
$searches[] = 'a.email LIKE ' . $token;
$query->where('(' . implode(' OR ', $searches) . ')');
}
ไฟล์รวมโค้ดที่เขียนทั้งหมด
โค้ด: เลือกทั้งหมด
<?php
/*
* @package MD Gallery
* @subpackage com_mdgallery
* @author Mindphp Developer Teams
* @copyright Copyright (C) 2015 Mindphp. All rights reserved..
* @license GNU General Public License version 2 or later;
*/
defined('_JEXEC') or die;
class TestModel extends JModelList
{
public function __construct($config = array ())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array (
'id', 'a.id',
'name', 'a.name',
'email', 'a.email',
);
}
parent::__construct($config);
}
protected function populateState($ordering='a.name',$direction='asc')
{
$this->setState('filter.search', $this->getUserStateFromRequest($this->context.'.filter.search','filter_search','', 'string'));
parent::populateState($ordering,$direction)
}
protected function getListQuery()
{
$db = $this->getDbo();
$query = $db->getQuery(true);
$query->select('*')->from('#__test AS a');
if ($this->getState('filter.search'))
{
$token = $db->q('%' . $db->escape($this->getState('filter.search')) . '%');
$searches = array ();
$searches[] = 'a.name LIKE ' . $token;
$searches[] = 'a.email LIKE ' . $token;
$query->where('(' . implode(' OR ', $searches) . ')');
}
}
}
การสร้าง Component Joomla เป็นการสร้างแบบ MVC ซึ่งโมเดลเป็นส่วนหนึ่งใน MVC นั้นเอง โมเดลเป็นส่วนที่จัดการเกี่ยวกับข้อมูล โมเดลจะทำหน้าที่รับข้อมูลภายในฐานข้อมูล และภายนอก APi บริการเว็บที่ทํางานบนเซิร์ฟเวอร์อื่น ภายในโมเดลนั้นจะมีส่วนการรับค่าจากแปรฟอร์ม มีการค้นหาค่าในฐานข้อมูล และมีการบันทึกค่าในฐานข้อมูล
อ้างอิง
https://docs.joomla.org/Model-View-Controller
https://joomla-thailand.blogspot.com/2013/11/joomla-module.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/%E0%B8%AA%E0%B8%AD%E0%B8%99-joomla-extension/1971-%E0%B8%AA%E0%B8%A3%E0%B9%89%E0%B8%B2%E0%B8%87-component-joomla-medel.html