2013-03-02 9 views
6

Tôi đang lưu địa chỉ khách hàng trong bảng customer_address_entity. Tất cả mọi thứ là ok nhưng tôi muốn có được cuối cùng tạo ra entity_id. Ai đó có thể giúp tôi được không?Cách lấy id được chèn cuối cùng trong mô hình địa chỉ khách hàng Magento

Tôi có mã sau;

 $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); 
     } 

Trả lời

10

Để có được id sau khi tiết kiệm sử dụng getId()

... 
try { 
     $customAddress->save(); 
     echo $customAddress->getId(); 
} 
.... 
12

1 - khi sử dụng một mô hình, điều này sẽ trả về được thêm gần đây/lưu id mục bằng $ mô hình đối tượng

$model->getId() 

2 - Nhưng khi sử dụng truy vấn tùy chỉnh, sau đó sử dụng sau để lấy id được chèn lần cuối

.210

3 - Từ một bảng cụ thể, nhận được id chèn vào cuối bởi việc xác định chức năng của riêng bạn

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']; 
    } 
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

1

Sử dụng đoạn mã sau để giải quyết truy vấn của bạn:

$customer = Mage::getModel("customer/customer"); 
$customer ->setWebsiteId($websiteId) 
      ->setStore($store) 
      ->setFirstname('John') 
      ->setLastname('Doe') 
      ->setEmail('[email protected]') 
      ->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()); 
     } 
0

Có một phương pháp Magento cho điều đó:

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

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