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

3 Answers 3

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 Commented Jun 4, 2019 at 8:27
  • what error throw.right now?
    – Amit Bera
    Commented Jun 4, 2019 at 8:47
0

I had to use use Magento\Framework\DB\Select; in my module with Zend_Db_Expr

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.