0

I am getting warning as

"Direct object instantiation (object of \Zend_Db_Expr) is discouraged in Magento2"

when I did MEQP2.

Please anyone can help me to resolve this warning.

I am getting issue in below line.

$Qry = $connection->select() ->from($this->resourceConfig->getTable($tableName), [new \Zend_Db_Expr('COUNT(*)')]) ->where('store=?', $storeId);

Thanks in Advance

2 Answers 2

3

Not sure about this, but I think you can use factories for the zend classes also.

Try this:

Have this member in your class:

private $zendExprFactory;

And add this in your constructor:

public function __construct(
    ....
    \Zend_Db_ExprFactory $zendExprFactory,
    ....
) {
   ....
   $this->zendExprFactory = $zendExprFactory;
   ....
}

Now (If I'm right) you should be able to create the zend_db_expr instance like this:

$expr = $this->zendExprFactory->create(['expression' => 'COUNT(*)']); 

and your code should look like this:

$Qry = $connection->select()->from(
     $this->resourceConfig->getTable($tableName), 
     [$expr]
 )->where('store=?', $storeId);
2

Instead of using Zend_Db_Expr, you can use \Magento\Framework\DB\Sql\Expression

Like

use Magento\Framework\DB\Sql\Expression;

class Yourclass
{

public function test()
{
....
$Qry = $connection->select() ->from($this->resourceConfig->getTable($tableName), [new Expression('COUNT(*)')]) ->where('store=?', $storeId);
....
}
}
2
  • Hi @Amit Bera,still warning is showing Jun 4, 2019 at 8:27
  • what error throw.right now?
    – Amit Bera
    Jun 4, 2019 at 8:47

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.