JModelForm's PopulateState

235 views
Skip to first unread message

Christopher Garvis

unread,
Dec 31, 2009, 1:22:23 PM12/31/09
to Joomla! CMS Development
I've noticed a common theme with components that I'm building using
JModelForm in the _populateState and a few other methods. I think
adding the code below to JModelForm would make development even easier/
faster using JModelForm in conjunction with JControllerForm.

class JModelForm extends JModel
{
/**
* @var string The URL option for the component.
*/
protected $_option = null;

public function __construct($config = array())
{
parent::__construct($config);

// Guess the option as the suffix, eg: OptionControllerContent.
if (empty($this->_option)) {
$r = null;
if (!preg_match('/(.*)Model(.*)/i', get_class($this), $r)) {
JError::raiseError(500, 'JModel_Error_Cannot_parse_option');
}
$this->_option = 'com_'.strtolower($r[1]);
}
}

/**
* Method to auto-populate the model state.
*
* This method should only be called once per instantiation and is
designed
* to be called on the first call to the getState() method unless the
model
* configuration flag to ignore the request is set.
*
* @return void
*/
protected function _populateState()
{
$app = &JFactory::getApplication();
$context = $this->_option.'.edit.'.$this->getName().'.id';

// Load the User state.
if (!($pk = (int) $app->getUserState($context.'.id'))) {
$pk = (int) JRequest::getInt('id');
}
$this->setState($this->_name.'.id', $pk);
}

/**
* Method to get a table object, load it if necessary.
*
* @param string The table name. Optional.
* @param string The class prefix. Optional.
* @param array Configuration array for model. Optional.
* @return object The table
*/
public function &getTable($name='', $prefix='Table', $options = array
())
{
if (empty($name)) {
$name = $this->getName();
}

return parent::getTable($name, $prefix, $options);
}

/**
* Method to save a record.
*
* @param array
* @return boolean
*/
public function save($data)
{
$table = &$this->getTable();
$pk = (!empty($data['id'])) ? $data['id'] : (int)$this->getState
($this->getName().'.id');
$isNew = true;

// Load the row if saving an existing item.
if ($pk > 0) {
$table->load($pk);
$isNew = false;
}

// Bind the data.
if (!$table->bind($data)) {
$this->setError(JText::sprintf('JTable_Error_Bind_failed', $table-
>getError()));
return false;
}

// Check the data.
if (!$table->check()) {
$this->setError($table->getError());
return false;
}

// Store the data.
if (!$table->store()) {
$this->setError($table->getError());
return false;
}

// Clean the cache.
$cache = &JFactory::getCache($this->_option);
$cache->clean();

$this->setState($this->getName().'.id', $table->id );

return true;
}
}

Andrew Eddie

unread,
Jan 3, 2010, 11:51:48 PM1/3/10
to joomla-...@googlegroups.com
Hi Christopher

Yeah, I've just been waiting to finish all the models to see if
there's some commonality to make JModelForm.

Good idea and hold that thought.

Regards,
Andrew Eddie
http://www.theartofjoomla.com - the art of becoming a Joomla developer


2010/1/1 Christopher Garvis <cga...@gmail.com>:

> --
>
> You received this message because you are subscribed to the Google Groups "Joomla! CMS Development" group.
> To post to this group, send an email to joomla-...@googlegroups.com.
> To unsubscribe from this group, send email to joomla-dev-cm...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/joomla-dev-cms?hl=en-GB.
>
>
>

Reply all
Reply to author
Forward
0 new messages