2

I have legacy code which stores temporary data in the context. I would like to store this in the DB using the following model:

class Model_MyModel extends Model_Table {
    function init(){
        parent::init();
        $this->addField('myString');
    }   
}

I can access the data from within the legacy Controller thus:

class Controller_LegacyController extends Controller {
    $myString = $this->api->recall("legacyString");
}

But I can't see how to tie everything together (all the examples use a Form to link to the DB)

Thanks for your help,

Greg.

1 Answers1

2

I find your question and code a bit confusing, but I'll try to help.

  1. You don't need controller to be able to use your model. When calling $form->setModel() it automatically pick the right controller for you.

    $page->add('MVCForm')->setModel('MyModel');

  2. When you want to send data back into data-base, you should call $form->update(). There is a View you can use, which will do that for you called: FormAndSave

    $page->add('FormAndSave')->setModel('MyModel'); // will also save data back to database.

  3. If you load data from database, you need to call loadData() on the model. Your final code might look like this (stickyGET ensures that it pass get argument inside form submit handler):

    $this->api->stickyGET('id'); $page->add('FormAndSave')->setModel('MyModel')->loadData($_GET['id']);

  4. method recall() deals with sessions, so it seems as if you are reading data from the session. If you intend that and you want to see value of your session variable in the form, then this will do it:

    $form->set('myfield',$this->api->recall('legacyString'));

I hope this will give you some hints on how to continue. Look through more samples, there are lots of them on http://agiletoolkit.org

romaninsh
  • 10,606
  • 4
  • 50
  • 70
  • Hi Romaninsh, thanks for the reply. Re-reading my question I realise that I didn't understand what I was trying to ask, and therefore understandable that you found it confusing. I wanted to define a model to ease access to a database but without the use of forms. I eventually found what I was looking for (after reading your hints) at: http://agiletoolkit.org/learn/understand/model/intro and http://agiletoolkit.org/intro/models. In brief: $model=$this->add(Model_MyModel)->set(...)->update(); Thanks for your help. I'll accept your answer given that there wasn't really an answer to that question. – BlueBiscuit Sep 06 '11 at 14:32