【问题标题】:How do i call a method in a modelTable from a different controller?如何从不同的控制器调用模型表中的方法?
【发布时间】:2017-12-13 06:22:03
【问题描述】:

我在 OrdersTable.php 中有一个方法 setTotalInDataBase

我尝试在 OrderproductsController.php 中调用 setTotalInDataBase

如下图

function setprice()
{
     $ordersTable=TableRegistry::get('OrdersTable');
     $ordersTable->setTotalInDataBase();
}

它给了我以下错误,

错误:在布尔值上调用成员函数 setTotalInDataBase()

如何从不同的控制器调用模型表中的方法?

【问题讨论】:

    标签: cakephp orm associations cakephp-3.0


    【解决方案1】:

    基于documentation,即使要加载Table对象,也不必在名称中指定“Table”。所以你需要打电话给Orders而不是OrdersTable

    use Cake\ORM\TableRegistry;
    
    public function setprice()
    {
        $ordersTable=TableRegistry::get('Orders');
        $ordersTable->setTotalInDataBase();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      相关资源
      最近更新 更多