Problems with configuring join relation in Cortex

120 views
Skip to first unread message

Yoeri Nijs

unread,
May 1, 2018, 4:04:17 PM5/1/18
to Fat-Free Framework
Hi all,

Currently, I am discovering the wonderful world of the F3 plugin Cortex. I like it a lot, but I am having troubles with configuring joins with it. Mainly this is because this is my first time I am working Cortex and PHP in a relative big personal project and I lack some experience on this matter.

At the moment, I have two models: User and Role. As you can imagine, in my application, an user would have one role. I was following the Cortex documentation and created this:

// User

<?php

class User extends \DB\Cortex {

    protected
        $fieldConf = array(
            'username' => array(
                'type' => 'string',
                'nullable' => false,
            ),
            'firstname' => array(
                'type' => 'string',
                'nullable' => false,
            ),
            'lastname' => array(
                'type' => 'string',
                'nullable' => false,
            ),
            'email' => array(
                'type' => 'string',
                'nullable' => false,
            ),
            'password' => array(
                'type' => 'string',
                'nullable' => false,
            ),
            'role' => array(
                'has-one' => '\Role' // tried array('\Role', 'users') here, but that was causing 'Invalid value for many field "users". Expecting null, split-able string, hydrated mapper object, or array of mapper objects.'
            ),
            'created_at' => array(
                'type' => 'timestamp',
                'nullable' => false,
            ),
            'updated_at' => array(
                'type' => 'timestamp',
                'nullable' => false,
            ),
            'ip' => array(
                'type' => 'string',
                'nullable' => true,
            )
        ),
        $db = 'DB',
        $table = 'users',
        $fluid = true,      // triggers the SQL Fluid Mode, default: false
        $primary = 'id',    // name of the primary key (auto-created), default: id
        $ttl = 120,         // caching time of field schema, default: 60
        $rel_ttl = 30;      // caching time of all relations, default: 0

        public function all() {
            $this->load();
            return $this->query;
        }

        public function getById($id) {
            $this->load(array('id=?', $id));
            return $this->query;
        }

        public function getByUsername($username) {
            $this->load(array('username=?', $username));
            return $this->query;
        }

        public function getByFirstname($firstname) {
            $this->load(array('firstname=?', $firstname));
            return $this->query;
        }

        public function getByLastname($lastname) {
            $this->load(array('lastname=?', $lastname));
            return $this->query;
        }

        public function getByEmail($email) {
            $this->load(array('email=?', $email));
            return $this->query;
        }
}

// Role
<?php

class Role extends \DB\Cortex {

    protected
        $fieldConf = array(
            'name' => array(
                'type' => 'string',
                'nullable' => false,
            ),
            'users' => array(
                'belongs-to-many' => '\User'
            )
        ),
        $db = 'DB',
        $table = 'roles',
        $fluid = true,      // triggers the SQL Fluid Mode, default: false
        $primary = 'id',    // name of the primary key (auto-created), default: id
        $ttl = 120,         // caching time of field schema, default: 60
        $rel_ttl = 30;      // caching time of all relations, default: 0

    public function all() {
        $this->load();
        return $this->query;
    }

    public function getById($id) {
        $this->load(array('id=?', $id));
        return $this->query;
    }

    public function getByName($name) {
        $this->load(array('name=?', $name));
        return $this->query;
    }
}

With Phinx, I seed my database initially. I have two seeds: one for the users, and one for the roles. Prior to running F3 I have filled the database with fake data.




After f3->run(), I assign a role to a user, just for testing purposes. This looks like:

$aRole= new Role();
$aRole->getById(1);

$user = new User();
$user->getById(1);
$user->role = $aRole;
$user->save();

Now, my database is looking like the following. Please note that in users nothing has changed.



I strongly believe that I am doing something wrong with setting up the linkage, but I am not sure. Can someone help me?



reth

unread,
May 1, 2018, 8:53:30 PM5/1/18
to Fat-Free Framework
https://github.com/ikkez/f3-cortex#relations  Users is like newsmodel and roles is like authormodel.

In your usermodel 'has-one' should be 'belongs-to-one' and role 'belongs-to-many' should be 'has-many'. That works for me. 

This is my code which works for me. 

<?php
namespace Model;

class User extends \DB\Cortex
{

    protected $fieldConf = [
        'username' => [
            'type' => 'VARCHAR128',
            'nullable' => true
        ],
......
        'user_role' => [
            'has-many' => ['\Model\User_role', 'user_id']
        ]
    ],
.....
    $table = 'user';

}

?>
<?php

class User_role extends \DB\Cortex
{

    protected $fieldConf = [
        'user_id' => [
            'belongs-to-one' => '\Model\User'
        ],
        'role_id' => [
            'belongs-to-one' => '\Model\Role'
        ]
    ],
.....
    $table = 'user_role';

}
?>
<?php
namespace Model;

class Role extends \DB\Cortex
{

    protected $fieldConf = [
        'name' => [
            'type' => 'VARCHAR128',
            'nullable' => false
        ],
        'permission' => [
            'has-many' => ['\Model\Permission', 'role_id']
        ],
        'user_role' => [
            'has-many' => ['\Model\User_role', 'role_id']
        ]
    ],
.....
    $table = 'role';

}
?>







Op dinsdag 1 mei 2018 22:04:17 UTC+2 schreef Yoeri Nijs:

ikkez

unread,
May 2, 2018, 3:19:54 AM5/2/18
to Fat-Free Framework
Hi,..
well it creates those extra fields in your table, because you have fluid mode enabled. Since you have a schema defined already, just disable it ($fluid=false).

Then follow reth's recommendation.. I couldn't have any better example and it also allows multiple roles.

In your current setup however, users it should be

            'role' => array(
                'belongs-to-one' => '\Role'
            ),

and in Roles, it's:


            'users' => array(
                'has-many' => ['\User','role']
            )

Yoeri Nijs

unread,
May 2, 2018, 3:23:53 PM5/2/18
to Fat-Free Framework
Hi,

Well, it works like a charm! Due to possible future performance issues, I added a linking table between User and Role, as reth suggested ;)

Thanks for the help. In my opinion, Cortex is a fantastic piece of code!

Op woensdag 2 mei 2018 09:19:54 UTC+2 schreef ikkez:
Reply all
Reply to author
Forward
0 new messages