简体   繁体   English

OctoberCMS Builder插件显示阵列

[英]OctoberCMS Builder Plugin displaying arrays

I have built a quoting/invoicing system using the builder plugin in OctoberCMS and I have ran into a speedbump. 我已在OctoberCMS中使用构建器插件构建了报价/发票系统,但遇到了一次撞车事故。 I am trying to save an array to the database to be used as list items for the invoice but when I try to view the invoice in the backend I get the following Exception: 我试图将数组保存到数据库中以用作发票的列表项,但是当我尝试在后端查看发票时,出现以下异常:

htmlentities() expects parameter 1 to be string, array given

I have written this as a component and the section which is causing the issue looks like this: 我已将此作为组件编写,导致问题的部分如下所示:

public function items() {
    $plates = Db::table('orders')->where('quote_no', $this->quoteNo())->value('total_plate_qty');
    $hires = Db::table('orders')->where('quote_no', $this->quoteNo())->value('req_hires');
    $hardcopy = Db::table('orders')->where('quote_no', $this->quoteNo())->value('req_hardcopy_proof');
    $pdfproof = Db::table('orders')->where('quote_no', $this->quoteNo())->value('req_pdf_proof');
    if ($plates != 0) {
        $plates = "Total Plates: " . $plates;
    } else {
        $plates = "None";
    }
    if ($pdfproof === 'yes') {
        $pdfproof = 'PDF Proof @ R25.00';
    } else {
        $hires = 'None';
    }
    if ($hires === 'yes') {
        $hires = 'HiRes PDF @ R50.00';
    } else {
        $hires = 'None';
    }
    if ($hardcopy === 'yes') {
        $hardcopy = 'HardCopy Proof @ R150.00';
    } else {
        $hardcopy = 'None';
    }
    return [
        'plates' => $plates,
        'pdf' => $pdfproof,
        'hires' => $hires,
        'hardcopy' => $hardcopy
    ];
}

In the database the column in question is type TEXT and my model type in Builder for this field is also TEXT. 在数据库中,所讨论的列是TEXT类型,而在Builder中此字段的模型类型也是TEXT。

I am guessing that the issue is in the view of my model but I am unsure how to correct this, any help will be greatly appreciated. 我猜想这个问题是我的模型认为的,但是我不确定如何纠正此问题,我们将不胜感激。

ok ok I got it now may be you are trying to print array instead of string as {{ item }} .. so if item is an array it will throw this error. 好吧,好吧,我现在明白了,可能是您正在尝试将数组而不是字符串打印为{{item}} ..因此,如果item是数组,它将抛出此错误。

$items =  [
    'plates' => [
        'name' => $platesName,
        'price' => $platesPrice,
        'quantity' => $variants,
        'total' => $platesTotal
    ],
    'pdf' => [
        'name' => $pdfName,
        'price' => $pdfPrice,
        'quantity' => $variants,
        'total' => $pdfTotal
    ],
    'hires' => [
        'name' => $hiresName,
        'price' => $hiresPrice,
        'quantity' => $variants,
        'total' => $hiresTotal
    ],
    'hardcopy' => [
        'name' => $hardcopyName,
        'price' => $hardcopyPrice,
        'quantity' => $variants,
        'total' => $hardcopyTotal
    ]
];

I guess this is your data and you want to show it. 我想这是您的数据,并且您想显示它。 as its multi-dimension array we need 2 loops. 作为其多维数组,我们需要2个循环。 we assume that you have share $items variable to view so, 我们假设您有共享$ items变量来查看,

{% for key, item in items %} 
    <h1> {{ key }} </h1>
    <ul>    
        <li>{{ item.name }}</li>
        <li>{{ item.price }}</li>
        <li>{{ item.quantity }}</li>
        <li>{{ item.total }}</li>
    </ul>
{% endfor %}

// output
<h1>Plates</h1>
<ul>
    <li>$platesName</li>
    <li>$platesPrice</li>
    <li>$variants</li>
    <li>$platesTotal</li>
