$this->db->where() doesn't recognize dbprefix setting
With the latest (dl'ed 3rd September 2012) build the query builder where() clause doesn't recognize the dbprefix setting. Eg.: my dbprefix: "cms_" $this->db->where('news.status', 'active') : drops error "Unknown column 'news.status' in 'where clause'"
$this->db->where('cms_news.status', 'active') : works perfectly
$this->db->where would accept the key and value pairs for matching column values, not table names. You are not supposed to pass table names to the WHERE clause.
Example:
If your DB Prefix is set to wp_
$this->db->where('user_login', 'rizwan'); $this->db->get('users');
....will select all values from table wp_users where the column user_login matches the value rizwan
@subdesign if @rizwaniqbal's answer doesn't fix your answer feel free to reopen this issue
@alexbilbie I don't agree with @rizwaniqbal.
So you mean that the following is not valid?
<?php
$this->db->from('user')->
join('group', 'group.id = user.group_id')->
where('user.active', TRUE)->
where('group.active', TRUE)->
get();
Yes, my code snippet (news_categories also has a status field):
$query = $this->db->select('news.title, news.preview, news.date, news.slug, news_categories.name as category')
->join('news_categories', 'news_categories.id = news.category_id')
->where('news.status', 'active')
Sorry I completely misread your original problem.
The protect_identifiers() method in system/database/DB_driver.php should automatically append the prefix in the query builder.
To clarify, are prefixes not being applied anywhere or just in certain functions (e.g. where())?
I've just made a test case of the protect_identifiers() function to check it is working as it should https://gist.github.com/3609857
Results in:
array(4) {
[0] =>
string(34) "hostname.database.cms_table.column"
[1] =>
string(25) "database.cms_table.column"
[2] =>
string(16) "cms_table.column"
[3] =>
string(21) "cms_table.column AS c"
}
array(4) {
[0] =>
string(42) "`hostname`.`database`.`cms_table`.`column`"
[1] =>
string(31) "`database`.`cms_table`.`column`"
[2] =>
string(20) "`cms_table`.`column`"
[3] =>
string(27) "`cms_table`.`column` AS `c`"
}
Which is the expected outcome
Only in where() function and when I use table names to differentiate them. For example, in my article_m and gallery_m (models) exists the same error, where I join tables, and the joined tables has the same field (ie. status). And I forgot to say, that I used a previous dev version downloaded in 26th April, with that version I hadn't problem.
Now I'll do the same test, brb
Your test working for me. Where should I dig into further?
Real example, db error message (in all places there is the dbprefix, except the where clause):
Unknown column 'galleries.status' in 'where clause'
SELECT subcms_galleries.id, subcms_galleries.date, subcms_galleries.title, subcms_galleries.folder_name, subcms_bitauth_userdata.fullname as full_name FROM subcms_galleries JOIN subcms_bitauth_userdata ON subcms_bitauth_userdata.user_id = subcms_galleries.user_id WHERE galleries.status = 'active' ORDER BY subcms_galleries.date DESC LIMIT 10
Okay I can go one-by-one and extend the where's but a bit confusing
where() used to add the dbprefix, but that broke table aliases when from(), join(), get(), etc. are called after where(), so the change was reverted. This of course is temporary and support for both prefixing and aliasing will be added.
It's already resolved in the feature/db_qb_aliasing branch, but I've had some quite busy weeks and didn't have the time to touch a bit of CI lately, so was unable to finish and test it.
Okay, thanks
Hello, little issue if in string is "."
(empty($matches[4]) OR strpos($matches[4], '.')) OR $matches[4] = ' '.$this->protect_identifiers(trim($matches[4]));
@churavka Could you explain in detail? It's really hard to get what you're trying to say.
Update: Just merged the feature/db_qb_aliasing branch.
Hello, db prefix ist set to default_
$this->db->select('username, email, id, password, group_id') ->where( sprintf('(username = "%1$s" OR email = "%1$s")', $this->db->escape_str($identity)) ) ->where( active, 1 );
Error Number: 1054 Unknown column '[email protected]' in 'where clause'
SELECT username, email, id, password, group_id FROM default_users WHERE (username = default_aaa@aaa.bb OR email = default_aaa@aaa.bb) AND active = 1 LIMIT 1
@churavka Thanks for reporting that! Here's the fix: 082aa4025ff5764cf10d429903bf48f66a65ce9e
Excellent Thank You
I'm experiencing the same issue as @churavka and the fix https://github.com/bcit-ci/CodeIgniter/commit/082aa4025ff5764cf10d429903bf48f66a65ce9e doesn't work for me.
By the way, i'm using the 3.0 dev version i downloaded today
I'm not reproducing it.
I think I have discovered a bug and am looking for a way to fix this.
I am using CI 3.0.1. The problem happens when I have a space in my search parameter for a like query. This causes items.name to NOT have a prefix in the where clause. (It should be phppos_items.name)
Is this considered a bug or is there a workaround?
ACTIVE RECORD CODE: (Notice search = 'test')
$search = 'test';
$this->db->select("items.*,categories.name as category");
$this->db->from('items');
$this->db->join('categories', 'categories.id = items.category_id','left');
$this->db->where('items.deleted',0);
$this->db->like('items.name', $search);
$this->db->limit($limit);
$by_name = $this->db->get();
echo $this->db->last_query();
QUERY RESULT:
SELECT `phppos_items`.*, `phppos_categories`.`name` as `category`
FROM `phppos_items`
LEFT JOIN `phppos_categories` ON `phppos_categories`.`id` = `phppos_items`.`category_id`
WHERE `phppos_items`.`deleted` =0 AND `phppos_items`.`name` LIKE '%test%' ESCAPE '!' LIMIT 100
ACTIVE RECORD CODE: (notice search is test 2)
$search = 'test 2';
$this->db->select("items.*,categories.name as category");
$this->db->from('items');
$this->db->join('categories', 'categories.id = items.category_id','left');
$this->db->where('items.deleted',0);
$this->db->like('items.name', $search);
$this->db->limit($limit);
$by_name = $this->db->get();
Query Result
SELECT `phppos_items`.*, `phppos_categories`.`name` as `category`
FROM `phppos_items`
LEFT JOIN `phppos_categories` ON `phppos_categories`.`id` = `phppos_items`.`category_id`
WHERE `phppos_items`.`deleted` =0 AND items.name LIKE '%test 2%' ESCAPE '!' LIMIT 100