7

我将客户地址保存在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);
       }
4

6 回答 6

15

1 - 使用模型时,这将返回最近添加/保存的项目 ID 由 $model 对象

    $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'];
   }
于 2013-11-12T10:52:11.740 回答
11

保存使用后获取idgetId()

...
try {
       $customAddress->save();
       echo $customAddress->getId(); 
 }
....
于 2013-03-02T16:18:33.533 回答
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 

点击这里

于 2014-01-15T06:45:31.260 回答
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());
        }
于 2016-11-30T11:54:28.773 回答
1

有一个magento方法:

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

$connection = Mage::getSingleton('core/resource')->getConnection();
$id = $connection->lastInsertId($tableName);
于 2017-08-25T15:48:48.197 回答
0
$freeevent = Mage::getModel('freeevent/freeevent');
$freeevent->setData('firstname', $firstname);
$freeevent->setData('lastname', $lastname);
$freeevent->save();
$getLastId = $freeevent->getId(); 

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

于 2018-04-10T12:57:02.527 回答