PHP code example of joomla / model
1. Go to this page and download the library: Download joomla/model library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
joomla / model example snippets
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();
}
}
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.
}