using active record (delete)

#1
[eluser]kikz4life[/eluser]
hi, still newbie to CI, i have this code in standard php. Now i want to use the CI active record (crud). Can anyone help me with this.? I tried modifying it and using the CI active record in delete.
Code:
$this->db->delete('sec_role', array('role_id' => $crudColumnValues['id']));
or like this
$this->db->where('erp_sec_role.role_id', $crudColumnValues['id']);
$this->db->delete('erp_sec_role');
but still wont work Sad

this is the standard code
Code:
case $crudConfig['create']:
                $sql = 'insert into '.$crudTableName.'(';
                $sql .= implode(',',$crudColumns);
                $sql .= ')VALUES(';
                $sql .= implode(',',$crudColumnValues);
                $sql .= ')';
                mysql_query( $sql );
                break;
            case $crudConfig['update']:
                $sql = 'update '.$crudTableName.' set ';
                foreach($crudColumns as $key => $value){ $updateArray[$key] = $value.'='.$crudColumnValues[$key]; };
                $sql .= implode(',',$updateArray);
                $sql .= ' where role_id = '.$crudColumnValues['id'];
                mysql_query( $sql );
                break;
            case $crudConfig['delete']:

                $sql = 'Delete from '.$crudTableName.' where role_id = '.$crudColumnValues['id'];
                mysql_query( $sql );
                break;
            }


any suggestion or pointer will be a great help for me. Smile

thanks thanks

#2
[eluser]cahva[/eluser]
Well this should work:
Code:
$this->db->delete('sec_role', array('role_id' => $crudColumnValues['id']));

Did you get any error why it didnt work? Does the $crudColumnValues['id'] contain the id you want to delete?

You can use $this->db->last_query() to see what is the last query.

#3
[eluser]kikz4life[/eluser]
yes, $crudColumnValues[‘id’] contains the value i want to delete cause if i use my original code(not using CI) it can delete the row. but using this $this->db->delete('sec_role', array('role_id' => $crudColumnValues['id'])); it aint. Sad(

no error response in my firebug. also i used the $this->db->last_query() but i didnt see the result of my last queried. How come?

also i tried to echo the result of $crudColumnValues['id'] by doing this echo $crudColumnValues['id'] and the result is 34 and thats the id i want to create., i saw this in my firebug.

i'll try every solution you'll come up. really want to work this in CI framework

thanks for the reply cahva

#4
[eluser]cahva[/eluser]
If the last_query didnt work, put this inside your controller's method:
Code:
$this->output->enable_profiler(TRUE);

That will give you detailed information about queries + much more(what libraries are loaded etc.)

#5
[eluser]kikz4life[/eluser]
hi cahva,

Code:
ive tried what you've suggested to snip this code: $this->output->enable_profiler(TRUE);
with
$this->db->delete('sec_role', array('role_id' => $crudColumnValues['id']));
$this->db->last_query();

results:
MEMORY USAGE: 2,867,344 bytes
GET DATA : No GET data exists
$_POST['oper'] : del
$_POST['id'] : 34
QUERIES:: DELETE FROM `erp_sec_role`
          WHERE `role_id` = '\"34\"' 

what do you think the problem?
i'm frequently checking my thread so i can try every possible way to solve this.

thanks

#6
[eluser]cahva[/eluser]
Well that kind of explains why it doesnt work. Its trying to delete role_id with id '"34"' (with quotes). Have you done something to the variable when putting it to $crudColumnValues['id'] variable? Does your code work if you use:
Code:
$this->db->delete('sec_role', array('role_id' => $this->input->post('id')));

#7
[eluser]kikz4life[/eluser]
it worked! haha., thanks a many cahva.. Smile

also could you help me about using the CI active record update

Code:
case $crudConfig['update']:
                $sql = 'update '.$crudTableName.' set ';
                foreach($crudColumns as $key => $value){ $updateArray[$key] = $value.'='.$crudColumnValues[$key]; };
                $sql .= implode(',',$updateArray);
                $sql .= ' where role_id = '.$crudColumnValues['id'];
                mysql_query( $sql );
                break;

also how can i rewrite this part of the code to CI
foreach($crudColumns as $key => $value){ $updateArray[$key] = $value.'='.$crudColumnValues[$key]; };
                $sql .= implode(',',$updateArray);

for the update i've read the manual, but the above code seems a little more complex for me. Also i would like to ask the differences of => to =. From my thoughts "=>" means of the array and for "=" will return strings. Am i right? Sorry for this Big Grin.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.