Part of the EllisLab Network
   
 
Using IN with ActiveRecord
Posted: 05 June 2007 09:55 AM   [ Ignore ]  
Summer Student
Total Posts:  7
Joined  05-29-2007

Hi there,

I’m having a little problem with ActiveRecord. I want to run a query like this:

DELETE FROM ci_news WHERE id IN (1, 2, 3);

I know you can write your own query and just run

$this->db->query();

method, but then you have a problem with the table’s prefix.

My questions are:

1. Is there an implementation of something like wherein() method in the ActiveRecord class?
2. If not, how can I access the database config to get the prefix? I tried using

$this->load->config('database')

but it returned an error like “Your database.php file does not appear to contain a valid configuration array.”, even though it works just fine through the entire application.

Thanks in advance.

Profile
 
 
Posted: 05 June 2007 11:17 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
RankRank
Total Posts:  166
Joined  02-06-2006

Is there an implementation of something like wherein() method in the ActiveRecord class?

Not as far as I know.

I don’t really see what would prevent you using:

$this->db->query("DELETE FROM ci_news WHERE id IN(1,2,3)");

Or perhaps:

$this->db->where('id IN', '(1,2,3)');

Might work with delete() if you would rather not user query().

I’m not sure if this will help - I hope it does.

 Signature 

My Blog, I work at Erskine Design

Profile
 
 
Posted: 05 June 2007 11:59 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  7
Joined  05-29-2007
gms - 05 June 2007 11:17 AM

Is there an implementation of something like wherein() method in the ActiveRecord class?

Not as far as I know.

I don’t really see what would prevent you using:

$this->db->query("DELETE FROM ci_news WHERE id IN(1,2,3)");

Or perhaps:

$this->db->where('id IN', '(1,2,3)');

Might work with delete() if you would rather not user query().

I’m not sure if this will help - I hope it does.

Actually, I have already tried that second one, but it results in a query like this:

... WHERE id IN '(1, 2, 3)'

which is not a valid query because of the quotes.

Profile
 
 
Posted: 05 June 2007 03:09 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
RankRank
Total Posts:  166
Joined  02-06-2006

Of course, it quotes it becuase it thinks its a string, I don’t really know why I thought that might work. - that’s just the problem with active record I guess.

I’ve tried a few different things but to no avail - I’d go with the query() option to be honest if you know SQL there are not many reasons to use active record. Disabling it may also improve performance.smile

 Signature 

My Blog, I work at Erskine Design

Profile
 
 
Posted: 11 June 2007 01:48 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  74
Joined  12-20-2006
gms - 05 June 2007 03:09 PM

Of course, it quotes it becuase it thinks its a string, I don’t really know why I thought that might work. - that’s just the problem with active record I guess.

I’ve tried a few different things but to no avail - I’d go with the query() option to be honest if you know SQL there are not many reasons to use active record. Disabling it may also improve performance.smile

Besides to use the table prefix as he stated above rolleyes.

I’m guessing it is possible to pull the prefix from the configuration… Does anyone know how?

Profile
 
 
Posted: 11 June 2007 03:48 AM   [ Ignore ]   [ # 5 ]  
Lab Assistant
RankRank
Total Posts:  166
Joined  02-06-2006

To get the prefix you can use

$this->db->dbprefix

Run print_r($this->db) in your controller, you will see all db config parameters.

I think that is what you were asking?

 Signature 

My Blog, I work at Erskine Design

Profile
 
 
Posted: 11 June 2007 06:08 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  7
Joined  05-29-2007

The second one, yes. Thanks!

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: 64450 Total Logged-in Users: 29
Total Topics: 80954 Total Anonymous Users: 0
Total Replies: 435673 Total Guests: 184
Total Posts: 516627    
Members ( View Memberlist )