</ul>
<h1>Pdf</h1>
<ul>
    <li>$platesName</li>
    <li>$platesPrice</li>
    <li>$variants</li>
    <li>$platesTotal</li>
</ul>
// and sooo on..

this link may help you to fight with loops : https://twig.symfony.com/doc/2.x/tags/for.html 该链接可以帮助您应对循环: https : //twig.symfony.com/doc/2.x/tags/for.html

So after much frustration I used this method to get it to work: 因此,经过很多挫折之后,我使用了这种方法来使其工作:

public function items() {
    $NoOfPlates = Db::table('orders')->where('quote_no', $this->quoteNo())->value('total_plate_qty');
    $polymerPrice = Db::table('quotes')->where('quote_no', $this->quoteNo())->value('calc_polymer_price');
    $hires = Db::table('orders')->where('quote_no', $this->quoteNo())->value('req_hires');
    $hardcopy = Db::table('orders')->where('quote_no', $this->quoteNo())->value('req_hardcopy_proof');
    $pdfproof = Db::table('orders')->where('quote_no', $this->quoteNo())->value('req_pdf_proof');
    $variants = Db::table('quotes')->where('quote_no', $this->quoteNo())->value('variants');
    if ($NoOfPlates != 0) {
        $platesName = 'Plates';
        $platesPrice = $polymerPrice;
        $variants = $variants;
        $platesTotal = $platesPrice * $variants;
    } else {
        $platesName = 'No Plates';
        $platesPrice = 0;
        $variants = $variants;
        $platesTotal = $platesPrice * $variants;
    }
    if ($pdfproof === 'yes') {
        $pdfName = 'PDF Proof';
        $pdfPrice = 25.00;
        $variants = $variants;
        $pdfTotal = $pdfPrice * $variants;
    } else {
        $pdfName = 'No PDF Proofs';
        $pdfPrice = 0;
        $variants = $variants;
        $pdfTotal = $pdfPrice * $variants;
    }
    if ($hires === 'yes') {
        $hiresName = 'HiRes PDFs';
        $hiresPrice = 50.00;
        $variants = $variants;
        $hiresTotal = $hiresPrice * $variants;
    } else {
        $hiresName = 'No HiRes PDFs';
        $hiresPrice = 0;
        $variants = $variants;
        $hiresTotal = $hiresPrice * $variants;
    }
    if ($hardcopy === 'yes') {
        $hardcopyName = 'HardCopy Proofs';
        $hardcopyPrice = 100.00;
        $variants = $variants;
        $hardcopyTotal = $hardcopyPrice * $variants;
    } else {
        $hardcopyName = 'No HardCopy Proofs';
        $hardcopyPrice = 0;
        $variants = $variants;
        $hardcopyTotal = $hardcopyPrice * $variants;
    }

    return [
        'plates' => [
            'name' => $platesName,
            'price' => $platesPrice,
            'quantity' => $variants,
            'total' => $platesTotal
        ],
        'pdf' => [
            'name' => $pdfName,
            'price' => $pdfPrice,
            'quantity' => $variants,
            'total' => $pdfTotal
        ],
        'hires' => [
            'name' => $hiresName,
            'price' => $hiresPrice,
            'quantity' => $variants,
            'total' => $hiresTotal
        ],
        'hardcopy' => [
            'name' => $hardcopyName,
            'price' => $hardcopyPrice,
            'quantity' => $variants,
            'total' => $hardcopyTotal
        ]
    ];
}

Then on the frontend I call these like this: 然后在前端,我这样称呼这些:

{% if record.items.plates.name == 'Plates' %}
<tr>
    <td>{{ record.items.plates.name }}</td>
    <td class="text-xs-center">R{{ record.items.plates.price }}</td>
    <td class="text-xs-center">{{ record.items.plates.quantity }}</td>
    <td class="text-xs-right">R{{ record.items.plates.total }}</td>
</tr>
{% endif %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM