2012-02-21 72 views
0

.Hi,
我试图使属性taxvat唯一为每个客户。 (特别是对于我从后端创建的用户)。即没有重复。
就像电子邮件属性一样,如果电子邮件已被使用,则会通知用户使用其他电子邮件。
我试图从eav_attribute更改“is_unique”为“1”,但什么都没有发生。
你能帮我解决这个问题吗?
感谢Magento独特TaxVat atrribute为每个客户

回答

1

好吧,我找到了解决办法。在 查找文件/app/code/core/Mage/Customer/Model/Form.php(不要忘了,而不是覆盖。) “公共职能是validateData(数组$数据)”

添加代码的 的foreach($这 - >的getAttributes()作为$属性)

foreach ($this->getAttributes() as $attribute) { 
... 
... 
//## code to add 
     if ($attribute->getIsUnique()) { 
      $cid = $this->getEntity()->getData('entity_id'); //get current customer id 
      $cli = Mage::getModel('customer/customer') 
      ->getCollection() 
      ->addAttributeToFilter($attribute->getAttributeCode(), $data[$attribute->getAttributeCode()]); 
      //->addFieldToFilter('customer_id', array('neq' => $cid)); //exclude current user from results //###### not working...... 
      $flag=0; 
      foreach ($cli as $customer) { 
       $dataid=$customer->getId(); 
       if ($dataid != $cid) //if the value is from another customer_id 
        $flag |= 1; //we found a dup value 
      } 

      if ($flag) { 
       $label = $attribute->getStoreLabel(); 
       $errors = array_merge($errors, Mage::helper('customer')->__('"%s" already used!',$label)); 
      } 
     } 
//## End of code to add 
} 
0

我一直在寻找该溶液一段时间内。但我注意到你所引用的form.php文件是magento的1.5版本。在版本1.6中,文件是不同的...在1.6版本中放置代码的位置在哪里?谢谢。

我找到了该文件。它位于/app/code/core/Mage/Eav/Model/form.php。 但我把代码,并没有在这里工作......我不断尝试一个解决方案。

+0

<customer_save_before> <observers> <mymodule_customer_save_before> <class>mymodule/observer</class> <method>checkUniqueAttribute</method> </mymodule_customer_save_before> </observers> </customer_save_before> 

而在你的观察员 if($ result!== true){error = array_merge($ errors,$ result); } ... – karpa 2012-03-14 14:01:32

1

我修改了哪些Karpa写道,使其更好地

if ($attribute->getIsUnique()) { 
      $cid = $this->getEntity()->getData('entity_id'); //get current customer id 
      $cli = Mage::getModel('customer/customer') 
      ->getCollection() 
      ->addAttributeToFilter($attribute->getAttributeCode(), $data[$attribute->getAttributeCode()]) 
      ->addFieldToFilter('entity_id', array('neq' => $cid)); //exclude current user from results //###### not working...... 
      if (count($cli)>0) { 
       $label = $attribute->getStoreLabel(); 
       $errors = array_merge($errors, array(Mage::helper('customer')->__('"%s" already used!',$label))); 
      } 
0

您可以创建一个事件:后添加

/** 
    * Check customer attribute which must be unique 
    * 
    * @param Varien_Event_Observer $observer 
    * @return $this 
    * @@see customer_save_before 
    */ 
    public function checkUniqueAttribute(Varien_Event_Observer $observer) 
    { 
     /** @var Mage_Customer_Model_Customer $customer */ 
     $customer = $observer->getEvent()->getCustomer(); 
     foreach ($customer->getAttributes() as $attribute) { 
      if ($attribute->getIsUnique()) { 
       $collection = Mage::getModel('customer/customer') 
        ->getCollection() 
        ->addAttributeToFilter($attribute->getAttributeCode(), $customer->getData($attribute->getAttributeCode())) 
        ->addFieldToFilter('entity_id', array('neq' => $customer->getId())); 

       if ($collection->getSize()) { 
        Mage::throwException(sprintf(
         'The value %s for %s is already used', 
         $customer->getData($attribute->getAttributeCode()), 
         $attribute->getStoreLabel() 
        )); 
       } 
      } 
     } 

     return $this;