Databases in Wayfinder

Wayfinder is setup to work with MySQL out of the box. To get started, just ammend the configuration in the app/conf/db.php file so that it points to your database.

You will need to load the DB from the libraries folder and into your Class, like so:

class Items extends Wayfinder {

    private $_db;

    public function __construct() {
        $this->load('libraries', 'db');
        $this->_db = new Db();
    }

}

Once your database connection has been established, you can start working with the helper methods supplied.

query()

The query() method is a wrapper for running any type of query.

public function addItem($name) {
    $sql = "SELECT * FROM myTable LIMIT 10";
    $results = $this->_db->query($sql);
}

escape()

escape() is a wrapper for PHP's own mysqli_real_escape_string method. You should escape your own inputs, especially when the content is user generated. You can call it when constructing your queries like so:

public function addItem($name) {
    $sql = "INSERT INTO myTable (column1, column2)
            VALUES ('myFirstVal', ".$this->_db->escape($_POST['username']).")";
    $this->_db->query($sql);
    return $this->_db->lastInsert();
}

lastInsertId()

After executing a query, you can fetch the ID generated by the last insert.

public function addItem($name) {
    // generate query
    // $sql = "INSERT ...";
    $this->_db->query($sql);
    return $this->_db->lastInsert();
}

error()

Use this method to return any errors related to queries that fail to execute.