Welcome Guest, Not a member yet? Register   Sign In
Active Record Class -> question
#1

[eluser]seba22[/eluser]
Hello,

Today, i found in codeigniter documentation function Active Record Class
:
Code:
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();

My question is, what if i write something like above and now i want change it to

Code:
$this->db->select('*');
$this->db->from('blogs');
$query2 = $this->db->get();



I remove this "join" cause.

Code:
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();

$this->db->select('*');
$this->db->from('blogs');
$query2 = $this->db->get();

But what with previous "join", how clean $this->db and start from beginning, make sure there is nothing "set" before... (for example join, like, where, or where ?

How make sure that query2, don't have "join".

regards
#2

[eluser]PhilTem[/eluser]
The database driver automatically cleans everything you did after you do a get() i.e. there are no more 'where', 'like', or other things Wink
#3

[eluser]seba22[/eluser]
Hello,
Thank You for reply,

Hmm, sounds good, but i'm still worried.

I'm just afraid of that kinds of hacks.

I will show You and example what im talking about.

Few months ago, i was doing a project (quite large) many functions, class, etc... and i got error.

The was actually no error, but inserted data in mysql went crazy.
The user_id of creator of insert was from outer space... just like magic puff 123312 <- no representation in real live.
It was blowing my head, i spend few days!!! to figure out what's happened ( i was trying show error to 0 ect) nothing.

The answer was, $dbh (PDO connection) and last_inset_id().


I got part of code and inside something like this

//
$dbh -- insert into XYZ bla bla

if(dbh last insert id > 0) { do more stuff }

Inset fail, because of duplicate key error... but last insert id was containing last insert id, before that above insert what fail ;-) Something from previous... from example from inserting logs...

That was the day, when i go very very carefully when i'm doing something with common data...


I'm afraid with Active Class Record for example, by my mistake i will have few function and inside one i will have if(something) {$this->db->from('blogs');} and it will be hard to debug... I know i can search mysql logs, and then search source for that string but... it will be easier to just write db->clean();


I had same issue with "phpmailer" ;-)
But there was option $phpmailer->ClearAddress(); what remove everything previously added...


So last question, is driver data will be clear if query fail (maybe by syntax, or by dropped connection by mysql) ? <- i will try double check my code, but what about that kind of situation what can be caused not by my fault ?

Regards



#4

[eluser]summanerd[/eluser]
I understand your concern, but was that last DB issue encountered while using the Active Records Class? I highly doubt it. The advantage of using a framework is that they they take care of all the little details. The situation you describe I've encountered numerous time with no problem.

Also, take a look at CI's own example of using two queries back to back, like PhilTem said, the get clears out all clauses.

http://ellislab.com/codeigniter/user-gui...sults.html ( go to the bottom of this page, the free_results example)
#5

[eluser]seba22[/eluser]
Hello,

Sounds good.

From yesterday i'm playing with this active record...
I must admit, this is the best thing i found from few years.

Previously i was charmed by php PDO, but this make live easier... just this stupid things what You have to do many times in small functions, get where, etc... it's great...


I hope this will last question:
Code:
if($this->db->count_all_results()>0)
{
    return true;
}
else
{
    return false;
}

Does "Count_all" clean "where" ?
Or "Where" etc is cleared only by $this->db->get(): ? ?
#6

[eluser]summanerd[/eluser]
Yes it will. The key thing to understand is not what method has to be called, but what action has to be completed for the query to be reset. In this case a query must be executed. So whenever you call a method that runs the query against the DB, it's reset.

In your example the query is executed so that you can get a count; therefore once complete it resets the query.

Hope this helps




Theme © iAndrew 2016 - Forum software by © MyBB