title: Insert DB Value into Form Field
body: <p><strong>The Question:</strong> How do I insert values from a database table (#__mytable) into form text fields (<code>motitle</code> and <code>modescription</code>) which have been rendered from an XML file within the Joomla 3.0 platform?</p>
  
  <p>-</p>
  
  <p>I've been trying for days to solve this "easy" Joomla! based undocumented challenge.
  I have followed <a href="http://docs.joomla.org/Developing_a_Model-View-Controller_Component/2.5/Introduction" rel="nofollow noreferrer">Joomla!'s guide for Developing an MVC</a>, read most of their out-of-date documentation and torn apart the <code>com_content</code> component but have still no idea how to populate my fields.</p>
  
  <p>I've been playing with <code>$this-&gt;form-&gt;bind($this-&gt;item);</code>.</p>
  
  <p>Below I have included some of my code to show the structure I am using.  Please feel free to point out any issues you spot along the way.  </p>
  
  <p><strong>Models\Forms\item.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;form&gt;
      &lt;fields name="groupOPTIONS"&gt;
          &lt;fieldset name="Options"&gt;
              &lt;field
                  type="text"
                  name="motitle"
                  id="motitle"
                  label="Title"
                  description="MY TEXT FIELD DESCRIPTION"
                  maxLength="255" /&gt;
              &lt;field
                  type="textarea"
                  name="modescription"
                  id="modescription"
                  label="Description"
                  description="MY TEXT FIELD DESCRIPTION"
                  rows="15"
                  cols="5"
                  maxLength="255" /&gt;
          &lt;/fieldset&gt;
      &lt;/fields&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>Models\item.php</strong></p>
  
  <pre><code>jimport('joomla.application.component.modelitem');
  class MagicObjectsModelItem extends JModelForm {
  
  public function getForm($data = array(), $loadData = true) {
      // Get the form 'items'
      $form = $this-&gt;loadForm('com_magicobjects.item', 'item',
          array('control' =&gt; 'jform', 'load_data' =&gt; $loadData));
  
  if (empty($form)) {
      return false;
  }
  
              return $form;
  }
  
  protected function loadFormData() {
      // Check the session for previously entered form data.
      $data = JFactory::getApplication()-&gt;getUserState('com_magicobjects.item.edit.data', array());
  
      if (empty($data)) {
              $data = $this-&gt;getDBItem(1);
      }
  
      return $data;
  }
  
  public function getDBItem($pk) {
      //Obtain JDatabase static connection
      $oDb = JFactory::getDbo();
      $oQuery = $oDb-&gt;getQuery(true);
      $sValueToMatch = $pk;
      $oQuery
          -&gt;select(array('mid', 'name', 'keyword', 'description'))
          -&gt;from('#__mytable')
          -&gt;where('mid = "' . $sValueToMatch . '"')
          -&gt;order('mid ASC');
  
      $oDb-&gt;setQuery($oQuery);
      return $oDb-&gt;loadObjectList();
  }
  </code></pre>
  
  <p><strong>views\item\view.html.php</strong></p>
  
  <pre><code>jimport('joomla.application.component.view');
  
  function display($tpl = null) {
  
      // Initialise variables.
      $this-&gt;form = $this-&gt;get('Form');
      $this-&gt;item = $this-&gt;get('Item');
  
  //Display the view
      parent::display($tpl);
  }
  </code></pre>
  
  <p><strong>Views\item\tmpl\default.php</strong></p>
  
  <pre><code>foreach ($this-&gt;form-&gt;getFieldset('Options') as $field) {
      echo $field-&gt;label;
      echo $field-&gt;input;
  }
  </code></pre>
  
  <p>By performing a print_r() on item I can see I have the data, but I need to insert the data into the fields shown.</p>
  
  <p><img src="https://i.stack.imgur.com/PgNr0.png" alt="Data Available in Array but not within fields"></p>