如何在Magento客户地址模型中获取最后插入的ID

时间:2013-03-02 15:58:54

标签: magento

我正在customer_address_entity表中保存客户地址。一切都好,但我想得到最后一次生成的entity_id。有人可以帮帮我吗?

我有以下代码;

        $customAddress = Mage::getModel('customer/address');

         $customAddress->setData($_custom_address)
            ->setCustomerId($customer->getId())
            ->setIsDefaultBilling('0')
            ->setIsDefaultShipping('1')
            ->setSaveInAddressBook('1');

         try {
           $customAddress->save();
         } catch (Exception $ex) {
              Mage::log("\n Address save Error:: \n" . $ex->getMessage(), null, $this->log_entry_file);
       }

6 个答案:

答案 0 :(得分:12)

1 - 使用模型时,会返回$ model object

最近添加/保存的项目ID
    $model->getId()

2 - 但是在使用自定义查询时,请使用以下内容来获取最后插入的ID

$connection    = Mage::getSingleton(’core/resource’)->getConnection(’core_write’);
$sql         = "INSERT INTO <table> SET field1 = ?, field2 = ?, ...";
$connection->query($sql, array($field1_value, $field2_value, ...));
$lastInsertId = $connection->lastInsertId();
echo $lastInsertId; 

3 - 从特定表中,通过定义自己的函数来获取最后插入的id

   function getLastInsertId($tableName, $primaryKey)
   {
    //SELECT MAX(id) FROM table
    $db = Mage::getModel('core/resource')->getConnection('core_read');
    $result = $db->raw_fetchRow("SELECT MAX(`{$primaryKey}`) as LastID FROM `{$tableName}`");
    return $result['LastID'];
   }

答案 1 :(得分:10)

要在保存后使用getId()

获取ID
...
try {
       $customAddress->save();
       echo $customAddress->getId(); 
 }
....

答案 2 :(得分:4)

If you use magento save method than try this to get the last inssert id.

$order->save();
Afetr save method use getId method to get the last insert id in magento 
echo $order->getId(); 


//For custom connection use 
$db_write = Mage::getSingleton('core/resource')->getConnection('core_write');
$sqlQuery = "SELECT * FROM TABLE_NAME ";
$db_write ->query($sqlQuery);
echo $lastInsertId  = $db_write ->fetchOne('SELECT last_insert_id()'); 
                              OR

echo $db_write ->lastInsertId(); // you may also try this.

For more Info 

Click Here

答案 3 :(得分:1)

使用以下代码解析您的查询:

$customer = Mage::getModel("customer/customer");
$customer   ->setWebsiteId($websiteId)
            ->setStore($store)
            ->setFirstname('John')
            ->setLastname('Doe')
            ->setEmail('jd1@ex.com')
            ->setPassword('somepassword');


            $address = Mage::getModel("customer/address");
            $address->setCustomerId($customer->getId())
            ->setFirstname($customer->getFirstname())
            ->setMiddleName($customer->getMiddlename())
            ->setLastname($customer->getLastname())
            ->setCountryId('HR')
            ->setPostcode('31000')
            ->setCity('Osijek')
            ->setTelephone('0038511223344')
            ->setFax('0038511223355')
            ->setCompany('Inchoo')
            ->setStreet('Kersov')
            ->setIsDefaultBilling('1')
            ->setIsDefaultShipping('1')
            ->setSaveInAddressBook('1');

        try{
            $address->save();
            $lastId = $address->getId(); // Last id
        }
        catch (Exception $e) {
            Zend_Debug::dump($e->getMessage());
        }

答案 4 :(得分:1)

有一种magento方法:

$table = $model->getResource()->getMainTable();

$connection = Mage::getSingleton('core/resource')->getConnection();
$id = $connection->lastInsertId($tableName);

答案 5 :(得分:0)

css

使用此代码获取magento中最后插入的id。