Magento 2: addAttributeToFilter not returning any results












1














When I use addAttributeToFilter on MagentoCatalogModelResourceModelProductCollectionFactory, with a custom attribute, I always get 0 results.



use MagentoCatalogModelResourceModelProductCollectionFactory;
use MagentoCatalogModelProductRepository;
use MagentoFrameworkApiSearchCriteriaBuilder;

...

$collection = $this->productCollection->create()
->addAttributeToSelect(array('custom_attribute','sku'))
->addAttributeToFilter('custom_attribute',['lt' => 1]);
}

echo count($collection); //Returns 0


I also tried to use the search criteria object as Konrad Siamro sugested:



$search_criteria = $this->searchCriteriaBuilder
->addFilter('custom_attribute', 1, 'lt')
->create();
$collection = $this->productRepository->getList($search_criteria);

echo $collection->getTotalCount(); //returns 0


But the results are the same. It works fine if I filter on a SKU, but my custom attribute returns nothing. I also tried adding the attribute to the flat product database and re indexing.



Update: Added code I used to create the attribute:



$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'custom_attribute',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'frontend_class' => 'validate-digits',
'label' => 'Custom Attribute',
'input' => 'text',
'class' => 'class',
'source' => '',
'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);









share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • U need to 1st check catalog_product_entity then respectively check on type table int, varchar & join
    – Ankit Shah
    Mar 7 '17 at 10:03
















1














When I use addAttributeToFilter on MagentoCatalogModelResourceModelProductCollectionFactory, with a custom attribute, I always get 0 results.



use MagentoCatalogModelResourceModelProductCollectionFactory;
use MagentoCatalogModelProductRepository;
use MagentoFrameworkApiSearchCriteriaBuilder;

...

$collection = $this->productCollection->create()
->addAttributeToSelect(array('custom_attribute','sku'))
->addAttributeToFilter('custom_attribute',['lt' => 1]);
}

echo count($collection); //Returns 0


I also tried to use the search criteria object as Konrad Siamro sugested:



$search_criteria = $this->searchCriteriaBuilder
->addFilter('custom_attribute', 1, 'lt')
->create();
$collection = $this->productRepository->getList($search_criteria);

echo $collection->getTotalCount(); //returns 0


But the results are the same. It works fine if I filter on a SKU, but my custom attribute returns nothing. I also tried adding the attribute to the flat product database and re indexing.



Update: Added code I used to create the attribute:



$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'custom_attribute',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'frontend_class' => 'validate-digits',
'label' => 'Custom Attribute',
'input' => 'text',
'class' => 'class',
'source' => '',
'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);









share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • U need to 1st check catalog_product_entity then respectively check on type table int, varchar & join
    – Ankit Shah
    Mar 7 '17 at 10:03














1












1








1







When I use addAttributeToFilter on MagentoCatalogModelResourceModelProductCollectionFactory, with a custom attribute, I always get 0 results.



use MagentoCatalogModelResourceModelProductCollectionFactory;
use MagentoCatalogModelProductRepository;
use MagentoFrameworkApiSearchCriteriaBuilder;

...

$collection = $this->productCollection->create()
->addAttributeToSelect(array('custom_attribute','sku'))
->addAttributeToFilter('custom_attribute',['lt' => 1]);
}

echo count($collection); //Returns 0


I also tried to use the search criteria object as Konrad Siamro sugested:



$search_criteria = $this->searchCriteriaBuilder
->addFilter('custom_attribute', 1, 'lt')
->create();
$collection = $this->productRepository->getList($search_criteria);

echo $collection->getTotalCount(); //returns 0


But the results are the same. It works fine if I filter on a SKU, but my custom attribute returns nothing. I also tried adding the attribute to the flat product database and re indexing.



Update: Added code I used to create the attribute:



$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'custom_attribute',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'frontend_class' => 'validate-digits',
'label' => 'Custom Attribute',
'input' => 'text',
'class' => 'class',
'source' => '',
'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);









