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?