Part of the EllisLab Network
   
 
weird bug on database model [ci 1.6.2]
Posted: 09 June 2008 01:16 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  92
Joined  12-27-2007

Hi .. I found a bug that not always happened in my model

i use ci 1.6.2

here is my code

function save()
        
{
            $this
->db->set('measureid',$this->measureid);
            
$this->db->set('goodsid',$this->goodsid);
            
$this->db->set('poid',$this->poid);
            
$this->db->set('qty',$this->qty);
            
$this->db->set('qty0',$this->qty0);
            
            
$this->db->set('unitprice',$this->unitprice);
            
$this->db->set('discount',$this->discount);
            
$this->db->set('total', $this->calculatetotal());
            
            
$this->db->trans_begin();
            
$status = '';  
            if(
$this->id != 0)
            
{
                $this
->db->where('id',$this->id);
                
$status = $this->db->update($this->_table);
            
}else
            {
                $status
= $this->db->insert($this->_table);
                
$this->id = $this->db->insert_id() ;
            
}
            
            
if(!$this->checkstatus($status,$this->_table)){
                
echo $this->goodsid.' <br/>';
                echo
$this->db->last_query();
                return
false;
            
}

            
if(!$this->updateHeader($this->poid))
                return
false;      

            if (
$this->db->trans_status()===FALSE){
              
return $this->rolllback();
            
} else {
              $this
->db->trans_commit();
              return
true;
            
}
        }

as you can see, there is

$this->db->set('measureid',$this->measureid);
$this->db->set('goodsid',$this->goodsid);


on the top of the db->set();

measureid and goodsid is linked with other table (foreign keys)

when I did insert it produce the error because measureid or goodsid not included into the insert sql.

here is the error:

