استخدم الطرق السحرية في الوحدة النمطية المخصصة

magento.stackexchange https://magento.stackexchange.com//questions/52464

  •  12-12-2019
  •  | 
  •  

سؤال

أريد أن الاستفادة من الأساليب السحرية في بلدي وحدة مخصصة.

أنا خلقت نموذجا جديدا مع متغيرات عضو مختلفة (فاريابل 1 ، فاريابل 2) وأريد الحصول عليها مثل جيتفارايبل 1.

كيف أحتاج إلى إعلان متغيرات الأعضاء هذه?هل يمكنني وضعها على القطاع الخاص?ماذا أنا في عداد المفقودين?

في مكان ما قرأت أنا بحاجة إلى حفظها في $_data['variable1'], ، لكن هذا لا يعمل.

شكراً!

هل كانت مفيدة؟

المحلول

يتم تنفيذ الأساليب السحرية عبر Varien_Object صف دراسي.

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).")");
}

ستحتاج إما إلى تمديد تلك الفئة أو أي فئة أخرى تمتد من Varien_Object مثل Mage_Core_Model_Abstract على سبيل المثال.لا يطلب منك تحديد أي متغيرات أو طرق في النموذج الخاص بك طالما أن طريقتك تبدأ بـ get, set, uns و has, ، هذا على افتراض أنك تريدهم أن يعملوا كطرق سحرية.

حتى تتمكن من كتابة شيء من هذا القبيل:

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

إذا كنت تفعل Zend_Debug::dump($model); ستلاحظ أن يتم تخزين البيانات الخاصة بك في _data الممتلكات التي أعلن عنها في Varien_Object صف دراسي.

مثال:

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) {
  }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top