2013-03-02 21 views
6

Zapisuję adres klienta w tabeli customer_address_entity. Wszystko jest w porządku, ale chcę uzyskać ostatni wygenerowany entity_id. Czy ktoś może mi pomóc?Jak uzyskać ostatni wstawiony identyfikator w modelu adresu klienta Magento

Mam następujący kod;

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

Odpowiedz

10

Aby uzyskać identyfikator po zapisaniu użycie getId()

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

1 - przy użyciu modelu, to zwraca ostatnio dodane/zapisanego Item ID przez $ model obiektowy

$model->getId() 

2 - Ale w przypadku korzystania z niestandardowego zapytania, użyj następującego polecenia, aby uzyskać ostatni wstawiony identyfikator:

$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 - Od konkretnej tabeli dostać ostatni zapisany identyfikator definiując własną funkcję

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

użyć następującego kodu, aby rozwiązać zapytanie:

$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

Istnieje metoda magento:

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

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