Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Cakephp:查询中不包括公开的虚拟字段_Mysql_Database_Cakephp - Fatal编程技术网

Mysql Cakephp:查询中不包括公开的虚拟字段

Mysql Cakephp:查询中不包括公开的虚拟字段,mysql,database,cakephp,Mysql,Database,Cakephp,我读了一些其他的问题,但它们都很老了。我使用的是CakePHP3.7.9。 我阅读了有关虚拟字段的文档,然后 到目前为止: Proforma.php 在视图中,我可以轻松访问此字段: <td class="text-right"><?= $proforma->item_proformas ? $this->Number->currency($proforma->total) : '' ?></td> 找不到字段总数。这里是调试的输出:

我读了一些其他的问题,但它们都很老了。我使用的是CakePHP3.7.9。 我阅读了有关虚拟字段的文档,然后

到目前为止:

Proforma.php

在视图中,我可以轻松访问此字段:

<td class="text-right"><?= $proforma->item_proformas ? $this->Number->currency($proforma->total) : '' ?></td>
找不到字段总数。这里是调试的输出:

object(Cake\ORM\Query) {

    '(help)' => 'This is a Query object, to get the results execute or iterate it.',
    'sql' => 'SELECT Proformas.id AS `Proformas__id`, Proformas.customer_id AS `Proformas__customer_id`, Proformas.proforma_number AS `Proformas__proforma_number`, Proformas.proforma_date AS `Proformas__proforma_date`, Proformas.payment_date AS `Proformas__payment_date`, Proformas.proforma_state_id AS `Proformas__proforma_state_id` FROM proformas Proformas',
    'params' => [],
    'defaultTypes' => [
        'Proformas__id' => 'integer',
        'Proformas.id' => 'integer',
        'id' => 'integer',
        'Proformas__customer_id' => 'integer',
        'Proformas.customer_id' => 'integer',
        'customer_id' => 'integer',
        'Proformas__proforma_number' => 'string',
        'Proformas.proforma_number' => 'string',
        'proforma_number' => 'string',
        'Proformas__proforma_date' => 'date',
        'Proformas.proforma_date' => 'date',
        'proforma_date' => 'date',
        'Proformas__payment_date' => 'date',
        'Proformas.payment_date' => 'date',
        'payment_date' => 'date',
        'Proformas__proforma_state_id' => 'integer',
        'Proformas.proforma_state_id' => 'integer',
        'proforma_state_id' => 'integer'
    ],
    'decorators' => (int) 0,
    'executed' => false,
    'hydrate' => true,
    'buffered' => true,
    'formatters' => (int) 0,
    'mapReducers' => (int) 0,
    'contain' => [],
    'matching' => [],
    'extraOptions' => [],
    'repository' => object(App\Model\Table\ProformasTable) {

        'registryAlias' => 'Proformas',
        'table' => 'proformas',
        'alias' => 'Proformas',
        'entityClass' => 'App\Model\Entity\Proforma',
        'associations' => [
            (int) 0 => 'customers',
            (int) 1 => 'proformastates',
            (int) 2 => 'invoices',
            (int) 3 => 'itemproformas'
        ],
        'behaviors' => [],
        'defaultConnection' => 'default',
        'connectionName' => 'default'

    }
}
为什么即使虚拟字段是公开的,它也不会插入到查询中

上面链接的文档说明:

请记住,虚拟字段不能用于查找。如果希望它们成为实体的JSON或数组表示的一部分,请参阅公开虚拟字段

然后:

默认情况下,将实体转换为数组或JSON时不会导出虚拟字段。为了公开虚拟字段,需要使其可见。定义实体类时,可以提供应公开的虚拟字段列表


因此,公开虚拟字段应该允许我在查找中使用它们。

遗憾的是,虚拟字段在查询运行时没有计算出来。以下是一些您可能会觉得有用的替代方法

创建实际表的视图,在DB级别处理总和 在表模型中创建beforeFind事件。
我不确定你要记住虚拟字段不能在查找中使用的部分还不清楚。看看如何用PHP编写一个特殊函数来进行计算?对于转换为SQL代码以使其成为查询的一部分,您的期望是什么?公开虚拟字段是指当您将已从SQL读取的实体转换为其他表示形式时,使该字段可用。因为英语不是我的主要语言,我知道,公开虚拟字段会使他们可以使用JSON和类似数组的查询。而如果你想不久后不能用在我听起来像解决方案使用!JSON和数组是输出格式,通过对查询结果运行PHP代码生成。该代码可以使用函数添加虚拟字段。但这一切都是在查询运行完毕之后发生的。这里肯定存在误解。虚拟字段用于对单个实体进行计算_例如,在订单记录中,getTotal是有意义的,其中的列包括价格、税和运费,它将这三个列相加,得出一个订单的总金额。如果要跨多行求和,则这是一项工作,或者可能是一项任务。
$query = $this->Proformas->find();
debug($query);
$query->select(['value' => $query->func()->sum('total')]);
object(Cake\ORM\Query) {

    '(help)' => 'This is a Query object, to get the results execute or iterate it.',
    'sql' => 'SELECT Proformas.id AS `Proformas__id`, Proformas.customer_id AS `Proformas__customer_id`, Proformas.proforma_number AS `Proformas__proforma_number`, Proformas.proforma_date AS `Proformas__proforma_date`, Proformas.payment_date AS `Proformas__payment_date`, Proformas.proforma_state_id AS `Proformas__proforma_state_id` FROM proformas Proformas',
    'params' => [],
    'defaultTypes' => [
        'Proformas__id' => 'integer',
        'Proformas.id' => 'integer',
        'id' => 'integer',
        'Proformas__customer_id' => 'integer',
        'Proformas.customer_id' => 'integer',
        'customer_id' => 'integer',
        'Proformas__proforma_number' => 'string',
        'Proformas.proforma_number' => 'string',
        'proforma_number' => 'string',
        'Proformas__proforma_date' => 'date',
        'Proformas.proforma_date' => 'date',
        'proforma_date' => 'date',
        'Proformas__payment_date' => 'date',
        'Proformas.payment_date' => 'date',
        'payment_date' => 'date',
        'Proformas__proforma_state_id' => 'integer',
        'Proformas.proforma_state_id' => 'integer',
        'proforma_state_id' => 'integer'
    ],
    'decorators' => (int) 0,
    'executed' => false,
    'hydrate' => true,
    'buffered' => true,
    'formatters' => (int) 0,
    'mapReducers' => (int) 0,
    'contain' => [],
    'matching' => [],
    'extraOptions' => [],
    'repository' => object(App\Model\Table\ProformasTable) {

        'registryAlias' => 'Proformas',
        'table' => 'proformas',
        'alias' => 'Proformas',
        'entityClass' => 'App\Model\Entity\Proforma',
        'associations' => [
            (int) 0 => 'customers',
            (int) 1 => 'proformastates',
            (int) 2 => 'invoices',
            (int) 3 => 'itemproformas'
        ],
        'behaviors' => [],
        'defaultConnection' => 'default',
        'connectionName' => 'default'

    }
}