You can get the Auto Increment Field from a database table using Magento 2 as Best coding practice way.
You need to use getAutoIncrementField() for getting the auto-increment field from the table.
Output: Returns field name if an auto-increment field is available otherwise return as false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php namespace Path\To\Class; use Magento\Framework\App\ResourceConnection; class AutoIncrementField { const ORDER_TABLE = 'sales_order'; /** * @var ResourceConnection */ private $resourceConnection; public function __construct( ResourceConnection $resourceConnection ) { $this->resourceConnection = $resourceConnection; } /** * get AutoIncremntFieldName using Query * * @return string|bool */ public function getAutoIncremntFieldName() { $connection = $this->resourceConnection->getConnection(); $tableName = $connection->getTableName(self::ORDER_TABLE); $getFieldName = $connection->getAutoIncrementField($tableName); return $getFieldName; } } |
You can call $this->getAutoIncremntFieldName() function to check sales_order table with auto_increment field name.
Output:
entity_id