I try to connect two tables in my database, but I get an error message saying that there is no relation:
Uzytkownicy is not associated with Uprawnienia
Here the code of my first model:
class UprawnieniaTable extends Table {
public function initialize(array $config) {
$this->primaryKey('ID');
$this->hasMany('Uzytkownicy', [
'foreignKey' => 'ID_uprawnienie'
]);
}
}
and second table:
class UzytkownicyTable extends Table {
public function initialize(array $config) {
$this->belongsTo('Uprawnienia', [
'foreignKey' => 'ID_uprawnienie'
]);
}
}
Table Uzytkownicy
has a foreign key ID_Uprawnienie
. In database everything is OK because I use this database in a normal SQL query.
Code that make error:
$uzytkownicy = TableRegistry::get('Uzytkownicy');
foreach ($uzytkownicy->find('all', ['contain' => ['Uprawnienia']]) $ uzytkownik) {
echo $uzytkownik->imie." ".$uzytkownik->nazwisko."<br>";
echo $uzytkownik->nazwa;
}
⟩ Cake\ORM\EagerLoader->_normalizeContain CORE/src/ORM/EagerLoader.php, line 250
⟩ Cake\ORM\EagerLoader->normalized CORE/src/ORM/EagerLoader.php, line 358
⟩ Cake\ORM\EagerLoader->attachableAssociations CORE/src/ORM/EagerLoader.php, line 337
⟩ Cake\ORM\EagerLoader->attachAssociations CORE/src/ORM/Query.php, line 897
⟩ Cake\ORM\Query->_transformQuery CORE/src/ORM/Query.php, line 853
⟩ Cake\ORM\Query->sql CORE/src/Database/Driver/Mysql.php, line 121
⟩ Cake\Database\Driver\Mysql->prepare CORE/src/Database/Connection.php, line 216
⟩ Cake\Database\Connection->prepare CORE/src/Database/Connection.php, line 268
⟩ Cake\Database\Connection->run CORE/src/Database/Query.php, line 174
⟩ Cake\Database\Query->execute CORE/src/ORM/Query.php, line 872
⟩ Cake\ORM\Query->_execute CORE/src/Datasource/QueryTrait.php, line 272
⟩ Cake\ORM\Query->_all CORE/src/ORM/Query.php, line 823
⟩ Cake\ORM\Query->all CORE/src/Datasource/QueryTrait.php, line 131
⟩ Cake\ORM\Query->getIterator APP/Template/Strony/glowna.ctp, line 49
⟩ include CORE/src/View/View.php, line 971
⟩ Cake\View\View->_evaluate CORE/src/View/View.php, line 931
⟩ Cake\View\View->_render CORE/src/View/View.php, line 579
⟩ Cake\View\View->render CORE/src/Controller/Controller.php, line 611
⟩ Cake\Controller\Controller->render APP/Controller/StronyController.php, line 11
⟩ App\Controller\StronyController->pokaz [internal function]
⟩ call_user_func_array CORE/src/Controller/Controller.php, line 429
⟩ Cake\Controller\Controller->invokeAction CORE/src/Routin/Dispatcher.php, line 114
⟩ Cake\Routing\Dispatcher->_invoke CORE/src/Routing/Dispatcher.php, line 87
⟩ Cake\Routing\Dispatcher->dispatch ROOT/webroot/index.php, line 37
⟩ require ROOT/index.php, line 16