really stuck: saved data in model seems empty in view :(

137 views
Skip to first unread message

vinnyboy

unread,
Sep 30, 2012, 10:08:21 AM9/30/12
to joomla-de...@googlegroups.com
Hi,

I'm new to Joomla MVC development, and I've spent many hours looking for an error.  The problem is
-In the controller, I set data into the model
-the data is set correctly in the model.
-then from the view, I try to load the data, but it does not work.

Can someone please help me out where my error is?

This is the output from controller,model and view:

controller finds eid: 4
i am the model, i have set the id to 4
model KskzEventsModeleditevent set in view: KskzEventsViewEditEvent
view tries to get data from model KskzEventsModeleditevent:
view tries again to get data from model KskzEventsModeleditevent:

Controller code:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controller library
jimport('joomla.application.component.controller');
 
class KskzEventsController extends JController
{   
    function display($cachable = false,$urlparams=false)
    {
        // call parent behavior
        parent::display($cachable);
    }
   
    function submit()
    {
        $document = JFactory::getDocument();
        $vType = $document->getType();
        $eid = JRequest::getCmd('eid', -1);
        die('mama'.$eid);
        $vName = JRequest::getCmd('view', 'kskzevents');
        JRequest::setVar('view', $vName);
        $view = $this->getView($vName, $vType);
        $data = JRequest::getVar('jform', array(), 'post', 'array');
        print_r($data);
        $mName = 'editevent';
        if ($model = $this->getModel($mName)) {
            // Push the model into the view (as default)
            $model->eid = $eid;
            die($eid);
            $model->set_eid($eid);
            $model->data = $data;
            $view->setModel($model, true);
            if($eid>0) $res = $model->updEvent($eid);
            else $res = $model->addEvent();
            if ($res==true) echo('event saved');
            else echo('event not saved');
        }   
    }
   
    function add()
    {
        $document = JFactory::getDocument();
        $vType = $document->getType();
       
        $vName = JRequest::getCmd('view', 'editevent');
        JRequest::setVar('view', $vName);
        $eid = JRequest::getCmd('id', -1);
        echo('controller finds eid: '.$eid);
        $etitle = JRequest::getCmd('title', -1);
        $etype = JRequest::getCmd('eventtype','');
        $estartdateincl = JRequest::getCmd('startdateincl','');
        $enddateincl = JRequest::getCmd('enddateincl','');
        $additionalinfo = JRequest::getCmd('additionalinfo','');
        $overridedate = JRequest::getCmd('overridedate','');
        $forwho = JRequest::getCmd('forwho','');
        $place = JRequest::getCmd('place','');
        $nationalteam = JRequest::getCmd('nationalteam','');
       
        $view = $this->getView($vName, $vType);
        $mName = 'editevent';
        if ($model = $this->getModel($mName)) {
            // Push the model into the view (as default)
            //die($model->get_shit());
            $model->eid = $eid;
            $model->set_eid($eid);           
           
            //die('lala'.$model->eid);
            $model->etitle = 'etitle';
            //die('test :'.$model->get_title());
            $model->etype = $etype;
            $model->estartdateincl = $estartdateincl;
            $model->enddateincl = $enddateincl;
            $model->additionalinfo = $additionalinfo;
            $model->overridedate = $overridedate;
            $model->forwho = $forwho;
            $model->place = $place;
            $model->nationalteam = $nationalteam;
            $view->setModel($model, true);
            echo('<br>model '.$model.' set in view: '.$view);
        }
        else die('mtf');
        parent::display(false);
    }
}



model (editevent.php):

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
 
// Include dependancy of the main model form
jimport('joomla.application.component.modelform');
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
// Include dependancy of the dispatcher
jimport('joomla.event.dispatcher');

class KskzEventsModeleditevent extends JModelForm
{
    public $eid;
    public $etitle;
    public $etype = '';
    public $estartdateincl = '';
    public $enddateincl = '';
    public $additionalinfo = '';
    public $overridedate = '';
    public $forwho = '';
    public $place = '';
    public $nationalteam = '';
        
    public function getForm($data = array(), $loadData = true)
    {
        $app = JFactory::getApplication('site');
        $form = $this->loadForm('com_kskzevents.editevent', 'editevent', array('control' => 'jform', 'load_data' => true));
        if (empty($form)) {
            return false;
        }
        return $form;
    }
    public function addEvent()
    {
        echo('adding...');
        // update click count
        $db    = $this->getDbo();
        $q = 'insert into #__tevent(eventtype,startdateincl,enddateincl,title,additionalinfo,overridedate,forwho,place,nationalteam) ';
        $q.= 'values ';
        $q.= '("'.$this->data['etype'].'"';
        $q.= ',"'.$this->data['estartdateincl'].'"';
        $q.= ',"'.$this->data['eenddateincl'].'"';
        $q.= ',"'.$this->data['etitle'].'"';
        $q.= ',"'.$this->data['eadditionalinfo'].'"';
        $q.= ',"'.$this->data['eoverridedate'].'"';
        $q.= ',"'.$this->data['eforwho'].'"';
        $q.= ',"'.$this->data['eplace'].'"';
        $q.= ',"'.$this->data['enationalteam'].'"';
        $q.= ')';
        $db->setQuery($q);
        if (!$db->query($q)) {
            JError::raiseError(500, $db->getErrorMsg());
            return false;
        }
        return true;
    }
   
    public function updEvent($eid)
    {
        // update click count
        die('nyi');
        $db        = $this->getDbo();
        $q = 'insert into #__paddles(name) values ("'.$this->data['pname'].'")';
        $db->setQuery($q);
        if (!$db->query($query)) {
            JError::raiseError(500, $db->getErrorMsg());
            return false;
        }
        return true;
    }
    public function set_Title($s) { $this->etitle = $s;}
    public function get_Title() {return $this->etitle;}
    public function get_eid() { return $this->eid;}
    public function getEid() { return $this->eid;}
    public function set_eid($i) { $this->eid = $i;echo('<br>i am the model, i have set the id to '.$this->eid);}
    public function get_type() { return $this->etype;}
    public function get_startdateincl() { return $this->estartdateincl;}
    public function get_enddateincl() { return $this->enddateincl;}
    public function get_additionalinfo() { return $this->additionalinfo;}
    public function get_overridedate() { return $this->overridedate;}
    public function get_forwho() {return $this->forwho;}
    public function get_place() { return $this->place;}
    public function get_nationalteam() { return $this->nationalteam;}

}



view:

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('joomla.application.component.view');
 
/**
 * HTML View class for the HelloWorld Component
 */

class KskzEventsViewEditEvent extends JView
{
    function display($tpl = null)
    {
        $model = $this->getModel();
        $i = $model->get('eid');
        echo('<br>view tries to get data from model '.$model.': '.$i);        


        $i = $this->get('eid');
        echo('<br>view tries again to get data from model '.$model.': '.$i);        

        
        die('');
        $this->assignRef('eventid',$i);
        $var1 = 'jaja';
        $this->assignRef('var1',$var1);
        parent::display($tpl);
    }
   
   

}


Any help appreciated!
 

 

vinnyboy

unread,
Sep 30, 2012, 11:08:13 AM9/30/12
to joomla-de...@googlegroups.com
why is the data not available in the model when I check it from the view?  I checked the name of the model in the view, and it's the correct one.  Can it be another instance?

Nils Rückmann

unread,
Sep 30, 2012, 7:19:17 PM9/30/12
to joomla-de...@googlegroups.com

hoochicken

unread,
Oct 1, 2012, 2:41:06 AM10/1/12
to joomla-de...@googlegroups.com
Hi,

(0) There are many ways
to attach data. The following is only one (and it took me some time to find it out exactly how it all worked together, still some questionmarks left). Adjust it to your case, good luck.
@everybody: If there is a mistake in my explanation or you have things to add, I'm grateful for any further hints:-)

(1) get data in view

In model there is the method getForm().
In view you can access the return of this method via $this->get('form')
 if model::getForm(); in view::display(){...$this->get('form')} ;
(if model::getMessage(); in view::display(){...$this->get('message')} ;
 if model::getThingsies(); in view::display(){...$this->get('thingsies')} a. s. o....)
Maybe this already helps.

(2) Attach form to make it accessable in template
view::display(){...$this->form=$this->get('form');...}
template: print_r($this->form); //test output

(3) Attach data to form
Test with <?php echo "<pre>"; print_r($this->get('form'));die; //test output ?>, if the formdata is loaded. If not:

(3.1) add an getItem-method
model:: getItem($id) { $db->setQuery("SELECT * FROM #__mytable WHERE id=$id");$db=JFactory::getDBO;db->query(); return $db->loadObject();}

(3.2) add method loadFormData()
add following method to model; it should be used automatically when getForm() is used (and the $loaddata variable is not set to false)
    /**
     * Method to get the data that should be injected in the form.
     *
     * @return    mixed    The data for the form.
     * @since    1.6
     */
    protected function loadFormData()
    {
        return $this->getItem();
    }

(4) bind()?
If this does not work, you might find the method bind() kinda useful: http://docs.joomla.org/API16:JForm

(5) Hope this helps you.

Greetings
Mareike

Vincent Naert

unread,
Oct 3, 2012, 12:08:15 PM10/3/12
to joomla-de...@googlegroups.com
Thank you for the link.  I've spent a lot of hours on the tutorials... As soon as i start building a new component, I suddenly get stuck somewhere.

On Mon, Oct 1, 2012 at 1:19 AM, Nils Rückmann <in...@nils-rueckmann.de> wrote:

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/12ixCkv-MO8J.

To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.

Vincent Naert

unread,
Oct 3, 2012, 12:52:20 PM10/3/12
to joomla-de...@googlegroups.com
Hi everyone,

thanks for the reactions and tips.
After some more searching i can summarize this:  The problem must be when controller sets the data in model.  It seems to be set fine, but it is not remembered.
The view will get the data, if it is a constant value (a "hello world" string) :So the problem is not in getting the data.
Can anyone see what i'm doing wrong in the function below?  Because I don't understand why it's not working...


    class KskzEventsController extends JController
{
function add()

    {
        $document = JFactory::getDocument();
        $vType = $document->getType();
       
        $vName = JRequest::getCmd('view', 'editevent');
        JRequest::setVar('view', $vName);
        $eid = JRequest::getCmd('id', -1);
        echo('controller finds eid: '.$eid);
      
        $view = $this->getView($vName, $vType);
        $mName = 'editevent';
        if ($model = $this->getModel($mName)) {
            $model->eid = $eid;
            $model->set_eid($eid);       //STILL OK HERE: debug output = "i am the model, i have set the id to 4   " 
          
            $view->setModel($model, true);
            echo('<br>model '.$model.' set in view: '.$view);
        }
        else die('mtf');
        parent::display(false);// PROBLEM from here on, when the view displays, the data is not available anymore in the model!
    }
}
 


the relevant code from the model:

class KskzEventsModeleditevent extends JModelForm
{
     public $eid; //this shoul be fine?  it's a public member var


     public function set_eid($i) {
           $this->eid = $i;
           echo('<br>i am the model, i have set the id to '.$this->eid); //STILL OK HERE: debug output = "i am the model, i have set the id to 4   " 
      }

public function getEid() { return $this->eid;} //this shoul be fine?  it should return the correct value?
}


and in short the display function:

function display($tpl = null)
    {
        $model = $this->getModel();
        $i = $this->get('eid');
        echo('<br>view tries again to get data from model '.$model.': '.$i);       
}

regards,
vincent

Sam Moffatt

unread,
Oct 4, 2012, 11:19:10 PM10/4/12
to joomla-de...@googlegroups.com
Look at what parent::display() does, which in your case is
JController. Looking at Joomla Master, JControllerLegacy has this
line:
https://github.com/joomla/joomla-cms/blob/master/libraries/legacy/controller/legacy.php#L646

Cheers,

Sam Moffatt
http://pasamio.id.au
> --
> You received this message because you are subscribed to the Google Groups
> "Joomla! General Development" group.

Vincent Naert

unread,
Oct 5, 2012, 6:54:22 AM10/5/12
to joomla-de...@googlegroups.com
Hi Sam!
thank you so much for the help!
i changed parent::display to $view->display which does the job!
Thanks a lot!
Vincent
Reply all
Reply to author
Forward
0 new messages