Welcome Guest, Not a member yet? Register   Sign In
Fatal Memory Error
#11

[eluser]iFadey[/eluser]
<b>Users model class continued...</b>

Code:
function user_exists( $username ) {
        $query = 'SELECT username FROM users WHERE username = ? LIMIT 1';
        
        $username = strtolower( xss_clean( trim( $username ) ) );
        
        $results = $this->db->query( $query, array( $username ) );
        $data = $results->num_rows();
        
        $results->free_result();
        return $data;
    }
    
    
    function email_exists( $email ) {
        $query = 'SELECT email FROM users WHERE email = ? LIMIT 1';
        
        $email = strtolower( xss_clean( trim( $email ) ) );
        
        $results = $this->db->query( $query, array( $email ) );
        $data = $results->num_rows();
        
        $results->free_result();
        return $data;
    }
    
    
    function getUsernameByEmail( $email ) {
        $query = 'SELECT username FROM users WHERE email = ? LIMIT 1';
        
        $results = $this->db->query( $query, array( $email ) );
        $data = $results->row_array();
        
        $results->free_result();
        return $data[ 'username' ];
    }
    
    
    function getImageByUsername( $username ) {
        $query = 'SELECT image FROM users WHERE username = ? LIMIT 1';
        
        $results = $this->db->query( $query, array( $username ) );
        $data = $results->row_array();
        
        $results->free_result();
        return $data[ 'image' ];
    }
    
    
    function getUserRecordByUsername( $username, $display_profile = 0 ) {
        $username = xss_clean( substr( $username, 0, 20 ) );
        
        if( empty( $display_profile ) )
            $query = 'SELECT * FROM users WHERE username = ? LIMIT 1';
        else
            $query = 'SELECT * FROM users WHERE username = ? AND display_profile = 1 LIMIT 1';
        
        $results = $this->db->query( $query, array( $username ) );
        $data = $results->row_array();
        
        $data[ 'articles_written' ] = $this->BlogPosts->countTotalUserArticles( $data[ 'username' ] );
        
        $results->free_result();
        return $data;
    }

} //end Users class
#12

[eluser]iFadey[/eluser]
This is the table structure of "posts" with which BlogPosts class is dealing
#13

[eluser]iFadey[/eluser]
This is table structure of "users"
#14

[eluser]rogierb[/eluser]
In BlogPosts you are loading
Code:
$this->load->model( 'Users' );
$this->load->model( 'Comments' );
$this->load->model( 'ColAuthors' );

and in Users you are loading
Code:
$this->load->model( 'BlogPosts' );

Normally the loader would throw an error when trying to load a model twice.
If it does not throw an error, it might be loading the models over and over again.

So try to remove the loading of blogpost.
#15

[eluser]iFadey[/eluser]
Now I removed all those model loading statements from the constructor and placed them in those methods who only require them like this:

Code:
$CI = get_instance();
$CI->load->model( 'myModel' );

//blah blah...
$CI->myModel->method();

Thank God! It's working now without exhausting complete memory. I check the memory usage and now it's only 2.35MB.

Thank you to both of you!
#16

[eluser]BrianDHall[/eluser]
Also take care in your model constructors with loading the database. I have found I use the database so much, for sessions and such, that it makes more sense to just autoload the database. Then you don't have to worry about making calls to load it.

For extra memory security in such instances, you might try something like this:

Code:
if (! is_set($CI->myModel))
{
   $CI->load->model( 'myModel' );
}

This way you guarantee you aren't loading something twice, leaving no chance things are being loaded more times than you want.

And as you code notes, in models be sure to make use of the CI super-object on loading so each model doesn't itself end up with its own sub-model and you end up with dozens of duplicates flying around.

Glad you got to the bottom of your problem!
#17

[eluser]iFadey[/eluser]
Hey Brian thank you so much. You gave some really good advices.
Yeah I am also glad that it's solved and thanks again to both of you because it won't be possible without your support :-)




Theme © iAndrew 2016 - Forum software by © MyBB