实体虚拟字段在不同视图中的工作方式不同-CakePHP 4

实体虚拟字段在不同视图中的工作方式不同-CakePHP 4,php,cakephp,Php,Cakephp,我在数据库中有3个表: 产品:id |名称 食谱:id |标题|输入|价格 菜肴:id |产品id |配方id |数量 一种产品包括许多配方,其相应数量如下: 产品:炒饭 菜肴:米饭|每公斤10美元| 0.1公斤 +鸡蛋|每只1美元| 1只 我创建了一个虚拟字段来计算单个产品的所有配方的总成本: // in src/Model/Entity/Product.php class Product extends Entity { protected $_accessible = [

我在数据库中有3个表:

  • 产品:id |名称
  • 食谱:id |标题|输入|价格
  • 菜肴:id |产品id |配方id |数量
一种产品包括许多配方,其相应数量如下:

  • 产品:炒饭
  • 菜肴:米饭|每公斤10美元| 0.1公斤 +鸡蛋|每只1美元| 1只
我创建了一个虚拟字段来计算单个产品的所有配方的总成本:

// in src/Model/Entity/Product.php
class Product extends Entity
{
    protected $_accessible = [
        'title' => true,
        'unit' => true,
        'price' => true,
        'dishes' => true,
        'created' => true,
        'modified' => true,
    ];

    protected function _getTotalCost()
    {
        $dishes = $this->dishes;
        $total_cost = 0;

        foreach ($dishes as $dish) {                // <--- this is line 44
            $input_price = $dish->recipe->input_price;
            $quantity = $dish->quantity;
            $amount = $input_price * $quantity;
            $total_cost += $amount;
        }
        return $total_cost;
    }
}
但在templates/Products/index.php中,它显示了一个错误:

Warning (2): Invalid argument supplied for foreach() [APP/Model\Entity\Product.php, line 44]
Code Context
App\Model\Entity\Product::_getTotalCost() - APP/Model\Entity\Product.php, line 44
Cake\ORM\Entity::get() - CORE\src\Datasource\EntityTrait.php, line 289
Cake\ORM\Entity::__get() - CORE\src\Datasource\EntityTrait.php, line 129
include - ROOT\templates\Products\index.php, line 30
Cake\View\View::_evaluate() - CORE\src\View\View.php, line 1176
Cake\View\View::_render() - CORE\src\View\View.php, line 1134
Cake\View\View::render() - CORE\src\View\View.php, line 764
Cake\Controller\Controller::render() - CORE\src\Controller\Controller.php, line 696
Cake\Controller\Controller::invokeAction() - CORE\src\Controller\Controller.php, line 538
Cake\Controller\ControllerFactory::invoke() - CORE\src\Controller\ControllerFactory.php, line 79
Cake\Http\BaseApplication::handle() - CORE\src\Http\BaseApplication.php, line 251
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 77
Authentication\Middleware\AuthenticationMiddleware::process() - ROOT\vendor\cakephp\authentication\src\Middleware\AuthenticationMiddleware.php, line 124
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 73
Cake\Http\Middleware\CsrfProtectionMiddleware::process() - CORE\src\Http\Middleware\CsrfProtectionMiddleware.php, line 156
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 73

请告诉我如何解决这个问题。非常感谢你的帮助

您的
视图
操作的控制器可能会使用
包含
来包含产品的菜肴和食谱,而
索引
操作则不会。你应该能够在
索引中简单地更新
包含部分查询的
,或者在完全缺失的情况下添加它。

我在
索引中更新了
操作:
$product=$this->Products->find('list',['contain'=>['disks.Recipes',],])但它不起作用。
列表
只是(默认情况下)ID和名称的平面数组,它根本不是实体。将其更改为
all
,您应该可以在视图中获得所需的数据。请注意,您的视图可能有类似于
foreach($id=>product$name)
,这需要更改以考虑到它现在是实体这一事实。
echo $product->total_cost;
// result $2;
Warning (2): Invalid argument supplied for foreach() [APP/Model\Entity\Product.php, line 44]
Code Context
App\Model\Entity\Product::_getTotalCost() - APP/Model\Entity\Product.php, line 44
Cake\ORM\Entity::get() - CORE\src\Datasource\EntityTrait.php, line 289
Cake\ORM\Entity::__get() - CORE\src\Datasource\EntityTrait.php, line 129
include - ROOT\templates\Products\index.php, line 30
Cake\View\View::_evaluate() - CORE\src\View\View.php, line 1176
Cake\View\View::_render() - CORE\src\View\View.php, line 1134
Cake\View\View::render() - CORE\src\View\View.php, line 764
Cake\Controller\Controller::render() - CORE\src\Controller\Controller.php, line 696
Cake\Controller\Controller::invokeAction() - CORE\src\Controller\Controller.php, line 538
Cake\Controller\ControllerFactory::invoke() - CORE\src\Controller\ControllerFactory.php, line 79
Cake\Http\BaseApplication::handle() - CORE\src\Http\BaseApplication.php, line 251
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 77
Authentication\Middleware\AuthenticationMiddleware::process() - ROOT\vendor\cakephp\authentication\src\Middleware\AuthenticationMiddleware.php, line 124
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 73
Cake\Http\Middleware\CsrfProtectionMiddleware::process() - CORE\src\Http\Middleware\CsrfProtectionMiddleware.php, line 156
Cake\Http\Runner::handle() - CORE\src\Http\Runner.php, line 73