Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to get last inserted id (id of new node created) with \Orm\Model_Nestedset
  • Hi,
    I try to get the id of new node created.

    This is my code :

    //Create Tree Node
    $page = Model_Page::forge(array('title' => $my_title))->last_child($my_parent)->save();

    But : 
    $page return a boolean so $page->id doesn't work.


    I try this :

    $page = Model_Page::forge(array('title' => $my_title))->last_child($my_parent)->save()->get_one();

    But :

    I have an error "Call to a member function get_one() on a non-object (LOGIC, it's a boolean!!)

    My question is : Is it possible to get the New node's id ?

    ---------------------------------------------------
    EDIT
    ---------------------------------------------------
    A solution but not sexy :

    $page = Model_Page::forge(array('title' => $my_title))->last_child($my_parent)->save()

    if($page)
    {
    $page = Model_Page::find('last');
    }

    I can access to the attributes of my object $page ( $page->id, $page->title..)
  • After calling save() on the node, you can just do echo $page->id (or whatever your primary key is called) to get the inserted ID.

    This even works for IDs generated manually and not auto-incremented.

    Just don't do
    $page = Model_Page::forge()->save();

    but
    $page = Model_Page::forge();
    $page->save();
    echo $page->id

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion