Exploit
Exploit

Reputation: 6386

how to use regular pdo in CI

i am used to writing regular pdo statements into php how can i continue doing that without having to learn active records or use whatever else method there is in CI

i did something like this

$sql = "select column1,column2 from table where column_id = :column_id and column_2 = :something"
$query = $db->query($sql);
$query->bindParam(":column_id", $_GET['value'], PDO::PARAM_INT);
$query->bindParam(":something", $_GET['something'], PDO::PARAM_STR);
$query->execute();

while($row = $query->fetch(PDO::FETCH_OBJ)
{
   print $row->data;
}

rather than using $_GET i can probably use $this->uri->segment() which hopefully works with this...

I looked at active record and that looks like a huge bad idea, i dont know what Ellislab was thinking. no clue how complex queries can go into them. I am a newb learning from nettuts i can be wrong and please show me if i am. just speaking from what i have learned so far.

writing query statments the otherway such as

$query = $this->db->query("my sql statement") i would have to escape each value i am querying with codeigniters escape methods or just use mysql_real_escape_string. which is something i could of done to begin with before diving into the mvc world but i chose pdo because its something that already works well.

how can i implement what i am already used to?? I dont mind modifying core files , ill hopefully end up learning all the classes to reverse engineer it but until that day comes i'll have hopefully written my own mvc.

thanks

Upvotes: 1

Views: 1045

Answers (2)

Damien Pirsy
Damien Pirsy

Reputation: 25445

Sorry, I fail to see how this:

$query = $this->db->select('column1,column2')
                 ->from('table')
                 ->where('column_id',$this->input->get('value'))
                 ->where('column_2',$this->input->get('something'))
                 ->get();
return $query->results();

"looks like alot of extra work" rather than this:

$sql = "select column1,column2 from table where column_id = :column_id and column_2 = :something"
$query = $db->query($sql);
$query->bindParam(":column_id", $_GET['value'], PDO::PARAM_INT);
$query->bindParam(":something", $_GET['something'], PDO::PARAM_STR);
$query->execute();

Keep in mind that AR automatically escapes, so you don't worry about SQL injections.

If you're more used to query bindings, you can also do

$sql = "select column1,column2 from table where column_id = ? and column_2 = ?";
$query = $this->db->query($sql, array($this->uri->segment(3), $something);

and take advantage of parametrization, without using Active Record class.

Nothing prevents you from using PDO, or even mysql_* for what that matters, instead of Active Record class. And your query has nothing that prevents CI from using it - apart from the $_GET array which you must enable, but can easily subsitute with $this->input->get() or fetch manually the URI segment or have it passed automatically to the controller's method, as usually happens.

You say "am a newb learning from nettuts" , but still say that Ellislabs doesn't know what it's doing; altough lightweight and far from perfect CI has a solid community, is actively developed and its general consensus is quite high; and if it decides to have the Active Record the way it is, there's a reason.

Also, consider that other more proper and real ORMs, like Propel or Doctrine, use a similar style in running queries (and you can use them too, instead of AR if you like it), taking advantage of PHP5 concatenation. An example taken right from the frontpage of propel:

$books = BookQuery::create()  // retrieve all books...
  ->filterByPublishYear(2009) // ... published in 2009
  ->orderByTitle()            // ... ordered by title
  ->joinWith('Book.Author')   // ... with their author
  ->find();

So I see nothing strange in the way CI's Active Record is implemented or structured. If your code is not running, please give more details, like error codes and so on, so we can actually understand why it's not working.

IMPORTANT:

I just read the changelog from the latest version; try downloading it (version 2.10), as they state:

Added a PDO driver to the Database driver.

Upvotes: 2

landons
landons

Reputation: 9547

You're not gaining anything by using CI's DB class if you're not using ActiveRecord or the other functions. Why not just use PDO natively?

By the way, you could do the exact same thing this way:

$result = $this->db->
    select(array('column1', 'column2'))->
    where(array(
        'column1' => $this->input->get('value'),
        'column2' => $this->input->get('something')
    ))->get('table')->result();

print_r($result);

Upvotes: 0

Related Questions