Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

get last Id from a table
 
optick
Member
 
Avatar
Total Posts:  34
Joined:  2008-07-20
Brasov, Romania
 

hi,
how can I get last ID from a table ?
i want to find what is the last Id from the customer table

thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
vianetz
Guru
 
Avatar
Total Posts:  537
Joined:  2008-04-13
Munich, Germany
 

Take a look at the Module Creator via forum search.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jazkat
Sr. Member
 
Total Posts:  154
Joined:  2009-07-09
 

I didn’t find a direct method for this,
so what I did is while retrieving collection, sort ID attribute descending and then limit collection by 1.

$lastRecord Mage::getModel('mymodule/mytable')
                                ->
getCollection()
                                ->
addAttributeToSort('mytable_id''desc')
                                ->
limit(1);

If you get error “addAttributeToSort()” or “limit()” methods don’t exist, just search through magento and copy methods to your Collection.php file in your modul. Just watch out for number of parameters methods use..

I used the following functions for Mage v 1.3.2.4

public function addAttributeToSort($attribute$dir='asc')
    
{
        
if (!is_string($attribute)) {
            
return $this;
        
}
        $this
->setOrder($attribute$dir);
        return 
$this;
    
}

    
public function limit($limit)
    
{
        $this
->getSelect()->limit($limit);
        return 
$this;
    
}

Hope someone finds this useful..

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top