1

我这里有多对多表,我只需要插入 id - BlogId。lastInsertId() 是 NULL 结果。有任何想法吗?

 $table = new Blog_Model_Blog_Table();
    $relTable = new Blog_Model_Relation_Table();
        $Object = $table->createRow();
        $form = new Blog_Form_Blog_Add($Object);
        if ($this->getRequest()->isPost() and $form->isValid($_POST)) {
            Blog_Model_Blog_Manager::add($Object);
            $blogId = $table->getAdapter()->lastInsertId();
            foreach ($_POST['category_id'] as $value) {
                $relTable->insert(array('id' => $blogId, 'category_id' => $value));
            }

Blog_Model_Blog_Manager:

 class Blog_Model_Blog_Manager
{
    static function add(Blog_Model_Blog_Item &$Object)
    {
        $data = array(
            'time_add'              =>  time(),
            'time_edit'             =>  time(),
            'url_keyword'           =>  Ap_Filter_Translit::asURLSegment($Object->name)
        );
        $Object->setFromArray($data)
               ->save();
    }
4

1 回答 1

1

我使用 mySql 函数 LAST_INSERT_ID() 来实现:

$blogId = $table->fetchRow($table->select()->from($table)->columns('LAST_INSERT_ID() as idi'));
于 2013-03-29T11:50:09.037 回答