1

I am trying to call a model method within same model and its not working as intended. Here is my class with two methods which do not work

class mymodel extends CI_Model{
    public function __construct(){
        parent::__construct();
        $this->tablea = 'tablea';
        $this->tableb = 'tableb';
    }

    public function saveData($data){
        $dataCopy['revisionkey'] = $this->getRevisionKey($data['id']);
        //check and condition revision key to be int with +1 from last one
        $this->db->insert($this->tableb, $dataCopy);
        $this->db->where('id', $id);
        return $this->db->update($this->user_table, $data) ? true : false;
    }

    public function getRevisionKey($id){
        $this->db->select($this->revision_tablea.'.revisions_number as revisions_number')
            ->from($this->revision_tablea)
            ->where($this->revision_tablea.'.id', $id)
            ->order_by($this->revision_table.'.revisions_number', 'asc')
            ->limit(1);
        $query = $this->db->get();
        if ($query->num_rows() > 0){
            return $query->row_array();
        }else{
            return 0;
        }
    }
}

Now method getRevisionKey() should produce a query like following

SELECT `tableb`.`revisions_number` as revisions_number FROM (`tableb`) WHERE `tableb`.`id` = '26' ORDER BY `tableb`.`revisions_number` asc LIMIT 1

but it produces a query like following

SELECT `tableb`.`revisions_number` as revisions_number FROM (`tableb`) WHERE `id` = '26' AND `tableb`.`id` = '26' ORDER BY `tableb`.`revisions_number` asc LIMIT 1

This of course is due to same method being called within the model, this method works fine if used outside of model. Any solution to this problem?

EDIT Rewriting the getRevisionKey() fixes this. Here is the new version

    public function getRevisionKey($id){
        $sqlQuery = $this->db->select($this->revision_tablea.'.revisions_number as revisions_number')
            ->from($this->revision_tablea)
            ->order_by($this->revision_table.'.revisions_number', 'asc')
            ->limit(1);
        $query = $sqlQuery->where($this->revision_tablea.'.id', $id)->get();
        if ($query->num_rows() > 0){
            return $query->row_array();
        }else{
            return 0;
        }
    }
Kumar
  • 5,038
  • 7
  • 39
  • 51
  • Are you using `getRevisionKey()` externally? I mean from any controller? – Aniket Jun 14 '12 at 15:15
  • In my opinion the problem is the `$this->db->where('id', $id);` in the method above, seems to me like it's adding the extra parameter in addition to the function below which is correct. – Mario Peshev Jun 14 '12 at 15:18
  • @Aniket I am using this in a controller and as expected it works fine. – Kumar Jun 14 '12 at 15:25
  • @MarioPeshev, you are correct. Is there any solution to this problem? Of course I can do some work around but the original problem remains. – Kumar Jun 14 '12 at 15:26
  • I'm guessing you've made some modifications before posting here? Because in the `getRevisionKey` you call `$this->revision_tablea` but you define `$this->tablea` and you have `order_by($this->revision_table.` without A or B at the end. What I'm saying is, I don't see anything wrong, but did you perhaps cut out some parts that could be causing it? – danneth Jun 14 '12 at 15:26
  • @Kumar do you really need this where line in the method above? What happens if you comment it out? – Mario Peshev Jun 14 '12 at 15:28
  • @danneth, oh yes, I didn't want to post the actual code. But the problem is regardless of my code. In `saveData()` I am trying to use 'getRevisionKey()' and both have `where`clause which I think is the way CI uses `$this->db` object. – Kumar Jun 14 '12 at 15:29
  • @MarioPeshev I need that `where`, I guess problem can be reproduced easily with similar use cases. – Kumar Jun 14 '12 at 15:30
  • The difference between both queries that you posted is only the id = 26 verification which comes from this where. If you don't need it here, you should omit it or wrap it with some appropriate if's depending on your cases :) – Mario Peshev Jun 14 '12 at 15:31

1 Answers1

0

Here is a simple hack that will give you exactly where you are making a mistake. Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions

public function _compile_select($select_override = FALSE)
public function _reset_select()

And save it. Before running the function i mean calling

$this->db->get() // Use $this->db->from() instead

use

$query = $this->db->_compile_select()

and echo $query;

These two functions also help in subquery in codeigniter active record. How can I rewrite this SQL into CodeIgniter's Active Records?

Community
  • 1
  • 1
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103