我尝试在我的数据库中连接两个表,但是我收到一条错误消息,说没有关系:
Uzytkownicy与Uprawnienia无关
这是我的第一个模型的代码:
class UprawnieniaTable extends Table {
public function initialize(array $config) {
$this->primaryKey('ID');
$this->hasMany('Uzytkownicy', [
'foreignKey' => 'ID_uprawnienie'
]);
}
}
和第二个表:
class UzytkownicyTable extends Table {
public function initialize(array $config) {
$this->belongsTo('Uprawnienia', [
'foreignKey' => 'ID_uprawnienie'
]);
}
}
表Uzytkownicy
有一个外键ID_Uprawnienie
。在数据库中,一切正常,因为我在正常的SQL查询中使用此数据库。
出错的代码:
$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