在Kanboard中,每当我点击按钮时为什么会出现未定义的索引错误?

huangapple go评论65阅读模式
英文:

In Kanboard, why do I get undefined index for this code whenever I click the button?

问题

In Kanboard,每当我点击按钮时,为什么在这段代码中我会得到未定义的索引错误?

[php7:notice] PHP Notice:  Trying to access array offset on value of type null in /var/www/.../public_html/app/Helper/LayoutHelper.php on line 90
[php7:notice] PHP Notice:  Undefined index: project in /var/www/.../public_html/app/Helper/LayoutHelper.php on line 91

我读到isset是一个解决方法,但如果使用isset是解决方法的话,我应该放在哪里?我搞不清楚。

我尝试过:

<?= $this->url->link(t('Delete'), 'ContactsItemsController', 'remove', array('item_id' => isset($item['id']), 'plugin' => 'AddressBook'), true, 'btn btn-red') ?>
英文:

In Kanboard, why do I get undefined index for this code whenever I click the button?

[php7:notice] PHP Notice:  Trying to access array offset on value of type null in /var/www/.../public_html/app/Helper/LayoutHelper.php on line 90
[php7:notice] PHP Notice:  Undefined index: project in /var/www/.../public_html/app/Helper/LayoutHelper.php on line 91
&lt;?= $this-&gt;url-&gt;link(t(&#39;Delete&#39;), &#39;ContactsItemsController&#39;, &#39;remove&#39;, array(&#39;item_id&#39; =&gt; $item[&#39;id&#39;], &#39;plugin&#39; =&gt; &#39;AddressBook&#39;), true, &#39;btn btn-red&#39;) ?&gt;

I read that isset is a solution but if using isset is the solution, where do I place it? I can't figure it out.

I tried:

&lt;?= $this-&gt;url-&gt;link(t(&#39;Delete&#39;), &#39;ContactsItemsController&#39;, &#39;remove&#39;, array(&#39;item_id&#39; =&gt; isset($item[&#39;id&#39;]), &#39;plugin&#39; =&gt; &#39;AddressBook&#39;), true, &#39;btn btn-red&#39;) ?&gt;

答案1

得分: 0

要使用 isset,基本上是包裹您使用数组(甚至是简单变量)的部分,如下所示:

<?php
if(isset($array['key'])){
    // ... 可以安全使用 $array['key'] 值的部分 ...
    echo $this->url->link(...);
}

但您使用了 <?= ,这是 <?php echo 的速记方式,echo 命令必须有一个参数,即使它是空字符串或 null,所以您必须传递一些值,这限制了您如何使用 isset 包裹该部分。

您可以使用三元运算符,如下所示:

<?= isset($array['key']) ? /*... 安全使用 $array['key'] ...*/ $this->url->link(...) : '' ?>

或者立即执行的匿名函数,如下所示:

<?= (function(){ if(isset($array['key'])){ /*... 安全使用 $array['key'] ...*/ return $this->url->link(...); } })() ?>

请注意,当您未显式调用 return 命令时,php 函数会隐式返回 null。此方法应用于您无法通过三元运算符解决的复杂情况,因为可读性原因,在一段时间后您或其他人回到代码时,可能存在多个分支,难以理解。

如果您只关心单个值而不是整个函数调用或甚至块,可以使用null 合并运算符的速记方式,如下所示:

array('item_id' => $item['id'] ?? 0)

这在内部执行了 isset 检查。

它看起来比三元运算符好看,不是吗?

array('item_id' => isset($item['id']) ? $item['id'] : 0)

至于您发布的警告:“尝试访问类型为 null 的值的数组偏移量”,这意味着变量 $item 设置为 null,因此它不是数组。像这样的代码会产生相同的警告:

$item = null;
$item['id'];

或者变量 $item 可能未定义,这种情况下会有另一个警告“未定义变量 $item”。

最可能的情况是,您只是从数据库设置了 $item 变量,但在那之后您可能忘记了实际获取记录的检查。

第二个警告与您发布的代码无关,“未定义索引:project”,这将在以下情况发生:

$var = [];
$var['project'];

因此,您有一个变量是数组,但在该数组中没有名为 project 的索引/键。

英文:

To use isset you basically wrap around section where you use that array (or even simple variable), like:

&lt;?php
if(isset($array[&#39;key&#39;])){
    //... section where we can safely use the value of $array[&#39;key&#39;] ...
    echo $this-&gt;url-&gt;link(...);
}

But you use the &lt;?= which is shorthand for &lt;?php echo and echo command must have an argument, even if its empty string or null, so you have to pass some value there, which limits your options how to wrap the section with isset.

You could use ternary operator, like:

&lt;?= isset($array[&#39;key&#39;]) ? /*... safe use of $array[&#39;key&#39;] ...*/ $this-&gt;url-&gt;link(...) : &#39;&#39; ?&gt;

Or anonymous function executed right away, like:

&lt;?= (function(){ if(isset($array[&#39;key&#39;])){ /*... safe use of $array[&#39;key&#39;] ...*/ return $this-&gt;url-&gt;link(...); } })() ?&gt;

Note that when you do not explicitly call return command, php functions implicitly return null. Also you should use this method for complex cases which you cannot solve by ternary operator, because of the readability, there could be multiple branches which are harder to get when you or someone else return to the code after while.

In case you care only about single value and not entire function call or even block, there is shorthand in form of null coalescing operator, like:

array(&#39;item_id&#39; =&gt; $item[&#39;id&#39;] ?? 0)

Which kinda does the isset check internally.

It looks way better then ternary operator, don't you think ?

array(&#39;item_id&#39; =&gt; isset($item[&#39;id&#39;]) ? $item[&#39;id&#39;] : 0)

To the warnings you posted: Trying to access array offset on value of type null, this means that the variable $item is set to null, therefore it is not an array. Code like this produce the same warning:

$item = null;
$item[&#39;id&#39;];

Or the variable $item could be undefined, in which case there would be another warning undefined variable $item.

Most likely you are just setting the $item variable from database, but you are missing the check after that if you actually get the record.

Second warning is irrelevant to the code you posted Undefined index: project, this would happen in case like this:

$var = [];
$var[&#39;project&#39;];

So you have variable which is an array, but there is no index/key named as project present in that array.

答案2

得分: 0

解释得很好,对于我遇到的其他问题,isset的答案也很有帮助,所以感谢你解释这一点。

对于这个特定问题的修复是因为我在控制器中使用了project布局助手,而不是config布局助手。

/**
     * 联系人项目配置页面
     *
     * @access public
     */
    public function config()
    {
        $this->response->html($this->helper->layout->config('addressBook:config/index', array(
            'title' => t('通讯录设置'),
            'items' => $this->contactsItemsModel->getAllItems(),
        )));
    }
英文:

The explanation was great and an answer for isset for other issues I was having so thanks for explaining that.

The fix for this particular problem was because I was using the project layout helper instead of the config one in the controller.

/**
     * Contacts items config page
     *
     * @access public
     */
    public function config()
    {
        $this-&gt;response-&gt;html($this-&gt;helper-&gt;layout-&gt;config(&#39;addressBook:config/index&#39;, array(
            &#39;title&#39; =&gt; t(&#39;Address Book Settings&#39;),
            &#39;items&#39; =&gt; $this-&gt;contactsItemsModel-&gt;getAllItems(),
        )));
    }

huangapple
  • 本文由 发表于 2023年6月8日 14:51:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定