2013-08-26 30 views
4

我已经通过一些代码XDebug的运行和我不断注意到_underscoreCache为k v对的数组:什么是_underscoreCache在Magento

object 
collection 
is_default 
default_group_id 
front 
store_id 
action 
session_hosts 
controller_action 
request 
secure 
visitor_data 
website 
quote 
items_collection 
parent_item_id 
quote_id 
item_id 
product_id 
code 
attributes 
website_id 
attribute_set_id 
additional_attribute_table 
attribute_codes 
is_global 
skip_confirmation_if_email 
confirmation 
visibility 
is_salable 
stock_item 
udropship_vendor 
custom_vars_combined 
password_enc 
vendor_name 
use_local_stock 
is_recurring 
customer_group_id 
date 
product_collection 
product_name 
product_type_id 
product_status_changed 
is_changed_websites 
product_changed_websites 
license_key 
license_key 
license_status 
license_expire 
server_restriction 
products 
module_name 
use_config_manage_stock 
is_in_stock 
stock_status 
product_type 
sku 
name 
weight 
tax_class_id 
cost 
base_cost 
is_qty_decimal 
quote_item 
product 
message 
item 
is_super_mode 
qty_to_add 
is_child_item 
has_error 
item_is_qty_decimal 
has_qty_option_update 
item_qty 
orig_qty 
use_config_min_sale_qty 
use_config_max_sale_qty 
suppress_check_qty_increments 
use_config_enable_qty_increments 
use_config_min_qty 
item_use_old_qty 
item_backorders 
ignore_old_qty 
use_old_qty 
skip_check_required_option 
ud_skip_quote_load_after_event 
order 
has_children 
vendor_sku 
address_type 
address 
postcode 
limit_zipcode 
country_id 
allowed_countries 
stockcheck_method 
true_stock 
udropship_stock_levels 
stock_result 
quote_currency_code 
currency_code 
customer_id 
prefix 
customer_prefix 
firstname 
customer_firstname 
middlename 
customer_middlename 
lastname 
customer_lastname 
suffix 
customer_suffix 
email 
customer_email 
dob 
customer_dob 
taxvat 
customer_taxvat 
gender 
customer_gender 
customer_tax_class_id 
remote_ip 
x_forwarded_for 
items_count 
checkout_method 
checkout_state 
collect_shipping_rates 
totals_collected_flag 
extra_tax_amount 
base_extra_tax_amount 
subtotal 
base_subtotal 
subtotal_with_discount 
base_subtotal_with_discount 
grand_total 
base_grand_total 
quote_address 
is_multi_shipping 
total_qty 
base_virtual_amount 
virtual_amount 
subtotal_incl_tax 
base_subtotal_incl_tax 
applied_rule_ids 
shipping_tax_amount 
base_shipping_tax_amount 
applied_taxes_reset 
free_shipping 
free_method_weight 
region_id 
customer_class_id 
store 
parent_id 
product_class_id 
shipping_amount 
base_shipping_amount 
shipping_incl_tax 
base_shipping_incl_tax 
shipping_taxable 
base_shipping_taxable 
is_shipping_incl_tax 
base_cod_fee 
cod_fee 
cod_tax_amount 
base_cod_tax_amount 
method 
prev_quote_customer_group_id 
discount_amount 
base_discount_amount 
base_calculation_price 
calculation_price 
base_original_price 
original_custom_price 
rate 
row_total 
base_row_total 
coupon_code 
no_discount 
coupon_type 
uses_per_customer 
is_primary 
uses_per_coupon 
use_auto_generation 
usage_limit 
operator_option 
operator_by_input_type 
value_option 
operator_options 
type 
aggregator 
value 
actions 
aggregator_option 
aggregator_options 
rule 
conditions_serialized 
attribute_option 
attribute 
operator 
is_value_parsed 
value_parsed 
actions_serialized 
simple_free_shipping 
stop_rules_processing 
tax_percent 
custom_price 
base_price 
price_incl_tax 
base_price_incl_tax 
row_total_incl_tax 
base_row_total_incl_tax 
taxable_amount 
base_taxable_amount 
is_price_incl_tax 
rounding_deltas 
weee_tax_applied 
base_weee_tax_disposition 
weee_tax_disposition 
base_weee_tax_row_disposition 
weee_tax_row_disposition 
base_weee_tax_applied_amount 
base_weee_tax_applied_row_amount 
weee_tax_applied_amount 
weee_tax_applied_row_amount 
row_weight 
all_items 
dest_country_id 
dest_region_id 
dest_region_code 
dest_street 
city 
dest_city 
dest_postcode 
package_value 
package_value_with_discount 
package_weight 
package_qty 
package_physical_value 
base_currency 
package_currency 
limit_carrier 
orig 
path 
cipher 
mode 
handler 
init_vector 
active_flag 
error 
weight_type 
full_row_weight 
carrier_code 
zip 
ups_pickup 
ups_container 
ups_dest_type 
udropship_calculate_rates 
calculate_rates_by_group_flag 
requests 
website_ids 
system_methods 

