Welcome Guest, Not a member yet? Register   Sign In
Using an own Class (= Library)
#1

[eluser]dsentker[/eluser]
Hey there,

i've got an own Database class (e.g. to return last 5 DB Entrys etc.). In a non-CI-Framework Project i would use my class like that:

Code:
$mydb = new MyDBClass;
$mydb->connect();
$mydb->returnlastentrys(5);
//etc.

This Class is stored in mydb.class.php.

I've read in the user documentation, that i can implement my own class with the Library feature:

Code:
$this->load->library('mydb.class');

and now i can act with my methods like that:

Code:
$this->mydb.class->connect();
//etc.

Is that right?

Next Q: The Connect Method, for example, is a method where is no need to load it multiple. If the method was started once, its not essential to start it twice.

Where do i have to place the
Code:
$this->mydb.class->connect();
//etc.
? Do i have to place it in the constructor of the Controller? That could be annoying, if i have many Controllers, and every Controller needs a Connection to the DB. Can i check if a method was called before? Like that:

Code:
$this->mydb.class->connect(); //returns true, connection is built now
// any other code and another Controller...
// [...]
if($this->mydb.class->connect() == TRUE) {
   echo 'Connection is already there!';
} else {
   echo 'Not connected - trying to connect...';
   $this->mydb.class->connect();
}
#2

[eluser]Johan André[/eluser]
I might be wrong but I don't think the classnames can have dots in them...
#3

[eluser]dsentker[/eluser]
you're right, sounds logical. But how can i prevent the multiple DB Connections (last Q.) ? And where can i place my $this->mydbclass->connect(); ? I don't want to load it manually to every Controller's constructor...
#4

[eluser]LuckyFella73[/eluser]
hi dsentker,

[quote author="dsentker" date="1262571351"]you're right, sounds logical. But how can i prevent the multiple DB Connections (last Q.) ? And where can i place my $this->mydbclass->connect(); ? I don't want to load it manually to every Controller's constructor...[/quote]

There is a file called autoload.php in "system/application/config/" where
you can define the database class to be loaded in every controller.

I recommend you to have a look at the user guide and get a bit more familiar with
CI classes and models otherwise you will and up scripting a lot of stuff that's
allready available! CI has many very useful libraries which are easy to use!

You don't have to care about connecting to your db when using the db class like
you use to do scripting "plain" PHP. Just edit the database.php file ("system/application/config/"),
(auto-)load the db class and do your queries (using models and the active record class - if possible).




Theme © iAndrew 2016 - Forum software by © MyBB