Creating a Library - Part 2


Posted by Tuts Admin on 2018-07-26

Contributions

 

Want to be a contributing author and help grow the CodeIgniter community?

Register and account and use the contact form and drop us a note.

Donations

 

We appreciate any donation you're willing to send.

Visit the donations page

Donations collected are used to pay for hosting, domain names, further development of the website, and gives me a little more time to write tutorials.

Subscribe

Get an email when new content is added.


0 v4-dev Libraries Beginner

Creating a Library - Part 2 - Connecting to CodeIgniter core functionality.

Notice: CodeIgniter 4 is currently in development as of the writing of this article. This article is aimed at beginners.

In the last article we learned how to create our spiffy new library. In this article we'll learn how to connect and use some of CodeIgniter4's core features, such as helpers and the database driver.

Here's what we left off with in the last article.

class MyLib {

                    // define empty property
        public $var1;

                    // Assign a default value
        public $var2 = "HI"; 

        public function returnVarOne()
        {
            return $this->var1;
        }

}

To keep things tidy, we'll add a few more class properties so they're fully available to the class and it's methods every time it runs.

// property for our database connection
public $db;

// property for our query builder
public $builder;

// set the table
protected $table = 'mytable';

Not too bad so far. We're using the database, so like with CodeIgniter 3, make sure you've edited the database configuration file. It's now located in application/Config/Database.php.

In the last article I mentioned there's a few times when the App\(namespace) is different... here's one of those changes. The database configuration lives in the Config namespace (notice the lack of App\) and is always available, so you don't see to use it when creating your class.

Now let's add the __contruct() method so we can initialize our database and Query Builder.

public function __construct($config = array())
{
    log_message('debug', 'MyLib Class Initialized');

    // instantiate the database connection
    $this->db = \Config\Database::connect();

    // set the table and create the query builder instance
    $this->builder = $this->db->table($this->table);

}

A few new things happened pretty quickly here.

  • We used the always present log_message() function
  • We instantiated the database so we can use it.
  • We instantiated the Query Builder with the table we'll be using

So, now we have access to the database and the query builder.

Still with me? Great!

Above we've used $this->builder as class-wide access to a single table in the database, but you can utilize several class-wide or method specific tables if you want, you're not restricted to a single table in a single class.

Let's use this database and builder connection now and see how to use it. I won't be going too in-depth just some quick, easy code to get the general point across.

Let's have 2 methods, the first will be to get all records from the table and one will be to get a specific record from the table.

public function getAll()
{
  // because we've already defind the table, all we have to do now is 'get()' all records
  $q = $this->builder->get();

  // get the result object and return it.
  return $q->getResult();     
}


public function getOne($id)
{
  // use the where() clause to limit the search 
  $q = $this->builder->where('id', $id)->get();

  // since we're only returning 1 row this time, we'll use getRow()
  return $q->getRow();
}

Pretty cool, huh? Those of you reading this are already familiar with CodeIgniter 3 will recognize that the getResult() and getRow() is a little different, but overall, still easy enough to follow.

That's it for this time. In the next article on creating libraries we'll venture into loading and using your spiffy new library.

Further Reading: * Connect to the Database * Query Builder * Getting Results