How to Get Started with Cortex: A Data-Mapper for the PHP Fat-Free Framework

Apr 27, 2022 | Programming

Cortex is a powerful and versatile Data Mapper designed for the PHP Fat-Free Framework. If you’re looking to streamline the management of databases such as SQL, Jig, or MongoDB, this guide will walk you through getting started with Cortex, highlighting its features and addressing common troubleshooting issues.

Table of Contents

Quick Start

Before diving into using Cortex, ensure your system meets the requirements. Cortex requires at least version 3.4 of Fat-Free and PHP 5.4.

Installation

To install Cortex, you can either manually copy the `libdbcortex.php` file to your `libs` directory or use Composer by executing the command:

composer require ikkezf3-cortex:1.*

Setting Up Your Database

Next, create a database object depending on your choice of database engine:

$db = new DBSQL('mysql:host=localhost;port=3306;dbname=MyAppDB,user,pw');

Repeat this for other databases like SQLite, PostgreSQL, or MongoDB as needed.

SQL Fluid Mode

The SQL Fluid Mode allows you to work without a fixed table schema. When this mode is enabled, Cortex will automatically create necessary database tables and columns based on the data you input.

$user = new DBCortex($db, 'users', TRUE);

Use this mode for rapid prototyping but remember to disable it when done to enhance performance.

Cortex Models

Creating models with Cortex is simple. Models can be defined by extending the DBCortex class.

namespace Model;
class User extends DBCortex {
    protected $fieldConf = [...]; // your field configuration here
    protected $db = 'AppDB1'; // the associated DB
    protected $table = 'users'; // the table name
}

This setup will allow for much flexibility when interacting with your database.

Relations

Cortex allows the establishment of relationships between models. These relationships can be one-to-one, one-to-many, or many-to-many. Here’s how you can set up a relation between a News model and an Author model:

class News extends DBCortex {
    protected $fields = [
        'author' => ['belongs-to-one' => ModelAuthor::class]
    ];
}

Event Handlers

Event handlers in Cortex enable the execution of additional code before or after certain actions. You can define custom field handlers using the following syntax:

$mapper->onload(function($self) {
    // Your custom code here
});

Filter Query Syntax

Cortex supports SQL-like queries for filtering data. Here are some basic examples:

$user = new DBCortex($db, 'users');
$user->load(['email = ?', 'example@example.com']);

This simplicity is a big advantage over more complex query frameworks.

Troubleshooting

If you encounter any issues during installation or while using Cortex, consider the following troubleshooting tips:

  • Ensure all dependencies are correctly installed and the correct versions of Fat-Free Framework and PHP are being used.
  • Check database connection parameters in your DB setup code for accuracy.
  • If tables are not created as expected, ensure Fluid Mode is enabled, and that sample data has been saved.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

Cortex offers a powerful and flexible solution for managing database interactions seamlessly. Whether you’re starting with a new project or enhancing an existing one, its features allow you to define, manipulate, and relate database records easily.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox