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

问题描述 投票:0回答:1

如何在 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

cakephp cakephp-3.0 cakephp-4.x
1个回答
7
投票

尝试:

<?php

class ArchivesTable extends Table
{

    use \Cake\ORM\Locator\LocatorAwareTrait; // <------------ 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/

© www.soinside.com 2019 - 2024. All rights reserved.