使用magento中的Zend_Db_Adapter_Pdo_Mysql在magento中获取最后一个插入ID 0

时间:2014-11-09 21:31:22

标签: magento zend-framework

我需要在magento mytable中插入数据....

我通过zend方式为它创建了一个连接......但整件事情很好......但我没有得到最后一个插入ID ....

我的连接助手

public function _dbTable(){

        return $db = new Zend_Db_Adapter_Pdo_Mysql(array(
            'host'     => 'localhost',
            'username' => 'root',
            'password' => '',
            'dbname'   => 'magento-8'
        ));
        }

我的查询

$binds = array(
                    'vname' => $vhelper->_vendorsession()->getVname(),
                    'email' => $vhelper->_vendorsession()->getEmail(),
                    'phone_number' => $vhelper->_vendorsession()->getPhoneNumber(),
                    'company_name' =>  $vhelper->_vendorsession()->getCompanyName(),

                    'brands_name' => $vhelper->_vendorsession()->getBrandsName(),
                    'about_business' => $vhelper->_vendorsession()->getAboutBusiness(),
                    'seller_trade' => $vhelper->_vendorsession()->getSellerTrade(),
                    'seller_primary_category' => $vhelper->_vendorsession()->getSellerPrimaryCategory(),
                    'seller_unique_product_count' => $vhelper->_vendorsession()->getSellerUniqueProductCount(),
                    'seller_unique_sku_depth' => $vhelper->_vendorsession()->getSellerUniqueSkuDepth(),
                    'seller_primary_client_type' => $vhelper->_vendorsession()->getSellerPrimarClientType(),
                    'company_website' => $vhelper->_vendorsession()->getCompanyWebsite(),
                    'seller_city' => $vhelper->_vendorsession()->getSellerCity()         
                );

                 $vhelper->_dbTable()->insert('db_vendor', $data);
                 $id =  $vhelper->_dbTable()->lastSequenceId();
                echo $id;exit;

2 个答案:

答案 0 :(得分:1)

我对zend不熟悉,但对magento不熟悉。你可以在magento中轻松获得last insert id。在你的场景中

<?php

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();


$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$productTable = Mage::getSingleton( 'core/resource' )->getTableName( 'db_vendor' );

$query = "INSERT INTO " . $productTable . " SET vname = :vname". "etc: etc";

$binds = array(
        'vname' => $vhelper->_vendorsession()->getVname(),
        'email' => $vhelper->_vendorsession()->getEmail(),
        'phone_number' => $vhelper->_vendorsession()->getPhoneNumber(),
        'company_name' =>  $vhelper->_vendorsession()->getCompanyName(),

        'brands_name' => $vhelper->_vendorsession()->getBrandsName(),
        'about_business' => $vhelper->_vendorsession()->getAboutBusiness(),
        'seller_trade' => $vhelper->_vendorsession()->getSellerTrade(),
        'seller_primary_category' => $vhelper->_vendorsession()->getSellerPrimaryCategory(),
        'seller_unique_product_count' => $vhelper->_vendorsession()->getSellerUniqueProductCount(),
        'seller_unique_sku_depth' => $vhelper->_vendorsession()->getSellerUniqueSkuDepth(),
        'seller_primary_client_type' => $vhelper->_vendorsession()->getSellerPrimarClientType(),
        'company_website' => $vhelper->_vendorsession()->getCompanyWebsite(),
        'seller_city' => $vhelper->_vendorsession()->getSellerCity()
);

$write->query( $query, $binds );

$lastInsertId = $write->lastInsertId();
echo $lastInsertId; 

如果您有任何疑问,请在此发表评论。

答案 1 :(得分:0)

$id = $vhelper->_dbTable()->insert('db_vendor', $data);