我如何从不同的控制器调用 modelTable 中的方法?
How do i call a method in a modelTable from a different controller?
我在 OrdersTable.php
中有一个方法 setTotalInDataBase
我试图在 OrderproductsController.php
中调用 setTotalInDataBase
喜欢下面
function setprice()
{
$ordersTable=TableRegistry::get('OrdersTable');
$ordersTable->setTotalInDataBase();
}
它给了我以下错误,
Error: Call to a member function setTotalInDataBase() on boolean
如何从不同的控制器调用 modelTable 中的方法?
基于documentation,即使要加载Table
对象,也不必指定“Table“ 在名字里。所以你需要调用 Orders
而不是 OrdersTable
.
use Cake\ORM\TableRegistry;
public function setprice()
{
$ordersTable=TableRegistry::get('Orders');
$ordersTable->setTotalInDataBase();
}
我在 OrdersTable.php
中有一个方法 setTotalInDataBase我试图在 OrderproductsController.php
中调用 setTotalInDataBase喜欢下面
function setprice()
{
$ordersTable=TableRegistry::get('OrdersTable');
$ordersTable->setTotalInDataBase();
}
它给了我以下错误,
Error: Call to a member function setTotalInDataBase() on boolean
如何从不同的控制器调用 modelTable 中的方法?
基于documentation,即使要加载Table
对象,也不必指定“Table“ 在名字里。所以你需要调用 Orders
而不是 OrdersTable
.
use Cake\ORM\TableRegistry;
public function setprice()
{
$ordersTable=TableRegistry::get('Orders');
$ordersTable->setTotalInDataBase();
}