I have the following code where I need to check whether the attribute in question is a customer attribute or customer address attribute. How do I check that?
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 return attribute codes like street if I select Street Address, gender if I select Gender and so on. Now what condition should be put in order to know whether a given attribute is customer or address attribute.
// Prepare Collection addition to store custom fields
foreach ($this->custom_columns as $col)
{
//Some Condition if its a Customer attribute
collection->addAttributeToSelect($col);
//else some condition if its a Customer address attribute
$collection->joinAttribute($col, "customer_address/$col", 'default_billing', null, 'left');
}
$this->setCollection($collection);
return parent::_prepareCollection();
}
I just want to know what those conditions would be. Hope this is a little clearer
You can use magic
has*()calls with each class extendingVarien_Objectto check whether a given property does, or does not exist.Additionally,
Varien_Objectoffers a non-magichasData('property_name')method.The
hasData()method basically does the same thing, just indirectly – i.e. by passing the property name as argument to the method, instead of using it as camel-cased part of the methods name.Mage_Customer_Model_CustomerandMage_Customer_Model_Addressactually do extendVarien_Object, so you could callhasData('street')on an object instance to check for the existence of such property and use the result for yourif/elsescenario.You could also magically call
hasStreet(), but in your case thehasData()variant will definitely serve better (as you’re looping thru an array with variable property names).Note that
hasData()can only help you distinguish unique property names. Of course you cannot usehasData()to distinguish properties of the same name existing in both classes (e.g.'entity_id').