5

I face a problem with a collection. I have fresh products that need to be taken out from store 3 days before the expiration date. I created a command that is called by a cron. I've constructed it like that :

public function __construct(
CategoryCollection $categoryCollection, CategoryCollection $productCollection, Category $category, HelperData $helper, Utf8force $utf8helper, State $appState, ObjectManagerFactory $objectManagerFactory, \Magento\Framework\App\ResourceConnection $resource, $name = null
) {
    $this->_categoryCollection = $categoryCollection;
    $this->_productCollection = $productCollection;
    $this->_categoryModel = $category;
    $this->_moduleHelper = $helper;
    $this->_utf8helper = $utf8helper;
    $this->_state = $appState;
    $this->_resource = $resource;
    $params = $_SERVER;
    $params[StoreManager::PARAM_RUN_CODE] = 'admin';
    $params[StoreManager::PARAM_RUN_TYPE] = 'store';
    $this->objectManager = $objectManagerFactory->create($params);
    parent::__construct($name);
}

This class contains other function that are working but are not relevant for this question (import new products and categories).

The function is called in the execute. We did not set if we will deactivate or delete the product so yet it is called deactivate even if I call delete function.

private function _deactivateOldProducts() {
    $today = new \DateTime();
    $dlc = new \DateTime();
    $dlc->add(new \DateInterval('P3D'));
    $this->_productCollection->addAttributeToFilter('date_limite',
            array(
                'from' => $today->format('Y-m-d H:i:s'),
                'to' => $dlc->format('Y-m-d H:i:s'),
                'date' => true
            )
        );
    foreach ($this->_productCollection as $product) {
        $product->delete();
    }
    die();
}

I tried both things and they do not work. Whatever attribute I try, I get the exception : Invalid attribute name: *****

Thank you for any help.

2 Answers 2

3
+100

The problem is probably caused by the fact that you're using CategoryCollection in your constructor to declare your product collection.

You need to replace:

public function __construct(
CategoryCollection $categoryCollection, CategoryCollection $productCollection, Category $category, HelperData $helper, Utf8force $utf8helper, State $appState, ObjectManagerFactory $objectManagerFactory, \Magento\Framework\App\ResourceConnection $resource, $name = null
)

With:

public function __construct(
CategoryCollection $categoryCollection, ProductCollection $productCollection, Category $category, HelperData $helper, Utf8force $utf8helper, State $appState, ObjectManagerFactory $objectManagerFactory, \Magento\Framework\App\ResourceConnection $resource, $name = null
)

Explanations about the error you were getting

Well it's because the id attribute simply does not exist.

The following code is what gives you the error:

$this->_productCollection->addAttributeToFilter('id',
        array(
            'eq' => "1"
        )
    );

Theorically, that would basically gives you a collection of one product with the id = 1.

If that's what you wanted to do, you need to replace id with entity_id:

$this->_productCollection->addAttributeToFilter('entity_id',
        array(
            'eq' => "1"
        )
    );
3
  • whatever attribute I try I get the error. Oct 6, 2016 at 13:50
  • 1
    @ChristopheFerreboeuf see my edited answer ;) Oct 6, 2016 at 13:53
  • 1
    I understood it when checking entity id. A bad copy-paste, I'll aware you the bounty tomorrow. Thanks. Oct 6, 2016 at 13:55
1

There is no id attribute. Try entity_id instead.

1

Your Answer

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

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