0

I have created a custom module with my own db table. My question is; what data is available to me in the block class while I'm editing an entry?

For example, I use the products collection in my block and call it as follows:

$collection = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->addAttributeToSelect('*');

Now, I can use the getters to retrieve info, such as:

$collection->getEmail();
$collection->getFirstname();
... etc, etc

All good. Now, if my custom model is called test_model/test, and I have an instance like so:

$test = Mage::getModel('test_model/test');

Calling a getter for getMyfield(); produces nothing. Obviously, if I explicitly load an item:

$test = Mage::getModel('test_model/test')->load('1');

Then I do have those getters available.

So... what am I missing? Why do I have to load the row I want first? Shouldn't it be available on edit with $this->getMyfield();.

Help a Mage nooby out, please.

0

If you use Mage::getModel('some/model') method Magento only returns model class without values.
If you use Mage::getModel('some/model')->load($id) then Magento loads the object (one row) from db table and returns this object with it's values. And you can get some value via $model->getMyField(). Also you can define some methods and default values for model class:

class My_SomeModule_Model_SomeModel extends Mage_Core_Model_Abstract{
    protected function _construct()
    {
        $this->_init('somemodule/somemodel');
    }
    public function getSomeValue(){
        //here is your some logic to calculate or load some $value
        return $value; 
    }
    public function validate(){
        //you can write object values validation logic  
        return $this->getMyField()!==null;
    }
    protected function _beforeSave(){
        if(!$this->validate()){
           throw new Exception('Some validation Message here');
        }
        return parent::_beforeSave();
    }
}

Using model:

$model=Mage::getModel('somemodule/somemodel')->load($id);
$model->setMyField($model->getSomeValue());
$model->setAnotherField('some value');
$model->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.