Welcome Guest, Not a member yet? Register   Sign In
Feature Request:
#1

[eluser]Unknown[/eluser]
After querying, to get the inserted id you call
Code:
$this->db->insert_id()

It would be really nice if you could do something like this instead:
Code:
$result = $this->db->query($query);
$inserted_id = $result->insert_id()

That method would simply return false if an insert was not performed on an result that tried to get the insert id. Make sense?
#2

[eluser]Michael Wales[/eluser]
How does this differ from:

Code:
$result = $this->db->query($query);
$inserted_id = $this->db->insert_id();

db->insert_id() merely calls PHP's mysql_insert_id() function (assuming you are running MySQL). That function returns FALSE if no connection was present, 0 if the previous query did not generate an AUTO_INCREMENT ID (the functionality you recommend), or the ID of the row if all was successful.
#3

[eluser]Unknown[/eluser]
I think you're misunderstanding me. I know you have to get it by calling the db object. i want the insert id to be in the returned db object. like

Code:
// i want this
$result = $this->db->query($q);
$result->num_rows();
$result->insert_id();

// its like this now
$result = $this->db->query($q);
$result->num_rows();
$this->db->insert_id();

Make sense?
#4

[eluser]Michael Wales[/eluser]
Ah yes - I see what you mean. One way to get around this is to place it within your model methods (this is usually what I do). Yeah, you're still hitting the database another time but you'd be hitting the database another time no matter what (since MySQL doesn't return the new ID upon INSERT). It's kind of a work around way and adds another layer of abstraction on top of what you are asking for, but depending on how your application is structured it may be a good solution.

Some demo code:
Code:
function newUser($username = '') {
  $result = $this->db->insert('users', array('username'=>$username));
  if ($this->db->affected_rows() > 0) {
    return $this->db->insert_id();
  } else {
    return FALSE;
  }
}

Of course, you could always extend the core and just override the insert() method within the DB driver. That would prevent you from adding in the layer of abstraction.
#5

[eluser]xwero[/eluser]
To do what you want the object always has to be loaded with the insert id which means an extra function call and then it is the question how to handle the result_array method, should the insert id be added or not?

Although it isn't impossible to do you will not always use it. I think it's a good thing not to load more than you need.

You could create a method where you add it to the result and call that function.




Theme © iAndrew 2016 - Forum software by © MyBB