I want to get the last inserted id to my block without adding new data into the table. I want to use this value to automatically filled in my phtml file. I tried getId and it's not working. I just need the last id. Can anyone help?
-
uhmm ok, but why? normally, you would set the ID column as autoincrement so you don't have to worry about 'what is the next ID to insert here'.– DianaJun 23, 2021 at 12:52
-
actually i want auto-increment in two colums and when i tried to create using dbschema,its saying that auto icrement is set only for one column. so thats why im going for another method:)– mageJun 23, 2021 at 15:46
-
i see. well, in that case, you either call last_insert_id() method (w3schools.com/Sql/func_mysql_last_insert_id.asp) ooooor you create a stored procedure that you call in order to increment the second column :D :D– DianaJun 23, 2021 at 16:14
4 Answers
Try below code
protected $_resource;
public function __construct(
...
\Magento\Framework\App\ResourceConnection $resource
...
) {
$this->_resource = $resource;
}
$connection = $this->_resource->getConnection();
$yourLastAddedId = $connection->lastInsertId('your_table_name');
or use below
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('your_table_name');
$yourLastAddedId = $connection->lastInsertId($tableName);
hope its helps you:)
-
-
-
1
-
@GohilRajesh it will always return 0.As there isn't any function in that class Jul 6, 2021 at 5:30
-
check another added code. try to first achive through object manager Jul 6, 2021 at 6:14
i got the answer by creating helper class
<?php
namespace vendor\module\Helper;
use vendor\module\Model\ResourceModel\Collection_Folder\CollectionFactory;
class Helper extends \Magento\Framework\Url\Helper\Data
{
public function __construct(
\Magento\Framework\App\Helper\Context $context,
CollectionFactory $modelFactory
) {
$this->modelFactory = $modelFactory;
return parent::__construct($context);
}
public function getLatestid()
{
$model = $this->modelFactory->create();
$data = $model->load();
foreach ($data as $item) {
$id = $item->getData('id');
}
return $id;
}
}
This way we can get last inserted Id..
You can achieve this with a custom SQL statement
protected $resourceConnection;
public function __construct(
...
\Magento\Framework\App\ResourceConnection $resourceConnection,
...
) {
$this->resourceConnection = $resourceConnection;
}
$connection = $this->resourceConnection->getConnection();
$sql = "select id FROM customtable order by id desc limit 1";
$latestIdResults= $connection->fetchAll($sql);
$latestId = 0;
foreach($latestIdResults as $id){
$latestId = $id["id"];
}
$latestId will be your lastest incremented id :) There are mysql functions for this but this is one way to do it if your keys and tables are heavily customized.
What about
$lastId = $this->resource->getConnection()->fetchOne(
$this->resource->getConnection()
->select()
->from('my_table', ['id'])
->order('id DESC')
)