簡體   English   中英

實體虛擬字段在不同視圖中的工作方式不同 - CakePHP 4

[英]Entity Virtual Fields works differently in different views - CakePHP 4

我在數據庫中有 3 個表:

  • 產品: id | 標題
  • 食譜:id | 標題 | 輸入價格
  • 菜品:id | product_id | recipe_id | 數量

一個產品包括許多具有相應數量的食譜,例如:

  • 產品:炒飯
  • 菜餚:米飯 | 每公斤 10 美元 | 0.1 公斤 + 雞蛋 | 每件 $1 | 1片
// in src/Model/Table/ProductsTable.php
public function initialize(array $config): void
{
    $this->belongsToMany('Recipes', [
        'foreignKey' => 'product_id',
        'targetForeignKey' => 'recipe_id',
        'joinTable' => 'Dishes',
    ]);
    $this->hasMany('Dishes');
}

// in src/Model/Table/RecipesTable.php
public function initialize(array $config): void
{
    $this->belongsToMany('Products', [
        'foreignKey' => 'recipe_id',
        'targetForeignKey' => 'product_id',
        'through' => 'Dishes',
    ]);        
    $this->hasMany('Dishes');
}

// in src/Model/Table/DishsTable.php
public function initialize(array $config): void
{
    $this->belongsTo('Products', [
        'foreignKey' => 'product_id',
        'joinType' => 'INNER',
    ]);
    $this->belongsTo('Recipes', [
        'foreignKey' => 'recipe_id',
        'joinType' => 'INNER',
    ]);
}

我創建了一個虛擬字段來計算單個產品所有配方的總成本:

// 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;
    }
}

它在模板/產品/view.php 中完美運行:

echo $product->total_cost;
// result $2;

但是在 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

請告訴我如何解決這個問題。 非常感謝你的幫助!

您的view操作的控制器可能使用contain來包含產品的菜餚和食譜,而index操作則不是。 您應該能夠簡單地更新index查詢的contain部分,或者如果它完全丟失則添加它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM