Kohana获得数据透视表的最后一个插入ID

时间:2014-01-23 20:30:04

标签: php orm kohana pivot-table

我想知道是否有可能获得数据透视表的最后一个插入ID。

数据库 我有3个表(Pk:PrimaryKey,Fk,ForeignKey):

users :
id <- PK
others

posts :
id <- Pk
others

users_posts :
id <-- Pk
user_id <-- Fk
post_id <-- Fk
others

模型

Model_User

Class Model_User extends ORM
{   
    protected $_has_many = array(
        'posts' => array('model'=>'post', 'foreign_key' => 'user_id','through' => 'users_posts'),
    );
}

Model_Post

Class Model_Post extends ORM
{   
    protected $_has_many = array(
        'users' => array('model'=>'user', 'foreign_key' => 'post_id','through' => 'users_posts'),
    );
}

Model_Users_Posts

Class Model_Users_Posts extends ORM
{
    protected $_table_name = 'users_posts';

    protected $_belongs_to = array(
        'users' => array('model'=>'user', 'foreign_key' => 'user_ud'),
        'posts' => array('model'=>'post', 'foreign_key' => 'post_id')
    );
}

控制器

public function action_create()
{

    $post = ORM::factory("posts");

    $post->others = 'hello';

    $post->save();

    $post->add("users",1);
    // Now i want to get the last insert id in users_posts
    $post->add("users",2);
    // Now i want to get the last insert id in users_posts
}

有什么想法吗?

感谢

0 个答案:

没有答案