Cannot add or update a child row: a foreign key constraint fails (`ci/podt`, CONSTRAINT `podt_ibfk_2` FOREIGN KEY (`goodsid`) REFERENCES `mtgoods` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL : INSERT INTO `podt` (`measureid`, `qty`, `unitprice`, `discount`, `total`) VALUES ('1', 20, '30000', '300', 599700)

as you can see on my code, i have set the goodsid and measureid right ?

 Signature 

[color=blue]——
wysmedia.com
Using:Code Igniter 1.6.2

Programming: 90% make data validation, 9% UI, 1% data entry

Profile
 
 
Posted: 09 June 2008 01:33 AM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1261
Joined  01-07-2008

That is quite odd, and I have no idea what could be causing it.  Even if the variables aren’t defined the fields should still get set.

What type of database are you using?
And can you try it without the transaction, just to whittle away at the possible causes and get a reduced version.

 Signature 

Blog | Twitter | Coffee

Profile
 
 
Posted: 09 June 2008 01:47 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  92
Joined  12-27-2007

I did a research on this

$this->db->set('measureid',$this->measureid);
            
$this->db->set('goodsid',$this->goodsid);
            
$this->db->set('poid',$this->poid);
            
$this->db->set('qty',$this->qty);
            
$this->db->set('qty0',$this->qty0);
            
$this->db->set('unitprice',$this->unitprice);
            
$this->db->set('discount',$this->discount);
            
$this->db->set('total', $this->calculatetotal());
            
            echo
'goodsid:'.$this->goodsid ;
            
print_r($this->db->ar_set);
            return
false;

it produce this code:

goodsid:1Array ( [`measureid`] => '1' [`qty`] => 20 [`unitprice`] => '30000' [`discount`] => '300' [`total`] => 599700 )

and something even weird than this smile
if I change the goodsid into something other that 1 it will produce a correct result:

goodsid:6Array ( [`measureid`] => '1' [`goodsid`] => '6' [`qty`] => 20 [`unitprice`] => '30000' [`discount`] => '300' [`total`] => 599700 )

... and this one will make you confuse a lot ...
I change the code into this (just changing the place of measureid and goodsid

$this->db->set('goodsid',$this->goodsid);
            
$this->db->set('measureid',$this->measureid);
            
$this->db->set('poid',$this->poid);
            
$this->db->set('qty',$this->qty);
            
$this->db->set('qty0',$this->qty0);
            
$this->db->set('unitprice',$this->unitprice);
            
$this->db->set('discount',$this->discount);
            
$this->db->set('total', $this->calculatetotal());

            echo
' goodsid: ' .$this->goodsid;  
            echo
' measureid :' .$this->measureid;  
            
print_r($this->db->ar_set);
            return
false;

if the goodsid and measureid = 1 the result : (no measureid in the ar_set)

goodsid: 1 measureid :1 Array ( [`goodsid`] => '1' [`qty`] => 20 [`unitprice`] => '30000' [`discount`] => '300' [`total`] => 599700 )

if the goodsid changed to other than 1 result

goodsid: 10 measureid :1 Array ( [`goodsid`] => '10' [`measureid`] => '1' [`qty`] => 20 [`unitprice`] => '30000' [`discount`] => '300' [`total`] => 599700 )

I will check on active record set() function after this :D
so weird ... so true ... :D

 Signature 

[color=blue]——
wysmedia.com
Using:Code Igniter 1.6.2

Programming: 90% make data validation, 9% UI, 1% data entry

Profile
 
 
Posted: 09 June 2008 01:49 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  92
Joined  12-27-2007
inparo - 09 June 2008 01:33 AM

That is quite odd, and I have no idea what could be causing it.  Even if the variables aren’t defined the fields should still get set.

What type of database are you using?
And can you try it without the transaction, just to whittle away at the possible causes and get a reduced version.

I did without transaction ... look at my 2nd post. i just doing db->set() ...
I use mysql database ... before i used to use array instead of using db->set() and it works well ... now i want to make my code looks better so I use set.

any idea hahahaha ...

 Signature 

[color=blue]——
wysmedia.com
Using:Code Igniter 1.6.2

Programming: 90% make data validation, 9% UI, 1% data entry

Profile
 
 
Posted: 09 June 2008 02:09 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  92
Joined  12-27-2007

I make a deeper research on this
I did a modification on system\database\DB_active_rec.php

function set($key, $value = '', $escape = TRUE)
    
{
        $key
= $this->_object_to_array($key);
    
        if ( !
is_array($key))
        
{
            $key
= array($key => $value);
        
}    

        
foreach ($key as $k => $v)
        
{
            
if ($escape === FALSE)
            
{
                $this
->ar_set[$this->_protect_identifiers($k)] = $v;
                if (
$this->ar_caching === TRUE)
                
{
                    $this
->ar_cache_offset[$this->_protect_identifiers($k)] = $v;
                
}
            }
            
else
            
{
                            $this
->ar_set[$this->_protect_identifiers($k)] = $this->escape($v);
                if (
$this->ar_caching === TRUE)
                
{
                    $this
->ar_cache_offset[$this->_protect_identifiers($k)] = $this->escape($v);
                
}
            }
                }
                print_r
($this->ar_set); echo '<br/>';
        
        return
$this;
    
}

I just add print_r($this->ar_set); echo ’<br/>’; to see what the happens in arr_set[] array ...

and when the code (my 2nd post) was runned it produce

Array ( [`goodsid`] => '1' )
Array (
[`goodsid`] => '1' [`measureid`] => '1' )
Array (
[`goodsid`] => '1' [`measureid`] => '1' [`poid`] => '1' )
Array (
[`goodsid`] => '1' [`measureid`] => '1' [`poid`] => '1' [`qty`] => 20 )
Array (
[`goodsid`] => '1' [`measureid`] => '1' [`poid`] => '1' [`qty`] => 20 [`qty0`] => '1' )
Array (
[`goodsid`] => '1' [`measureid`] => '1' [`poid`] => '1' [`qty`] => 20 [`qty0`] => '1' [`unitprice`] => '30000' )
Array (
[`goodsid`] => '1' [`measureid`] => '1' [`poid`] => '1' [`qty`] => 20 [`qty0`] => '1' [`unitprice`] => '30000' [`discount`] => '300' )
Array (
[`goodsid`] => '1' [`qty`] => 20 [`unitprice`] => '30000' [`discount`] => '300' [`total`] => 599700 )

but if I change the measureid into something else

Array ( [`goodsid`] => '1' )
Array (
[`goodsid`] => '1' [`measureid`] => '7' )
Array (
[`goodsid`] => '1' [`measureid`] => '7' [`poid`] => '1' )
Array (
[`goodsid`] => '1' [`measureid`] => '7' [`poid`] => '1' [`qty`] => 1 )
Array (
[`goodsid`] => '1' [`measureid`] => '7' [`poid`] => '1' [`qty`] => 1 [`qty0`] => '1' )
Array (
[`goodsid`] => '1' [`measureid`] => '7' [`poid`] => '1' [`qty`] => 1 [`qty0`] => '1' [`unitprice`] => '30000' )
Array (
[`goodsid`] => '1' [`measureid`] => '7' [`poid`] => '1' [`qty`] => 1 [`qty0`] => '1' [`unitprice`] => '30000' [`discount`] => '300' )
Array (
[`goodsid`] => '1' [`measureid`] => '7' [`qty`] => 1 [`unitprice`] => '30000' [`discount`] => '300' [`total`] => 29700 )


it produce correct result ... sigh ... any idea ?

so weird ... :(

ps: I use PHP Version 5.2.5

 Signature 

[color=blue]——
wysmedia.com
Using:Code Igniter 1.6.2

Programming: 90% make data validation, 9% UI, 1% data entry

Profile
 
 
Posted: 09 June 2008 02:33 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  92
Joined  12-27-2007

I found the problem but don’t know which one is the error ... and i think it is not logic at all .. :(
I found that the problem was caused by calculatetotal() function ... but that function dont have anything to do with db->ar_set()

function calculatetotal(){
            $measure
= $this->mtconversions->get($this->measureid);
            
$this->qty = $this->qty0 * $measure->value ;
            
$total = ($this->qty * $this->unitprice) - $this->discount ;  
            
$this->total = $total ;
            return
$total ;
        
}

        
function save()
        
{
            $this
->calculatetotal();   

            
$this->db->set('goodsid',$this->goodsid);
            
$this->db->set('poid',$this->poid);
            
$this->db->set('measureid',$this->measureid);
            
$this->db->set('qty',$this->qty);
            
$this->db->set('qty0',$this->qty0);
            
$this->db->set('unitprice',$this->unitprice);
            
$this->db->set('discount',$this->discount);
            
$this->db->set('total', $this->total);
            
// bug will be triggered the above with
            // $this->db->set('total', $this->calculatetotal());
            
            
print_r($this->db->ar_set);
            return
false;
        
}

let me know if someone knows about this incident :D.. i am still curious ...

 Signature 

[color=blue]——
wysmedia.com
Using:Code Igniter 1.6.2

Programming: 90% make data validation, 9% UI, 1% data entry

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 62793 Total Logged-in Users: 18
Total Topics: 77479 Total Anonymous Users: 0
Total Replies: 418200 Total Guests: 147
Total Posts: 495679    
Members ( View Memberlist )
Newest Members:  newbie boymarkrichesongerdyPorscheescapeexpyAnn BaileyTy BexDamien2k8cibbuser
Active Members:    bema2004swcfullerCI LeecicadessCodeSpeedCrucialDark PreacherDerek AllardDewoshjuejsdfmahutimarcossnewbie boyTien HungYuryZac G.