hey why do I always get the following Error when i am trying to use the users db model in my mywebsockets controller…:
PHP Fatal error: Uncaught TypeError: rtrim() expects parameter 1 to be string, null given in /var/www/html///vendor/cakephp/cakephp/src/Core/App.php:63
Stack trace:
#0 /var/www/html///vendor/cakephp/cakephp/src/Core/App.php(63): rtrim()
#1 /var/www/html///vendor/cakephp/cakephp/src/ORM/Locator/TableLocator.php(291): Cake\Core\App::className()
#2 /var/www/html///vendor/cakephp/cakephp/src/ORM/Locator/TableLocator.php(227): Cake\ORM\Locator\TableLocator->_getClassName()
#3 /var/www/html///vendor/cakephp/cakephp/src/Datasource/Locator/AbstractLocator.php(62): Cake\ORM\Locator\TableLocator->createInstance()
#4 /var/www/html///vendor/cakephp/cakephp/src/ORM/Locator/TableLocator.php(205): Cake\Datasource\Locator\AbstractLocator->get()
#5 /var/www/html///src/Controller/MywebsocketsController.php(280): Cake\ORM\Locator\TableLocator->get()
#6 /var/www/html///vendor/cbod in /var/www/html///vendor/cakephp/cakephp/src/Core/App.php on line 63
i tried it with many ways:
$this->loadModel('Users');
$usersTable = TableRegistry::getTableLocator()->get('Users');
$user = $usersTable->get($uid, [
'contain' => ['Box'],
]);
$usersTable = \Cake\ORM\TableRegistry::getTableLocator()->get('Users', [
'className' => 'App\Model\Table\UsersTable'
]);
$usersTable = new UsersTable();
$user = $usersTable->get($uid, [
'contain' => ['Box'],
]);
but i always get the same error.
my websocketscontroller looks like:
<?php
declare(strict_types=1);
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* Mywebsockets Model
*
* @property \App\Model\Table\UsersTable&\Cake\ORM\Association\BelongsTo $Users
*
* @method \App\Model\Entity\Mywebsocket newEmptyEntity()
* @method \App\Model\Entity\Mywebsocket newEntity(array $data, array $options = [])
* @method \App\Model\Entity\Mywebsocket[] newEntities(array $data, array $options = [])
* @method \App\Model\Entity\Mywebsocket get($primaryKey, $options = [])
* @method \App\Model\Entity\Mywebsocket findOrCreate($search, ?callable $callback = null, $options = [])
* @method \App\Model\Entity\Mywebsocket patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method \App\Model\Entity\Mywebsocket[] patchEntities(iterable $entities, array $data, array $options = [])
* @method \App\Model\Entity\Mywebsocket|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\Mywebsocket saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
* @method \App\Model\Entity\Mywebsocket[]|\Cake\Datasource\ResultSetInterface|false saveMany(iterable $entities, $options = [])
* @method \App\Model\Entity\Mywebsocket[]|\Cake\Datasource\ResultSetInterface saveManyOrFail(iterable $entities, $options = [])
* @method \App\Model\Entity\Mywebsocket[]|\Cake\Datasource\ResultSetInterface|false deleteMany(iterable $entities, $options = [])
* @method \App\Model\Entity\Mywebsocket[]|\Cake\Datasource\ResultSetInterface deleteManyOrFail(iterable $entities, $options = [])
*/
class MywebsocketsTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('mywebsockets');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('id')
->allowEmptyString('id', null, 'create');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn(['user_id'], 'Users'), ['errorField' => 'user_id']);
return $rules;
}
}
the websocketscontroller is started with:
<?php
// in src/WebSocket.php
namespace App;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use App\Controller\MywebsocketsController;
require dirname(__DIR__) . '/vendor/autoload.php'; // Autoload Composer packages
$webSocketServer = IoServer::factory(
new HttpServer(
new WsServer(
new MywebsocketsController()
)
),
8080 // Port number
);
$webSocketServer->run();
any idea? thx