joomla / model
Joomla 模型包
3.0.0
2023-10-08 13:22 UTC
Requires
- php: ^8.1.0
Requires (Dev)
- joomla/database: ^3.0
- joomla/registry: ^3.0
- phan/phan: ^5.4.2
- phpstan/phpstan: ^1.10.7
- phpunit/phpunit: ^9.5.28
- squizlabs/php_codesniffer: ^3.7.2
Suggests
- joomla/database: ^3.0 Allows using database models
- joomla/registry: ^3.0 Allows using stateful models
This package is auto-updated.
Last update: 2024-09-03 21:59:28 UTC
README
接口
Model\ModelInterface
Model\ModelInterface
是一个接口,要求实现类必须包含getState
和setState
方法。
类
Model\AbstractModel
构造
新Model\AbstractModel
对象的构造函数接受一个可选的Registry
对象,该对象定义了模型的状态。如果省略,将自动分配一个空的Registry
对象。
用法
Model\AbstractModel
类是抽象的。接口的所有要求都已被基类满足。
namespace MyApp; use Joomla\Model\AbstractModel; /** * My custom model. * * @pacakge Examples * * @since 1.0 */ class MyModel extends AbstractModel { /** * Get the time. * * @return integer * * @since 1.0 */ public function getTime() { return time(); } }
Model\AbstractDatabaseModel
构造
Model\AbstractDatabaseModel
继承自Model\AbstractModel
,构造函数需要一个必需的Database\DatabaseDriver
对象和一个可选的Registry
对象。
用法
Model\AbstractDatabaseModel
类是抽象的,不能直接使用。它为任何需要与数据库交互的模型提供了一个基础。
namespace MyApp use Joomla\Model; use Joomla\Database; /** * My custom database model. * * @package Examples * * @since 1.0 */ class MyDatabaseModel extends Model\AbstractDatabaseModel { /** * Get the content count. * * @return integer * * @since 1.0 * @throws RuntimeException on database error. */ public function getCount() { // Get the query builder from the internal database object. $q = $this->db->getQuery(true); // Prepare the query to count the number of content records. $q->select('COUNT(*)')->from($q->qn('#__content')); $this->db->setQuery($q); // Execute and return the result. return $this->db->loadResult(); } } try { $driver = Database\DatabaseFactory::getInstance()->getDriver('mysqli'); $model = new MyDatabaseModel($driver); $count = $model->getCount(); } catch (RuntimeException $e) { // Handle database error. }
通过Composer安装
将"joomla/model": "~3.0"
添加到composer.json中的require块,然后运行composer install
。
{ "require": { "joomla/model": "~3.0" } }
或者,您可以直接从命令行运行以下命令
composer require joomla/model "~3.0"