Domanda

Voglio fare uso di Metodi Magici nel mio Modulo Personalizzato.

Ho creato un nuovo Modello con diverse variabili membro (variabile1, variabile2) e voglio ottenere come getVariable1.

Come devo dichiarare le variabili membro?Posso impostare loro in privato?Cos'altro mi manca?

Da qualche parte ho letto che ho bisogno di salvare in $_data['variable1'], ma che non funziona.

Grazie!

È stato utile?

Soluzione

La magia, i metodi sono implementati tramite Varien_Object classe.

public function __call($method, $args)
{
    switch (substr($method, 0, 3)) {
        case 'get' :
            //Varien_Profiler::start('GETTER: '.get_class($this).'::'.$method);
            $key = $this->_underscore(substr($method,3));
            $data = $this->getData($key, isset($args[0]) ? $args[0] : null);
            //Varien_Profiler::stop('GETTER: '.get_class($this).'::'.$method);
            return $data;

        case 'set' :
            //Varien_Profiler::start('SETTER: '.get_class($this).'::'.$method);
            $key = $this->_underscore(substr($method,3));
            $result = $this->setData($key, isset($args[0]) ? $args[0] : null);
            //Varien_Profiler::stop('SETTER: '.get_class($this).'::'.$method);
            return $result;

        case 'uns' :
            //Varien_Profiler::start('UNS: '.get_class($this).'::'.$method);
            $key = $this->_underscore(substr($method,3));
            $result = $this->unsetData($key);
            //Varien_Profiler::stop('UNS: '.get_class($this).'::'.$method);
            return $result;

        case 'has' :
            //Varien_Profiler::start('HAS: '.get_class($this).'::'.$method);
            $key = $this->_underscore(substr($method,3));
            //Varien_Profiler::stop('HAS: '.get_class($this).'::'.$method);
            return isset($this->_data[$key]);
    }
    throw new Varien_Exception("Invalid method ".get_class($this)."::".$method."(".print_r($args,1).")");
}

È necessario estendere la classe o di qualsiasi altra classe che si estende da Varien_Object come Mage_Core_Model_Abstract per esempio.Non è necessario definire tutte le variabili o metodi del modello, come lungo come metodo inizia con get, set, uns e has, che è supponendo che si desidera loro di lavorare come metodi magici.

Così si può scrivere qualcosa di simile a questo:

$model = Mage::getModel('namespace_module/sample');
$model->setSomeData('Hello');
echo $model->getSomeData();

Se si Zend_Debug::dump($model); si noterà che i vostri dati sono memorizzati in _data la struttura, che è stato dichiarato in Varien_Object classe.

Esempio:

object(Namespace_Module_Model_Sample)#128 (7) {
  ["_data":protected] => array(1) {
    ["some_data"] => string(5) "Hello"
  }
  ["_hasDataChanges":protected] => bool(true)
  ["_origData":protected] => NULL
  ["_idFieldName":protected] => NULL
  ["_isDeleted":protected] => bool(false)
  ["_oldFieldsMap":protected] => array(0) {
  }
  ["_syncFieldsMap":protected] => array(0) {
  }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top