Welcome Guest, Not a member yet? Register   Sign In
SQL WHERE LIKE is being ignored in an ActiveRecord update??
#1

[eluser]Zoon[/eluser]
I've enabled profiling (and have it in a hidden DIV, which I toggle() usin jquery) and I'm running the following SQL query.

Code:
$this->db->select("netaddress, AddressId, NetCidr");
$this->db->from("net_ips");
$this->db->limit(10);
$query = $this->db->get();
        
if ($query->num_rows() > 0 )
{
    $result = $query->result_array();
    foreach ($result as $row)
    {
        $iprangeEx = explode('.', $row["netaddress"]);
        $update = array("netips_id" => $row["AddressId"]);
        $this->db->like("ip", "$iprangeEx[0].$iprangeEx[1].$iprangeEx[2]", "after");
        $this->db->where("NetID", $row["NetCidr"]);
        $this->db->update("addresses", $update);
    }
    return "Success";
}
The LIKE line is not being generated, example:

Code:
0.0001      SELECT `netaddress`, `AddressId`, `NetCidr`
FROM (`net_ips`)
LIMIT 10
0.2824      UPDATE `addresses` SET `netips_id` = '1' WHERE `NetID` = '1'
0.2730      UPDATE `addresses` SET `netips_id` = '2' WHERE `NetID` = '1'
0.2722      UPDATE `addresses` SET `netips_id` = '3' WHERE `NetID` = '1'
0.2847      UPDATE `addresses` SET `netips_id` = '4' WHERE `NetID` = '1'
0.2785      UPDATE `addresses` SET `netips_id` = '5' WHERE `NetID` = '1'
0.2694      UPDATE `addresses` SET `netips_id` = '6' WHERE `NetID` = '1'
0.2705      UPDATE `addresses` SET `netips_id` = '7' WHERE `NetID` = '1'
0.2696      UPDATE `addresses` SET `netips_id` = '8' WHERE `NetID` = '1'
0.2684      UPDATE `addresses` SET `netips_id` = '9' WHERE `NetID` = '1'
0.2696      UPDATE `addresses` SET `netips_id` = '10' WHERE `NetID` = '1'

If I change it from like to where
Code:
$iprangeEx = explode('.', $row["netaddress"]);
$update = array("netips_id" => $row["AddressId"]);
$this->db->where("ip", "$iprangeEx[0].$iprangeEx[1].$iprangeEx[2]", "after");
$this->db->where("NetID", $row["NetCidr"]);
$this->db->update("addresses", $update);
it is rendered, although that obviously doesn't do what I want it to do anyway.

Code:
0.0002      SELECT `netaddress`, `AddressId`, `NetCidr`
FROM (`net_ips`)
LIMIT 10
0.0004      UPDATE `addresses` SET `netips_id` = '1' WHERE `ip` = '172.24.0' AND `NetID` = '1'
0.0002      UPDATE `addresses` SET `netips_id` = '2' WHERE `ip` = '172.24.1' AND `NetID` = '1'
0.0002      UPDATE `addresses` SET `netips_id` = '3' WHERE `ip` = '172.24.2' AND `NetID` = '1'
0.0002      UPDATE `addresses` SET `netips_id` = '4' WHERE `ip` = '172.24.3' AND `NetID` = '1'
0.0002      UPDATE `addresses` SET `netips_id` = '5' WHERE `ip` = '172.24.4' AND `NetID` = '1'
0.0002      UPDATE `addresses` SET `netips_id` = '6' WHERE `ip` = '172.24.5' AND `NetID` = '1'
0.0002      UPDATE `addresses` SET `netips_id` = '7' WHERE `ip` = '172.24.6' AND `NetID` = '1'
0.0002      UPDATE `addresses` SET `netips_id` = '8' WHERE `ip` = '172.24.7' AND `NetID` = '1'
0.0001      UPDATE `addresses` SET `netips_id` = '9' WHERE `ip` = '172.24.8' AND `NetID` = '1'
0.0002      UPDATE `addresses` SET `netips_id` = '10' WHERE `ip` = '172.24.9' AND `NetID` = '1'

(This code is for a DB convertor, I'm writing an application which has to be data-compatible with an existing open source product we use, which doesn't have useful things like unique keys on half its tables, or any kind of relationship model to string it together.)

I could override this with a standard SQL query, but I prefer to use ActiveRecord...

What's going on? Sad
#2

[eluser]Zoon[/eluser]
Well, this teaches me for not searching properly.

http://ellislab.com/forums/viewthread/143736/

The bug link there no longer exists however.

I'll download a fresh copy today from the website, and if its not fixed there, I'll checkout the database driver file from bitbucket and if its also not fixed THERE, I'll open a new bug for it.
#3

[eluser]Zoon[/eluser]
http://bitbucket.org/ellislab/codeignite...driver.php

Edit for clarification:
The _update method is not passed $like, nor does it expect $like.

Raised as a bug/fr on bitbucket! http://bitbucket.org/ellislab/codeignite...ecord-like
#4

[eluser]InsiteFX[/eluser]
Code:
$this->db->like("ip", "$iprangeEx[0].$iprangeEx[1].$iprangeEx[2]", "after");

// should be:
$this->db->like('ip', $iprangeEx[0].$iprangeEx[1].$iprangeEx[2], 'after');

// you should be using single qoutes not double qoutes!

InsiteFX
#5

[eluser]Zoon[/eluser]
Thanks for your feedback, but I use double quotes everywhere, and my app works fine. I'm self taught, so that's probably why I don't appreciate the difference. Can you elaborate why?

By hacking my database driver (I'm never going to re-distribute this code so I just made myself a note as to what I did) and with the code as per the first post I made above it now outputs the code correctly, and works.
#6

[eluser]WanWizard[/eluser]
'like' is not defined in the database driver files, it's a method of DB_active_rec.php.

And the issue is not the 'like' statement, it is the fact that the update() method only uses the where clauses defined, and ignores the likes...
#7

[eluser]Zoon[/eluser]
You're quite right. The method like is not defined in the database driver files.

To clarify, DB_active_rec.php is not passing the like options into the database driver's _update method, which also isn't expecting it. It is passing it for _delete, and it is expected for delete.

This is either an oversight, and thus a bug, or has been excluded for some reason.

If the latter, I'd like to know why, if anyone knows? Smile

I've also never contributed to an open source project, so I dunno if I'm literally supposed to fix it and say "do you want this code?" or what!




Theme © iAndrew 2016 - Forum software by © MyBB