CodeIgniter Forums

Full Version: how to use database class in own library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]GSV[/eluser]
Hello all.

I need use database class in my own library. How to initialize it in library for accessing?


Sorry for my english Smile

El Forum

[eluser]coolfactor[/eluser]
Have you read the User Guide?

Same way you'd do it in your controller, except you first need to get an instance of your controller.

Code:
$CI =& get_instance();

Then just load and access your database library the same way you would elsewhere:
Code:
$CI->load->db();

$CI->db->query('Some query here');

El Forum

[eluser]GSV[/eluser]
[quote author="coolfactor" date="1188207195"]Have you read the User Guide?

Same way you'd do it in your controller, except you first need to get an instance of your controller.
[/quote]
Thank you for answer

Yes, I have read. But I have little special task.

controller:

Code:
$this->load->library('Search_Server');

library search_server.php :
Code:
class Search_Server extends HTML_AJAX_Server {
    var $initMethods = true;
    function initSearch() {
        include 'class.Search.php';
        $this->registerClass(new Search(), 'Search', array('validate'));
    }
}

$server = new Search_Server();
$server->handleRequest();

file class.Search.php :

Code:
class Search {
    var $CI;
    function Search() {}
    function validate($arrayForm) {
        $this->CI =& get_instance();
...

I need access to database class in my class Search()
In this case I have error: PHP Fatal error: Call to undefined function get_instance()
I think, it need some include.

El Forum

[eluser]Rick Jolly[/eluser]
Well, you have access to the CI instance in the library so you could pass it into the Search class by reference. Maybe this will work?:
Code:
class Search_Server extends HTML_AJAX_Server {
    var $initMethods = true;
    function initSearch() {
        include 'class.Search.php';
        $CI =& get_instance();
        $this->registerClass(new Search($CI), 'Search', array('validate'));
    }
}
Code:
class Search {
    var $CI;
    function Search(& $CI)
    {
       $this->CI =& $CI;
    }
    function validate($arrayForm) {

El Forum

[eluser]coolfactor[/eluser]
By the time the Search class is loaded and instantiated, the get_instance() function should be available. I'm not sure why it's not working for him. From what I see, he's taking a rather complicated approach to the problem.

GSV, please tell us what you want to do from the beginning, and maybe we can offer a different approach.

El Forum

[eluser]GSV[/eluser]
I need connect AJAX (prefer PEAR::HTML_AJAX) to the project.


Code:
ini_set("include_path", ".:../:./pear:../pear:/var/www/html/rmb/pear:/var/www/html/rmb/system/application/libraries");
include 'HTML/AJAX/Server.php';
include 'HTML/AJAX/Action.php';


class SearchServer {
    var $ci;
    function SearchServer(){
        $this->ci =& get_instance();
        $this->ci->load->database();
    }
}

class SS extends HTML_AJAX_Server {
    var $initMethods = true;
    function initSearch() {
        $this->registerClass(new Search(), 'Search', array('validate'));
    }
}

$server = new SS();
$server->handleRequest();

define('ERR_STATE_EMPTY', 'You forgot to select a state, please try again');
define('ERR_SEARCHWORD_INVALID', 'Correct yor query! Resonse returned empty result, please try again');

class Search {
    var $ci;
    function Search() {
        $this->ci = new SearchServer();
    }
    function validate($arrayForm) {
        $arrMessages = array();
        $arrValidated = array();
        $arrValidated['select_state'] = true;
        $arrValidated['search_word'] = true;
        $objForm = new stdClass();
        $objForm->select_state = trim($arrayForm['select_state']);
        $objForm->search_word = trim($arrayForm['search_word']);
        
        $objResponse = new HTML_AJAX_Action();
        
        //if ($strMessages == "") {
            $r = "Search result for word <b>'".$objForm->search_word."'</b><br> ";
            $objResponse->insertScript("document.getElementById('live_search').style.visibility='visible';");
//            $data = $this->_search_result($objForm->search_word, $objForm->select_state);
//            for ($i=0; $i < count($data); $i++){
//                $r .= "<a href='#'>" . $data[$i]['fname'] . " " . $data[$i]['lname'] . "</a><br>";
//            }

            $objResponse->assignAttr('live_search', 'innerHTML', $r);
        //}
        //else {
        //    $objResponse->insertAlert($strMessages);
        //}
        return $objResponse;
    }
}

class SearchServer works fine, but when I make object in Search::Search(), I have the same error Sad
PHP Fatal error: Call to undefined function get_instance()