Magento2 数据库操作

直接操作数据库

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('employee');

Select Data from table
$sql = "Select * FROM " . $tableName;
$result = $connection->fetchRow($sql); //get one info,返回二维数组
$result = $connection->fetchAll($sql);   //get all info

Delete Data from table
$sql = "Delete FROM " . $tableName." Where emp_id = 10";
$connection->query($sql);

Insert Data into table
$sql = "Insert Into " . $tableName . " (emp_id, emp_name, emp_code, emp_salary) Values ('','XYZ','ABD20','50000')";
$connection->query($sql);

Update Data into table
$sql = "Update " . $tableName . "Set emp_salary = 20000 where emp_id = 12";
$connection->query($sql);

 

addFieldToFilter

protected  $_productCollectionFactory ;
public  function __construct (
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory  $productFactory
     )  {
         $this ->_productCollectionFactory  =  $productFactory ;
     }
     public  function getProductCollection ( )
     {
       return    $this ->_productCollectionFactory -> create ( ) -> addAttributeToSelect ( '*' ) -> addFieldToFilter ( 'sku’,'test ');
     
     }
Equal: eq

Now we use equal to filter production collection.

$this ->_productCollectionFactory -> addFieldToFilter ( 'status' ,  array ( 'eq'  =>  1 ) ) ;  // Using the operator
$this ->_productCollectionFactory -> addFieldToFilter ( 'status' ,  1 ) ;  // Without using the operator
Not Equals – neq

Now we use not equal to filter production collection.

$this ->_productCollectionFactory -> addFieldToFilter ( 'sku' ,  array ( 'neq'  =>  'test-product' ) ) ;
Like – like

Now we use Like to filter production collection.

$this ->_productCollectionFactory -> addFieldToFilter ( 'sku' ,  array ( 'like'  =>  'UX%' ) ) ;
Not Like – nlike

Now we use not like to filter production collection.

$this ->_productCollectionFactory -> addFieldToFilter ( 'sku' ,  array ( 'nlike'  =>  'err-prod%' ) ) ;
In – in

Now we use In to filter production collection.

$this ->_productCollectionFactory -> addFieldToFilter ( 'entity_id' ,  array ( 'in'  =>  array ( 1 , 4 , 98 ) ) ) ;
Not In – nin

Now we use not In to filter production collection.

$this ->_productCollectionFactory -> addFieldToFilter ( 'entity_id' ,  array ( 'nin'  =>  array ( 1 , 4 , 98 ) ) ) ;
NULL – null

Now we use null to filter production collection.

$this ->_productCollectionFactory -> addFieldToFilter ( 'description' ,  array ( 'null'  =>  true ) ) ;
Not NULL – notnull

Now we use not null to filter production collection.

$this ->_productCollectionFactory -> addFieldToFilter ( 'description' ,  array ( 'notnull'  =>  true ) ) ;
Greater Than – gt

Now we use greater than to filter production collection.

$this ->_productCollectionFactory -> addFieldToFilter ( 'entity_id' ,  array ( 'gt'  =>  5 ) ) ;
Less Than – lt

Now we use less than to filter production collection.

$this ->_productCollectionFactory -> addFieldToFilter ( 'entity_id' ,  array ( 'lt'  =>  5 ) ) ;
Greater Than or Equals To- gteq

Now we use greater than to filter production collection.

$this ->_productCollectionFactory -> addFieldToFilter ( 'entity_id' ,  array ( 'gteq'  =>  5 ) ) ;
Less Than or Equals To – lteq

Now we use less than equal to filter production collection.

$this ->_productCollectionFactory -> addFieldToFilter ( 'entity_id' ,  array ( 'lteq'  =>  5 ) ) ;

 

打印SQL语句

$collection = $this->_order->getCollection()->addFieldToFilter('created_at',array('like' => '2018-10%'));
echo $collection->getSelect()->__toString(); //打印sql语句

打印getCollection结果数量

$collection = $this->_order->getCollection()->addFieldToFilter('created_at',array('like' => '2018-10%'));
echo $collection->count(); //打印sql语句

 

 

转载于:https://my.oschina.net/ganfanghua/blog/3029100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值