在 cakephp4 中如何访问模型中的模型

问题描述

如何在 cakePHP4.2 的模型中访问另一个模型?我不清楚有关此问题的文档,然后我可以对此进行查询? TableRegistry 现已弃用。

 error UnkNown method "getTableLocator" called on App\Model\Table\LessonsTable  

//none of these no longer work
in model {

use Cake\ORM\Locator\LocatorAwareTrait;


class LessonsTable extends Table
 {
 ..

  private function  getlessonRevenue(){

  //$clients = $this->getTableLocator()->get('Clients');
  // $cleints = TableRegistry::get('Clients'); 
  // $this->Table = TableRegistry::get('Clients');
   $clients = $this->getTableLocator()->get('Clients');

https://api.cakephp.org/4.0/class-Cake.ORM.TableRegistry.html

解决方法

试试:

<?php
use Cake\ORM\Locator\LocatorAwareTrait; //<------------ add here
class ArchivesTable extends Table
{
  use LocatorAwareTrait; // <--------------------------- and add here
  public function myMethod()
  {
      $clients = $this->getTableLocator()->get('Clients');
  }

并阅读https://book.cakephp.org/4/en/orm/table-objects.html#using-the-tablelocator

并学习如何使用 php trait https://www.phptutorial.net/php-tutorial/php-traits/

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...