share|improve this question















When I use addAttributeToFilter on MagentoCatalogModelResourceModelProductCollectionFactory, with a custom attribute, I always get 0 results.



use MagentoCatalogModelResourceModelProductCollectionFactory;
use MagentoCatalogModelProductRepository;
use MagentoFrameworkApiSearchCriteriaBuilder;

...

$collection = $this->productCollection->create()
->addAttributeToSelect(array('custom_attribute','sku'))
->addAttributeToFilter('custom_attribute',['lt' => 1]);
}

echo count($collection); //Returns 0


I also tried to use the search criteria object as Konrad Siamro sugested:



$search_criteria = $this->searchCriteriaBuilder
->addFilter('custom_attribute', 1, 'lt')
->create();
$collection = $this->productRepository->getList($search_criteria);

echo $collection->getTotalCount(); //returns 0


But the results are the same. It works fine if I filter on a SKU, but my custom attribute returns nothing. I also tried adding the attribute to the flat product database and re indexing.



Update: Added code I used to create the attribute:



$eavSetup->addAttribute(
MagentoCatalogModelProduct::ENTITY,
'custom_attribute',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'frontend_class' => 'validate-digits',
'label' => 'Custom Attribute',
'input' => 'text',
'class' => 'class',
'source' => '',
'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);






magento2 product-collection eav






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 '17 at 15:49

























asked Mar 7 '17 at 9:58









Dennis van Schaik

4941318




4941318





bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • U need to 1st check catalog_product_entity then respectively check on type table int, varchar & join
    – Ankit Shah
    Mar 7 '17 at 10:03


















  • U need to 1st check catalog_product_entity then respectively check on type table int, varchar & join
    – Ankit Shah
    Mar 7 '17 at 10:03
















U need to 1st check catalog_product_entity then respectively check on type table int, varchar & join
– Ankit Shah
Mar 7 '17 at 10:03




U need to 1st check catalog_product_entity then respectively check on type table int, varchar & join
– Ankit Shah
Mar 7 '17 at 10:03










2 Answers
2






active

oldest

votes


















0














Use search criteria for filtering collections.

Go to http://alanstorm.com/magento_2_understanding_object_repositories/

and look for search criteria.






share|improve this answer





















  • I changed my code to use search criteria, results are exactly the same. (searching on SKU gives hits, searching on my custom attribute returns nothing.)
    – Dennis van Schaik
    Mar 7 '17 at 10:45










  • can You edit your post and show your code? :)
    – Konrad Siamro
    Mar 7 '17 at 10:55










  • I just edited my post.
    – Dennis van Schaik
    Mar 7 '17 at 11:23










  • $collection->getItems(); also return null? Where are you using his code? Meaby custom attribute have some options set incorrect?
    – Konrad Siamro
    Mar 7 '17 at 13:29










  • I added the code used to create the attribute as well. Can you check it for me?
    – Dennis van Schaik
    Mar 7 '17 at 14:57



















0














Problem is that when you create your custom_attribute its still empty:



'user_defined' => false,
'default' => '',
'searchable' => false,


your default value is empty, but you check if value is lower than 1



->addFilter('custom_attribute', 1, 'lt')


I tested it and result without setting any value for the custom_attribute gave as result 0 items which is the correct result. I changed for example the filter to



->addFilter('custom_attribute', 1, 'eq')


and set the custom_attribue to 1 in one Product in Admin. Result is now one.



Problem is lower than one is not "empty"



