doukuiqian5345 2015-11-27 16:00
浏览 39

在CakePHP 3中关联表不起作用

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;
}

enter image description here

⟩ 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
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?