这是什么? 我在想这会是一个类似于法师注册表的js下划线窗口? 如何使用? 它是如何填充的?

回答

10

简版:如果你不知道这是什么,你不需要使用它。

长版本如下。

这与underscoe.js完全没有关系。在它是一个JavaScript框架之前,下划线是一个简单的ASCII字符_

在Magento的系统,大多数物体从基

Varien_Object 

类继承。该对象为Magento的对象提供了特殊功能。例如,在一个Magento对象中,你不需要定义setter和getters。你可以做这样的事情

$object = new SomeObject; //which inherits form Varien_Object 

$object->setSomeValue('Our Value'); 

echo $object->getSomeValue('Our Value'); 
echo $object->getData('our_value'); 

$data = $object->getData(); 
echo $data['our_value']; 

在上面的例子中,没有名为setSomeValue具体方法。 Magento神奇地知道我们只是想设置一个数据属性。这是在PHP魔术方法__call

#File: lib/Varien/Object.php 
public function __call($method, $args) 
{ 
    ... 
} 

实现。当你调用setSomeValue,Magento的设置对象的命名some_value数据阵列上的一个键。也就是说,它需要将骆驼套装SomeValue转换成非骆驼套装some_value。你可以看到,在魔术__call执行此

#File: lib/Varien/Object.php 
public function __call($method, $args) 
{ 
    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; 
} 

_underscore方法将字符串SomeValue,并将其与下面的行

#File: lib/Varien/Object.php 
$result = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name)); 

在某些时候转换为some_value,无论是通过分析或直觉,Magento的开发者每次都意识到调用strToLowerpreg_replace数据被获取或设置在对象上意味着性能瓶颈。为了解决这个问题,他们介绍了_underscoeCache。这是一个数组,并在Varien_Object类的静态属性

#File: lib/Varien/Object.php 
/** 
* Setter/Getter underscore transformation cache 
* 
* @var array 
*/ 
protected static $_underscoreCache = array(); 

如果你看看整个_underscore方法,你可以看到它是如何使用

protected function _underscore($name) 
{ 
    if (isset(self::$_underscoreCache[$name])) { 
     return self::$_underscoreCache[$name]; 
    } 
    #Varien_Profiler::start('underscore'); 
    $result = strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name)); 
    #Varien_Profiler::stop('underscore'); 
    self::$_underscoreCache[$name] = $result; 
    return $result; 
} 

也就是说,Magento的仍然会执行strToLowerpreg_replace转换,但它只会对每个唯一字符串执行一次。它的完成键/值(SomeValuesome_value)后放置在_underscoreCache

self::$_underscoreCache[$name] = $result; 

这样一来,下一次调用该方法,则返回从缓存中值,而不是

if (isset(self::$_underscoreCache[$name])) { 
    return self::$_underscoreCache[$name]; 
} 

这避免了对通常引用的变量的strToLowerpreg_replace的调用。

+0

有时候,我开始读的了var像这样回答,我知道作者将会是谁而不去寻找。 – siliconrockstar

0

完美的澄清,但我可以从前端tamplating系统采取变种。 即使我看到它在$ _underscoreCache

我使用:

Mage::getSingleton('core/session')->getCartWasUpdated(); 

,我想利用这在 cartControlller.php设置

$this->_getSession()->setCartWasUpdated(true); 
+0

听起来像你想问一个新的问题 - 但我的猜测是_getSession返回一个不同的会话对象。即不是“核心/会话”的那个 –

+0

是的! :)谢谢你,我看到它后!它使用checkout/session,然后我发现我的var:D – aimiliano