0

I have no idea why my Subscriber Collection Factory is not working. Below is my code. Wish someone can help me to fix my problem. I believe something wrong with my setup of CollectionFactory

use Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory;

public function __construct(
        CollectionFactory $subscriberCollection
    ) {
        $this->subscriberCollection = $subscriberCollection;
    }

public function  getSubscriberStatus()
    {

        $subscribers = $this->subscriberCollection->create();
        $subscribers->addAttributeToSelect("*");

/************** don't know why code not keep run after above *****************/

        $currtDate = date("Y-m-d H:i:s"); // Y-m-d h:i:s
        $newDate = strtotime('-1 MONTH', strtotime($currtDate));
        $newDate = date('Y-m-d H:i:s', $newDate);

        $subscribers->addFieldToFilter('change_status_at', ['gteq' => $newDate]);
        $subscribers->addFieldToFilter('change_status_at', ['lteq' => $currtDate]);

        $subscribers->load();

        return $subscribers;

    }

my query

select * from newsletter_subscriber where change_status_at >=  (NOW() - INTERVAL 1 MONTH);

object manager method

public function  getSubscriberStatus()
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();
    $query = "select * from newsletter_subscriber where change_status_at >=  (NOW() - INTERVAL 1 MONTH);";
    $result = $connection->fetchAll($query);

    return $result;
}

update:

use Exception;
use Magento\Email\Model\BackendTemplate;
use Magento\Framework\App\Area;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\HTTP\Client\Curl;
use Magento\Framework\Mail\Template\TransportBuilder;
use Psr\Log\LoggerInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\CustomerFactory;
use Zend_Db_Expr;


/**
 * Class Check
 *
 * @package 
 */
class Check
{
    /**
     * @var SerializerInterface
     */
    private $serializer;



    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var TransportBuilder
     */
    private $transportBuilder;

    /**
     * @var BackendTemplate
     */
    private $emailTemplate;

    /**
     * @var Curl
     */
    protected $curl;

    /**
     * @var CustomerRepositoryInterface
     */
    private $customerRepo;


    protected $subscriberCollection;


    /**
     * Check constructor.
     *
     * @param SerializerInterface $serializer
     * @param CollectionFactory   $collectionFactory
     * @param LoggerInterface     $logger
     * @param TransportBuilder    $transportBuilder
     * @param BackendTemplate     $emailTemplate
     * @param CustomerRepositoryInterface $customerRepo
     * @param Curl $curl
     */
    public function __construct(
        SerializerInterface $serializer,
        //CollectionFactory $collectionFactory,
        LoggerInterface $logger,
        TransportBuilder $transportBuilder,
        BackendTemplate $emailTemplate,
        CustomerRepositoryInterface $customerRepo,
        CustomerFactory $customerFactory,
        \Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollectionFactory,
        Curl $curl
    ) {
        $this->serializer = $serializer;
        //  $this->collectionFactory = $collectionFactory;
        $this->logger = $logger;
        $this->transportBuilder = $transportBuilder;
        $this->emailTemplate = $emailTemplate;
        $this->customerRepo = $customerRepo;
        $this->customerFactory = $customerFactory;
        $this->_subscribersCollection = $subscriberCollectionFactory->create();
        $this->curl = $curl;
    }


  
    public function  getSubscriberStatus()
    {

        $subscribers = $this->_subscribersCollection;
        $subscribers->addAttributeToSelect("*");

        $currtDate = date("Y-m-d H:i:s"); // Y-m-d h:i:s
        $newDate = strtotime('-10 day', strtotime($currtDate));
        $newDate = date('Y-m-d H:i:s', $newDate);

        $subscribers->addFieldToFilter('change_status_at', ['gteq' => $newDate]);
        $subscribers->addFieldToFilter('change_status_at', ['lteq' => $currtDate]);

        $subscribers->load();

        return $subscribers;

    }
2
  • what error are you facing ? – Hamendra Sunthwal Dec 24 '20 at 8:09
  • the function not went thru, just like being ignore. I did try to echo something and die(); not print anything at all and just pass thru – Paul Fan Dec 24 '20 at 8:20
0

Try this :-

protected $subscriberCollection;

public function __construct(
    \Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollectiona
) {
    $this->subscriberCollection = $subscriberCollection;
}

public function getSubscriberCollection() 
{
    $subscriberCollection =$this->subscriberCollection->create();
    foreach($subscriberCollection as $subscriber)
    {
        print_r($subscriber->getData());
    }
}
4
  • I did try this, still not sure, seems something wrong with the "subscriberCollection" it cannot call back original resources. Can you please help to check again on my above setting is correct or not? thanks – Paul Fan Dec 24 '20 at 9:47
  • getting any error ?? – Rk Rathod Dec 24 '20 at 9:48
  • the function cannot load, it didn't get the query result. I just update on my question. Can you please help to take a look, I am getting confuse with my head setup. – Paul Fan Dec 24 '20 at 9:58
  • @PaulFan first check your block or controller action is working or not... – madani.codes Dec 24 '20 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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