12 Replies - 1525 Views - Last Post: 08 August 2014 - 04:30 AM Rate Topic: -----

#1 murume   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 21-June 11

Codeigniter's $this->db works in a controller and not in

Posted 29 July 2014 - 12:40 AM

Im building a website on Codeigniter2 and Smarty3 and I am loving it so far. I have met two problems that I'm failing to solve:
Problem 1. I'm autoloading the database library but I keep getting a fatal non-object error when I try to use any method in $this->db in a model.
My model looks like this:
<?php
class Membership extends CI_Model{
function pindai(){
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$ismember = $this->db->get('members');

if ($ismember) {
return TRUE;
}
}
}

The funny thing is that the function pindai() works perfectly when I put it in a controller. Currently I have all my database calls in controllers and Im not comfortable with that. May somebody tell me where I am getting it wrong?

Problem 2. I am trying to remove the index.php in addresses using mod_rewrite. I tried using the .htaccess they give in the userguide but it did not work. I get "Internal Server Error". I went to their wiki on Github and got a different one. I changed the RewriteBase to /mycifolder/. When I visit the site all links are now taking me to my server's document root. I've watched tutorials and I see others getting the job done using the same file that is not working for me. What could be the problem?
I am using WampServer2.2

Is This A Good Question/Topic? 0
  • +

Replies To: Codeigniter's $this->db works in a controller and not in

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: Codeigniter's $this->db works in a controller and not in

Posted 29 July 2014 - 03:21 AM

Problem 1) it looks like CodeIgniter only defines database objects in a controller. on the other-hand-side, why should a database be available in a model?

Problem 2) what addresses?
Was This Post Helpful? 1
  • +
  • -

#3 murume   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 21-June 11

Re: Codeigniter's $this->db works in a controller and not in

Posted 29 July 2014 - 04:34 AM

Quote

why should a database be available in a model?

Correct me if Im wrong I think that's what models do. They handle all things database

Quote

what addresses

Sorry I mean urls
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3150
  • View blog
  • Posts: 11,955
  • Joined: 03-December 12

Re: Codeigniter's $this->db works in a controller and not in

Posted 29 July 2014 - 05:31 AM

The model handles the business logic of the application primarily. It does deal with the database, but not the connection itself. The actual connection is handled elsewhere, a database class for instance. An example model:

Spoiler


This is a model I am working with, other than the framework is Yii the premise is the same.
Was This Post Helpful? 1
  • +
  • -

#5 murume   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 21-June 11

Re: Codeigniter's $this->db works in a controller and not in

Posted 29 July 2014 - 06:17 AM

Thanks for your input and thanks for reminding me that I also need to try Yii before I settle on Codeigniter
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Codeigniter's $this->db works in a controller and not in

Posted 29 July 2014 - 08:47 AM

CodeIgniter is all but dead.

Its spiritual successor -- and current darling of many in the framework set -- is Laravel.
Was This Post Helpful? 0
  • +
  • -

#7 murume   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 21-June 11

Re: Codeigniter's $this->db works in a controller and not in

Posted 29 July 2014 - 04:54 PM

I've heard so much noise about Laravel but this whole Composer business just turned me off. I will force myself to like the console and try it

My apologies for that truncated thread title
Was This Post Helpful? 0
  • +
  • -

#8 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3150
  • View blog
  • Posts: 11,955
  • Joined: 03-December 12

Re: Codeigniter's $this->db works in a controller and not in

Posted 29 July 2014 - 06:21 PM

It seems like I'm pushing the product now but,

I'm evaluating Zends new IDE Zend Studio. And so far it is amazing with what it offers, native mobile development, built in composer integration, as well as Ant, PHPUnit, and a host of other things. It's extended eclipse making it already familiar. The actual product is not free, but for what you get it is an awesome IDE.
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: Codeigniter's $this->db works in a controller and not in

Posted 30 July 2014 - 12:20 AM

View Postmurume, on 30 July 2014 - 01:54 AM, said:

I've heard so much noise about Laravel but this whole Composer business just turned me off.

what does the one have to do with the other? besides, without that composer stuff, Node wouldn’t be where it is without npm (which is the javascript equivalent of composer)
Was This Post Helpful? 1
  • +
  • -

#10 murume   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 50
  • Joined: 21-June 11

Re: Codeigniter's $this->db works in a controller and not in

Posted 06 August 2014 - 11:38 PM

Quote

what does the one have to do with the other?

I believe you need composer to install Laravel. Am I wrong?
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: Codeigniter's $this->db works in a controller and not in

Posted 07 August 2014 - 02:50 AM

they only list that one possibility. but nowadays I’d prefer composer installs anyways.
Was This Post Helpful? 0
  • +
  • -

#12 Slice   User is offline

  • sudo pacman -S moneyz


Reputation: 253
  • View blog
  • Posts: 762
  • Joined: 24-November 08

Re: Codeigniter's $this->db works in a controller and not in

Posted 08 August 2014 - 01:52 AM

View Postastonecipher, on 30 July 2014 - 02:21 AM, said:

It seems like I'm pushing the product now but,

I'm evaluating Zends new IDE Zend Studio. And so far it is amazing with what it offers, native mobile development, built in composer integration, as well as Ant, PHPUnit, and a host of other things. It's extended eclipse making it already familiar. The actual product is not free, but for what you get it is an awesome IDE.


Have you tried the 30 day free trial? I went with it as we started a Zend project at work but fell back to netbeans before the 30 days was up. Considering it was specifically designed to run with zend, the "Create Zend Module" function would always throw an error when trying to use it with a remote server, yet the netbeans plugin for doing it worked ok. (When I say ok; the plugin can't auto fill your controller/model info but it only takes 5 minutes to do that anyway. It just generally creates the folder structure for you.)
Was This Post Helpful? 0
  • +
  • -

#13 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3150
  • View blog
  • Posts: 11,955
  • Joined: 03-December 12

Re: Codeigniter's $this->db works in a controller and not in

Posted 08 August 2014 - 04:30 AM

I'm using the trial for the evaluation. I am going to buy it. The setup in a pain, but it handles more than Zend. It's built off of eclipse.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1