php - 从 CI4 中的另一个模型访问模型函数的正确方法是什么?

标签 php model-view-controller namespaces codeigniter-4

我正在将现有的开源应用程序从 CodeIgniter 3.x 框架转换为 CodeIgniter 4.x 框架。
根据文档,我需要执行以下步骤:
enter image description here
这是我的模型之一的示例

<?php

namespace App\Models;

use CodeIgniter\Model;

/**
 * Sale class
 */
class Sale extends Model
{
    /**
     * Get sale info
     */
    public function get_info($sale_id)
    {
        $this->create_temp_table(array('sale_id' => $sale_id));

        $decimals = totals_decimals();
        $sales_tax = 'IFNULL(SUM(sales_items_taxes.sales_tax), 0)';
        $cash_adjustment = 'IFNULL(SUM(payments.sale_cash_adjustment), 0)';
        $sale_price = 'CASE WHEN sales_items.discount_type = ' . PERCENT
            . " THEN sales_items.quantity_purchased * sales_items.item_unit_price - ROUND(sales_items.quantity_purchased * sales_items.item_unit_price * sales_items.discount / 100, $decimals) "
            . 'ELSE sales_items.quantity_purchased * (sales_items.item_unit_price - sales_items.discount) END';

        if($this->config->item('tax_included'))

我面临的问题是,在执行此操作后,PHPStorm 在 if($this->config->item('tax_included')) 上给我警告。告诉我“通过魔法方法访问的属性”
根据我添加的 CI4 文档 $this->config = new Config();但是 PHPStorm 然后提示 $this->config告诉我“动态声明的属性”并建议我添加一个名为 $config 的私有(private)变量......我认为这不是我想要的。它还提示 Config() 说“未定义的类配置”
我还尝试在类声明上方添加以下内容:
/**
 * Sale class
 * @property Config config
 */
但是我得到了同样的提示,即 Config 不是一个有效的类。
Config 模型位于 app\Models\Config.php 下,该目录与该模型所在的目录相同。

最佳答案

$mymodel = new MyModel();
我建议在您的 ctl 中使用服务层和实体调用服务。

关于php - 从 CI4 中的另一个模型访问模型函数的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69030352/

相关文章:

php - 在 codeigniter 的 form_multiselect() 中设置多个值

php - 如何使用 PHP 格式化 Excel 工作表单元格

namespaces - 如何获取 MediaWiki namespace 列表?

php - 动态添加命名空间

jQuery 选择带有命名空间元素的选择器

php - 在 Apache VirtualHost 上设置 Laravel 5

php - 变量在函数内未定义,无法在函数内到达 MySQL $connection

model-view-controller - CakePHP 对一起使用 set() 和 compact() 的说明。仅适用于 compact()

java - 将主页重定向到前端 Controller

php - 一个很好的 mvc 模型类,跟踪属性