Tried it with the searchCriteriaBuilder and with addAttributeToFilter works both with changed Filters and setting the custom_attribute for example to 1.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f163209%2fmagento-2-addattributetofilter-not-returning-any-results%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Use search criteria for filtering collections.

    Go to http://alanstorm.com/magento_2_understanding_object_repositories/

    and look for search criteria.






    share|improve this answer





















    • I changed my code to use search criteria, results are exactly the same. (searching on SKU gives hits, searching on my custom attribute returns nothing.)
      – Dennis van Schaik
      Mar 7 '17 at 10:45










    • can You edit your post and show your code? :)
      – Konrad Siamro
      Mar 7 '17 at 10:55










    • I just edited my post.
      – Dennis van Schaik
      Mar 7 '17 at 11:23










    • $collection->getItems(); also return null? Where are you using his code? Meaby custom attribute have some options set incorrect?
      – Konrad Siamro
      Mar 7 '17 at 13:29










    • I added the code used to create the attribute as well. Can you check it for me?
      – Dennis van Schaik
      Mar 7 '17 at 14:57
















    0














    Use search criteria for filtering collections.

    Go to http://alanstorm.com/magento_2_understanding_object_repositories/

    and look for search criteria.






    share|improve this answer





















    • I changed my code to use search criteria, results are exactly the same. (searching on SKU gives hits, searching on my custom attribute returns nothing.)
      – Dennis van Schaik
      Mar 7 '17 at 10:45










    • can You edit your post and show your code? :)
      – Konrad Siamro
      Mar 7 '17 at 10:55










    • I just edited my post.
      – Dennis van Schaik
      Mar 7 '17 at 11:23










    • $collection->getItems(); also return null? Where are you using his code? Meaby custom attribute have some options set incorrect?
      – Konrad Siamro
      Mar 7 '17 at 13:29










    • I added the code used to create the attribute as well. Can you check it for me?
      – Dennis van Schaik
      Mar 7 '17 at 14:57














    0












    0








    0






    Use search criteria for filtering collections.

    Go to http://alanstorm.com/magento_2_understanding_object_repositories/

    and look for search criteria.






    share|improve this answer












    Use search criteria for filtering collections.

    Go to http://alanstorm.com/magento_2_understanding_object_repositories/

    and look for search criteria.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 7 '17 at 10:24









    Konrad Siamro

    905521




    905521












    • I changed my code to use search criteria, results are exactly the same. (searching on SKU gives hits, searching on my custom attribute returns nothing.)
      – Dennis van Schaik
      Mar 7 '17 at 10:45










    • can You edit your post and show your code? :)
      – Konrad Siamro
      Mar 7 '17 at 10:55










    • I just edited my post.
      – Dennis van Schaik
      Mar 7 '17 at 11:23










    • $collection->getItems(); also return null? Where are you using his code? Meaby custom attribute have some options set incorrect?
      – Konrad Siamro
      Mar 7 '17 at 13:29










    • I added the code used to create the attribute as well. Can you check it for me?
      – Dennis van Schaik
      Mar 7 '17 at 14:57


















    • I changed my code to use search criteria, results are exactly the same. (searching on SKU gives hits, searching on my custom attribute returns nothing.)
      – Dennis van Schaik
      Mar 7 '17 at 10:45










    • can You edit your post and show your code? :)
      – Konrad Siamro
      Mar 7 '17 at 10:55










    • I just edited my post.
      – Dennis van Schaik
      Mar 7 '17 at 11:23










    • $collection->getItems(); also return null? Where are you using his code? Meaby custom attribute have some options set incorrect?
      – Konrad Siamro
      Mar 7 '17 at 13:29










    • I added the code used to create the attribute as well. Can you check it for me?
      – Dennis van Schaik
      Mar 7 '17 at 14:57
















    I changed my code to use search criteria, results are exactly the same. (searching on SKU gives hits, searching on my custom attribute returns nothing.)
    – Dennis van Schaik
    Mar 7 '17 at 10:45




    I changed my code to use search criteria, results are exactly the same. (searching on SKU gives hits, searching on my custom attribute returns nothing.)
    – Dennis van Schaik
    Mar 7 '17 at 10:45












    can You edit your post and show your code? :)
    – Konrad Siamro
    Mar 7 '17 at 10:55




    can You edit your post and show your code? :)
    – Konrad Siamro
    Mar 7 '17 at 10:55












    I just edited my post.
    – Dennis van Schaik
    Mar 7 '17 at 11:23




    I just edited my post.
    – Dennis van Schaik
    Mar 7 '17 at 11:23












    $collection->getItems(); also return null? Where are you using his code? Meaby custom attribute have some options set incorrect?
    – Konrad Siamro
    Mar 7 '17 at 13:29




    $collection->getItems(); also return null? Where are you using his code? Meaby custom attribute have some options set incorrect?
    – Konrad Siamro
    Mar 7 '17 at 13:29












    I added the code used to create the attribute as well. Can you check it for me?
    – Dennis van Schaik
    Mar 7 '17 at 14:57




    I added the code used to create the attribute as well. Can you check it for me?
    – Dennis van Schaik
    Mar 7 '17 at 14:57













    0














    Problem is that when you create your custom_attribute its still empty:



    'user_defined' => false,
    'default' => '',
    'searchable' => false,


    your default value is empty, but you check if value is lower than 1



    ->addFilter('custom_attribute', 1, 'lt')


    I tested it and result without setting any value for the custom_attribute gave as result 0 items which is the correct result. I changed for example the filter to



    ->addFilter('custom_attribute', 1, 'eq')


    and set the custom_attribue to 1 in one Product in Admin. Result is now one.



    Problem is lower than one is not "empty"



    Tried it with the searchCriteriaBuilder and with addAttributeToFilter works both with changed Filters and setting the custom_attribute for example to 1.






    share|improve this answer




























      0














      Problem is that when you create your custom_attribute its still empty:



      'user_defined' => false,
      'default' => '',
      'searchable' => false,


      your default value is empty, but you check if value is lower than 1



      ->addFilter('custom_attribute', 1, 'lt')


      I tested it and result without setting any value for the custom_attribute gave as result 0 items which is the correct result. I changed for example the filter to



      ->addFilter('custom_attribute', 1, 'eq')


      and set the custom_attribue to 1 in one Product in Admin. Result is now one.



      Problem is lower than one is not "empty"



      Tried it with the searchCriteriaBuilder and with addAttributeToFilter works both with changed Filters and setting the custom_attribute for example to 1.






      share|improve this answer


























        0












        0








        0






        Problem is that when you create your custom_attribute its still empty:



        'user_defined' => false,
        'default' => '',
        'searchable' => false,


        your default value is empty, but you check if value is lower than 1



        ->addFilter('custom_attribute', 1, 'lt')


        I tested it and result without setting any value for the custom_attribute gave as result 0 items which is the correct result. I changed for example the filter to



        ->addFilter('custom_attribute', 1, 'eq')


        and set the custom_attribue to 1 in one Product in Admin. Result is now one.



        Problem is lower than one is not "empty"



        Tried it with the searchCriteriaBuilder and with addAttributeToFilter works both with changed Filters and setting the custom_attribute for example to 1.






        share|improve this answer














        Problem is that when you create your custom_attribute its still empty:



        'user_defined' => false,
        'default' => '',
        'searchable' => false,


        your default value is empty, but you check if value is lower than 1



        ->addFilter('custom_attribute', 1, 'lt')


        I tested it and result without setting any value for the custom_attribute gave as result 0 items which is the correct result. I changed for example the filter to



        ->addFilter('custom_attribute', 1, 'eq')


        and set the custom_attribue to 1 in one Product in Admin. Result is now one.



        Problem is lower than one is not "empty"



        Tried it with the searchCriteriaBuilder and with addAttributeToFilter works both with changed Filters and setting the custom_attribute for example to 1.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 8 '17 at 3:03

























        answered Mar 8 '17 at 2:51









        Samir Shaban

        53125




        53125






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f163209%2fmagento-2-addattributetofilter-not-returning-any-results%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1300-talet

            1300-talet

            Display a custom attribute below product name in the front-end Magento 1.9.3.8