Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

add_key() in DB Forge is not behaving correctly #2027

Closed
cjbuchmann opened this Issue · 3 comments

2 participants

@cjbuchmann

So it looks like this issue was submitted here : #1946, but has since been closed. Following the thread comment, I didn't see any resolution to the problem at all.

this code should create a key called blog_name_blog_label

$this->dbforge->add_key(array('blog_name', 'blog_label'));

unfortunately, it just throws a warning

Severity: Warning

Message: Illegal offset type in isset or empty

Filename: mysqli/mysqli_forge.php

Line Number: 184

Looking through the code, I noticed that the mysqli_forge.php file on line 184 doesn't handle keys that are arrays, thus the problem shown above.

// these keys can be arrays
if ( ! isset($this->fields[$this->keys[$i]]))
{
  unset($this->keys[$i]);
  continue;
}

the fix is simple enough

if(is_array($this->keys[$i]))
{
  foreach($this->keys[$i] as $key)
  {
    if( ! isset($this->fields[$key]))
    {
      unset($this->keys[$i]);
      break;
    }
  }
}
else
{
  if ( ! isset($this->fields[$this->keys[$i]]))
  {
    unset($this->keys[$i]);
    continue;
  }
}

However I'm not sure about just 'unsetting' the values. Would it make more sense to throw an error here instead?

@narfbg
Owner

How is that useful?
I undestand composite PRIMARY and/or UNIQUE keys, but what benefit do you have with a combined index on 2 fields?

@cjbuchmann

[composite indexes] will benefit a query that uses those fields for joining,
filtering, and sometimes selecting.
It will also benefit queries that use left-most subsets of columns in that composite.

From a Google search that attempts to answer that.
http://stackoverflow.com/questions/1823685/when-should-i-use-a-composite-index

That aside, MySQL fully supports this and I personally think the library should too. If that's not the case, then the guide should definitely be updated to reflect that, as it's mentioned here http://codeigniter.com/user_guide/database/forge.html under the Adding Keys section.

@narfbg narfbg closed this issue from a commit
@narfbg narfbg Fix #2027 3545102
@narfbg narfbg closed this in 3545102
@narfbg
Owner

See the above commit.

@nonchip nonchip referenced this issue from a commit in nonchip/CodeIgniter
@narfbg narfbg Fix #2027 4678a86
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.