Welcome Guest, Not a member yet? Register   Sign In
Active Record, update using both with and like criteria, like criteria drops
#1

[eluser]Xollef[/eluser]
I must be blind :coolcheese: or something but I can't get this little query to work. For some reason the like criteria is dropped from the query.
Code:
$this->db->set('parentage', $parentage."-".$newSortOrder);
$this->db->like('parentage', '0-00004', 'after');
$this->db->where('language_code', $language);
$this->db->update('pages');

echo $this->db->last_query();
Produces
Quote:UPDATE `pages` SET `parentage` = '0-00001-00001-00001' WHERE `language_code` = 'en'


Can someone see what is wrong with this active record query and give a reason why the like criteria is dropped.

PS: I use the newest Codeigniter.
#2

[eluser]Chris Newton[/eluser]
I can't tell you from experience, but maybe replace the WHERE statement with another LIKE statement.

$this->db->like('language_code', $language);

If your language code is always just 2 letters, then the wildcards around 'en' wouldn't matter anyway.
#3

[eluser]Seppo[/eluser]
Active record does not check for "like" arguments, it only checks for "where" clauses. I'm not sure if it's a bug or intentional... probably they didn't think that anybody will use it, but it would be ok to add...
#4

[eluser]Chris Newton[/eluser]
http://ellislab.com/codeigniter/user-gui...ecord.html

According to the documentation it does. The following from Active Records documentation. Also, if you check the code in Db_active_rec class around line 369, the function for LIKE exists. So if it doesn't work, that's a pretty major, major bug.

Code:
$this->db->like();
This function enables you to generate LIKE clauses, useful for doing searches.

Note: All values passed to this function are escaped automatically.
Simple key/value method:
$this->db->like('title', 'match');

// Produces: WHERE title LIKE '%match%'
If you use multiple function calls they will be chained together with AND between them:

$this->db->like('title', 'match');
$this->db->like('body', 'match');

// WHERE title LIKE '%match%' AND body LIKE '%match%
If you want to control where the wildcard (%) is placed, you can use an optional third argument. Your options are 'before', 'after' and 'both' (which is the default).
$this->db->like('title', 'match', 'before');
// Produces: WHERE title LIKE '%match'    

$this->db->like('title', 'match', 'after');
// Produces: WHERE title LIKE 'match%'

$this->db->like('title', 'match', 'both');
// Produces: WHERE title LIKE '%match%'
Associative array method:
$array = array('title' => $match, 'page1' => $match, 'page2' => $match);

$this->db->like($array);

// WHERE title LIKE '%match%' AND page1 LIKE '%match%' AND page2 LIKE '%match%'
#5

[eluser]Xollef[/eluser]
Thanks for your replies, if you havent spotted any obvious typing error then it seems it is a bug/feature.

Quote:
Code:
$this->db->set('parentage', $parentage."-".$newSortOrder);
$this->db->like('parentage', '0-00004', 'after');
$this->db->where('language_code', $language);
$this->db->update('pages');

echo $this->db->last_query();
Produces
Quote:UPDATE `pages` SET `parentage` = '0-00001-00001-00001' WHERE `language_code` = 'en'

As you can see I only use LIKE on the parentage column and there I have to use a LIKE in this query. The language code is a normal where criteria.

Has anyone got a similar query to work with Active record or can someone confirm that this is a bug(feature)?

After checking through my code I notice that I have perfectly working Active Record select queries that use LIKE criteria. So it seems this problem, for me at least, only occurres on update Active Record queries.

Quote:I have actually solved this using this run query method so I have no panic
Code:
$this->db->query($updateQuery);


But I would like to be able to use Active Record (I like it) for this query. I have actually followed the documentation when trying to build the Active Record query and as Mahuti points out in the documentation there are examples on how to use like statements with Active Record. It seems to be a bug.
#6

[eluser]Noah David[/eluser]
Put the LIKE statement after the WHERE statement.
#7

[eluser]Xollef[/eluser]
Actually I have tried having the like statement after the WHERE but it does not help.
#8

[eluser]Noah David[/eluser]
And if you comment out the WHERE statement, it produces the LIKE statement correctly?
#9

[eluser]Xollef[/eluser]
Just tested, and the answer is nope.
It does not produce a LIKE statement at all when the WHERE statement is commented out. It just produces this
Code:
UPDATE `pages` SET `parentage` = ‘0-00001-00001-00001’
#10

[eluser]Noah David[/eluser]
Sounds like a bug then. Smile




Theme © iAndrew 2016 - Forum software by © MyBB