[ 'type' => Schema::DT_BOOL, 'nullable' => false, 'default' => 1, 'index' => true ], 'userId' => [ 'type' => Schema::DT_INT, 'index' => true, 'belongs-to-one' => 'Model\UserModel', 'constraint' => [ [ 'table' => 'user', 'on-delete' => 'CASCADE' ] ] ], 'characterId' => [ 'type' => Schema::DT_INT, 'index' => true, 'unique' => true, 'belongs-to-one' => 'Model\CharacterModel', 'constraint' => [ [ 'table' => 'character', 'on-delete' => 'CASCADE' ] ] ] ]; /** * set an array with all data for a character * @param $characterData */ public function setData($characterData){ foreach((array)$characterData as $key => $value){ if(!is_array($value)){ if($this->exists($key)){ $this->$key = $value; } } } } /** * event "Hook" * -> remove user if there are no other characters bound to this user * @param UserCharacterModel $self * @param $pkeys */ public function afterEraseEvent($self, $pkeys){ if( is_object($self->userId) && is_null($self->userId->userCharacters) ){ $self->userId->erase(); } } /** * get the character model of this character * @return mixed */ public function getCharacter(){ return $this->characterId; } /** * overwrites parent * @param null $db * @param null $table * @param null $fields * @return bool */ public static function setup($db=null, $table=null, $fields=null){ $status = parent::setup($db,$table,$fields); if($status === true){ $status = parent::setMultiColumnIndex(['userId', 'characterId'], true); } return $status; } }