2

I created a custom attribute for customer and would like to filter my collection through it.

My UpgradeData:

if (version_compare($context->getVersion(), '1.0.6', '<')) {

    $attributesInfo = [
        'celular'  => [
        'label'    => 'Celular',
        'type'     => 'varchar', // can be static too
        'input'    => 'text',
        'unique' => true,
        'position' => 90,
        'visible'  => true,
        'required' => true,
        'system'   => 0, // Important otherwise attribute is not seen as custom attribute
    ],
];

    foreach ($attributesInfo as $attributeCode => $attributeParams) {
        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, $attributeCode, $attributeParams);
    }

    foreach ($attributesInfo as $attributeCode => $attributeParams) {

        $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $attributeCode);
        $attribute->setData(
            'used_in_forms',
            ['adminhtml_customer']
        );
        $attribute->save();
    }
}

My Collection:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection'); 
        $customer = $customerObj->addAttributeToSelect('*')
        ->addAttributeToFilter('celular',$username)
        ->load()->getData();

Even though I am attaching a value to this field, no customer is found, what should I do?

--EDIT!

Images database: eav_attribute

customer_entity_varchar

5 Answers 5

5

Check the value of $username

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection'); 
$customer = $customerObj->addAttributeToSelect('*')
        ->addAttributeToFilter('celular',$username)
        ->load();
echo $customer->getSelect()->__toString();die;

Please check query string.

1
  • I have checked and all information is correct in their respective tables. I edited the question with the images for you to check. Do you have any other tips? Thank you! Jul 22, 2019 at 18:02
1

Please don't use the ObjectManager, use the constructor:

<?php 
    namespace Vendor\Module\Block; 
    class ClassName {    
       protected $_productCollectionFactory;
        
       public function __construct(
           \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory 
            $productCollectionFactory     
       )
       {    
           $this->_productCollectionFactory = $productCollectionFactory;    
       }
    
       public function getProductCollection()
       {
         $collection = $this->_productCollectionFactory->create();
         $collection->addAttributeToSelect('*');
         $collection->addAttributeToFilter('celular', ['eq' => $username ]);
         return $collection;
      } 

}

0

You can try this. It worked for me couple of months ago.

$replydb = $objectManager->get('\Magento\Module\Model\Yourmodel')->getCollection();
$replydb->addFieldToFilter('status', 'Approved');
$replycoll = $replydb->getData();
1
  • I already tested it and it did not work too, I believe that due to being using the custom attribute of the customer. Do you have any suggestion? Thank you! Jul 22, 2019 at 18:04
0

Try This :-

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$customerObj = $objectManager->create('Magento\Customer\Model\Customer'); 

$customerData = $customerObj->addAttributeToSelect('*')
                ->addAttributeToFilter('celular',$username);

foreach($customerData as $customer)
{
    $customer->getData();
}
-1

Please check query.

Check the value of $username

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection'); 
$customer = $customerObj->addAttributeToSelect('*')
        ->addAttributeToFilter('celular',$username)
        ->load();
echo $customer->getSelect()->__toString();die;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.