【问题标题】:Check whether an attribute is customer attribute or customer address attribute检查属性是客户属性还是客户地址属性
【发布时间】:2011-12-16 15:21:37
【问题描述】:

我有以下代码需要检查相关属性是客户属性还是客户地址属性。如何检查?

private $custom_columns = array();

public function __construct() 
{
    parent::__construct();
    $this->setId('customerGrid');
    $this->setUseAjax(true);
    $this->setDefaultSort('email');
    $this->setDefaultLimit('200');
    $this->setSaveParametersInSession(true);
    $attributeIds = Mage::getStoreConfig('sectionname/group/field');
    $this->custom_columns = array($attributeIds); 
}

$attributeIds 如果我选择街道地址,则返回属性代码,如 street,如果我选择性别,则返回 gender 等等。现在应该设置什么条件才能知道给定属性是客户属性还是地址属性。

// 准备集合添加到存储自定义字段
foreach ($this->custom_columns as $col)
{
   //如果它是客户属性的一些条件
   集合->addAttributeToSelect($col);
   //如果它是客户地址属性,则为其他条件
   $collection->joinAttribute($col, "customer_address/$col", 'default_billing', null, 'left');
 }
 $this->setCollection($collection);
 返回父级::_prepareCollection();
}

我只想知道那些条件会是什么。希望这更清楚一点

【问题讨论】:

  • 很难理解你在问什么。请尽量说明您的问题是什么,并发布您已经尝试过的代码,以及您收到的任何错误消息。

标签: php zend-framework magento


【解决方案1】:

您可以使用 magic has*() 调用以及扩展 Varien_Object 的每个类来检查给定属性是否存在。

另外,Varien_Object 提供了一个非魔法的hasData('property_name') 方法。

hasData() 方法基本上做同样的事情,只是间接地 - 即通过将属性名称作为参数传递给方法,而不是将其用作方法名称的驼峰式部分。

Mage_Customer_Model_CustomerMage_Customer_Model_Address 实际上确实扩展了Varien_Object,因此您可以在对象实例上调用hasData('street') 来检查此类属性的存在并将结果用于您的if/else 场景。

您也可以神奇地调用hasStreet(),但在您的情况下,hasData() 变体肯定会更好地服务(因为您正在循环通过具有变量属性名称的数组)。

请注意,hasData() 只能帮助您区分唯一属性名称。当然,您不能使用hasData() 来区分两个类中存在的同名属性(例如'entity_id')。

【讨论】:

  • 感谢您的详细解释。这有助于我更好地理解事情。所以我可以使用像 if(Mage::getModel('customer/entity_attribute_collection)->hasData($col)){do something...}else{something else...}跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-24
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多