Welcome Guest, Not a member yet? Register   Sign In
Call to a member function get() on a non-object
#1

[eluser]notebook[/eluser]
Hi

i am trying to use code from the book "Codeigniter 1.7" by Jose Argudo; David Upton.Please help.

It is giving me this error :

A PHP Error was encountered
Severity: Notice

Message: Undefined property: Sites_model::$db

Filename: models/sites_model.php

Line Number: 11

Fatal error: Call to a member function get() on a non-object in C:\wamp\www\codeig\system\application\models\sites_model.php on line 11

Controller (site.php)

Code:
<?php

class Sites extends Controller
{
      function Sites()
      {
        parent:: Controller( ) ;
      }
    
      
      function index( )
    {
      //First we load the library and the model
      $this->load->library('table') ;
      $this->load->model('sites_model') ;
      //Then we call our model's get_sites function
      
    $sites = $this->sites_model->get_sites( );
      //If it returns some results we continue
      if ($sites->num_rows( ) >0)  
      {
            //Prepare the array that will contain the data
        $table = array( );
         //Prepare the table headers , one column for each table field
         $table[ ] = array( 'id' , 'name' , 'url' , 'un' , 'pw',
                      'client1' , 'client2' , 'admin1',
                      'admin2' , 'domainid' , 'hostid',
                      'webroot' , 'files' , 'filesdate',
                      'lastupdate' , 'submit');
            //Then we loop through our query results
            foreach($sites->result( ) as $row)
            {
                    //For each one of the results we create an array of values , one
                   //for each field , that will be under our table headers
                                   $table[ ] = array ($row->id ,$row->name ,$row->url,
                                $row->un ,$row->pw ,$row->client1,$row->client2 ,$row->admin1 ,$row->admin2,$row->domainid ,$row->hostid,
                                $row->webroot,$row->files, $row->filesdate, $row->lastupdate ,$row->submit);
            }    
        //Next step is to place our created array into a new array  
         //variable , one that we are sending to the view.
        $data ['sites'] = $table;  
        }  
        //Also we put into that array another variable ,  "heading" that we  
           //will use to echo the heading title

         $data['heading'] = "Sites admin";
        
        //Now we are prepared to call the view passing all the necessary variables
        //inside the $data array
        
        $this->load->view('sites/index',$data);
    }
    
    }

Model (sites_model.php)

Code:
<?php
class Sites_model extends Model
{
        function sites_model()
        {
            parent::Model();
        }
        
      function get_sites()
      {
        $query = $this->db->get('sites');
        return $query;
  }
}
?>

View (/views/sites/index.php)

Code:
<?php
//First we echo the $heading variable
echo " <h1>".$heading."</h1>" ;
//And if the $site variable is not empty we echo it' s content by  
//using the generate method of the table class / library
if( !empty($sites) ) echo $this->table->generate($sites) ;
?&gt;
#2

[eluser]WanWizard[/eluser]
Active record enabled in your database configuration?
#3

[eluser]notebook[/eluser]
Hi

Wanwizard

Yes they are enabled.

$active_record = TRUE;
#4

[eluser]Dennis Rasmussen[/eluser]
Database library loaded?
#5

[eluser]notebook[/eluser]
[quote author="Dennis Rasmussen" date="1286553333"]Database library loaded?[/quote]

Thanks Dennis for the quick help ,
i have now loaded database in model and it's working now.
Code:
&lt;?php
class Sites_model extends Model
{
        function sites_model()
        {
            parent::Model();
        }
        
      function get_sites()
      {
          $this->load->database();
        $query = $this->db->get('sites');
        return $query;
  }
}
?&gt;
#6

[eluser]Bas Vermeulen[/eluser]
If you gonna need the db regularly for your application I'd advice to auto load it:

/application/config/autoload.php
Code:
$autoload['libraries'] = array('database');
#7

[eluser]CappY[/eluser]
Can any1 help me with similar problem ? Currentry Im trying to rewrite my site on CodeIgniter but I dont really know OOP and im trying to learn it. Smile

Fatal error: Call to a member function row() on a non-object in /var/www/test/htdocs/application/views/stats/artist_view.php on line 3

Controller:
Code:
function artist()
    {
        $artist = $this->uri->segment(3);
        $from = $this->uri->segment(4);
        if($from == NULL) $from = 0;
        $to = $from + 30;
        $this->load->model('stats_model','',true);
        $data['query'] = $this->stats_model->getArtist($artist, $from, $to);
        $data['title'] = 'Artist - '.$artist;
        $this->load->vars($data);
        // Loading View !
        $data['view'] = 'stats/artist_view';
        $this->load->view('template', $data);
    }

Model:
Code:
function getArtist($artist, $from, $to)
    {
        $query = $this->db->query('SELECT `artist`,`title`,COUNT(title) AS `played` FROM `songs` WHERE `artist`=".$artist." GROUP BY `title` ORDER BY COUNT(title) DESC LIMIT '.$from.' , '.$to);
        return $query->result();
    }

view:
Code:
$row = $query->row();
$last_fm_artist = urlencode($row->artist);
....
#8

[eluser]Bas Vermeulen[/eluser]
You should return $query->row() instead of $query->result() in your Model because now you are doing DB class work in your view, which isn't working.
#9

[eluser]CappY[/eluser]
[quote author="Bas Vermeulen" date="1296921345"]You should return $query->row() instead of $query->result() in your Model because now you are doing DB class work in your view, which isn't working.[/quote]
I get it, thanks. Smile Im using just return $query; then in view Im generating result. $query->result(); and etc. Smile




Theme © iAndrew 2016 - Forum software by © MyBB