我正在将现有的开源应用程序从 CodeIgniter 3.x 框架转换为 CodeIgniter 4.x 框架。
根据文档,我需要执行以下步骤:
这是我的模型之一的示例
<?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/