首页 > 解决方案 > 从 PHP 5 升级到 7。创建的数组只有 null 值,然后才创建一个空数组

问题描述

我正在将应用程序从 PHP 5 转换为 PHP 7。

如果 $values=null 之前,那么之后 $values 的值将为 null 或空数组。现在它似乎正在使它成为 [0=>null]

发生了什么变化?

                if(!is_array($values)) {
                        $values = array($values);
                }
    /**
     * Search for Imaging group report data by sysi code. Optionally
     * specify a Banner username to narrow the search.
     *
     * @param Mixed $sysiCodes
     * @param String $username
     */
    public function searchBySysiCode($sysiCodes, $username = '%', $sort = 'user')
    {
        $imagingModuleDataAccess = new ImagingModuleDataAccess();
        $imagingGroupDataAccess = new ImagingGroupDataAccess();
        $op = '';
        
        /* Retreive modules and build a search string based on the
        specified sysi codes. This is required to ensure we exclude 
        "private" sysi codes and groups that cannot be requested
        for access. */
        $module = null;
        
        if(is_array($sysiCodes))
        {
            foreach($sysiCodes as $sysiCode)
            {
                $module = $imagingModuleDataAccess->searchBySysiCode($sysiCode)->first();
                
                if($module != null)
                {
                    $searchString .= $op . $imagingGroupDataAccess->getGroupMapSearchString($module);
                }
                
                $op = ' or ';
            }
        }
        else 
        {
            $module = $imagingModuleDataAccess->searchBySysiCode($sysiCodes)->first();
            
            if($module != null)
            {
                $searchString = $imagingGroupDataAccess->getGroupMapSearchString($module);
            }
        }
        
        $searchString = " and ( $searchString ) ";
        
        // Add the username if specified.
        if($username != null && $username != '%')
        {
            $searchString .= ' and ae_login.usrnam like upper(?) ';
            $values = $username;
        }
        
        // Add sorting option.
        switch ($sort)
        {
            case 'user':
                $searchString .= ' order by ae_login.usrnam, ae_als.aliasname ';
                break;
                
            case 'group':
            default:
                $searchString .= ' order by ae_als.aliasname, ae_login.usrnam ';
                break;
        }
        
        return $this->search($searchString, $values);
    }
    /**
     * Function used to access underlying datastore handle
     *
     * @param string $searchString sql where clause for search
     * @param array $values bind values for sql
     * @return mixed return type based on RETURNTYPE code
     */
    protected function search($searchString, $values)
    {
        $return = '';
        
        $sql = "select distinct
                    ae_login.usrnam,
                    ae_als.aliasid,
                    ae_als.aliasname,
                    ae_als.aliasdesc
                from
                    otgmgr.ae_als,
                    otgmgr.ae_login,
                    otgmgr.ae_amap
                where 
                    1 = 1 
                    and ae_amap.usrid = ae_login.usrid
                    and ae_als.aliasid = ae_amap.aliasid 
                    $searchString ";
        try 
        {
            $return = $this->retrieve($sql, $values);
        }
        catch(Exception $e)
        {
            throw $e;
        }
        
        return $return;
    }
        protected function retrieve($sql, $values=array())
        {
                // The result set to return.
                $return = '';

                /* Doctrine DBAL */
                if(!is_array($values)) {
                        $values = array($values);
                }

                $stmt = $this->dataSource->executeQuery($sql, $values);

                // Determine what type of result to return.
                if ($this->returnType == RETURNTYPE_RAW) {
                        $return = $stmt;
                } else if ($this->returnType == RETURNTYPE_ARRAY) {
                        $return = $this->getArray($stmt);
                }       else if ($this->returnType == RETURNTYPE_ASSOC) {
                  $return = $this->getAssoc($stmt);
                } else if ($this->returnType == RETURNTYPE_ITERATOR) {
                        $return = $this->getIterator($this->getArray($stmt));
                } else {
                        throw new Exception('Invalid returnType ' . $this->returnType);
                }

                return $return;
        }

标签: phpdbal

解决方案


在比较 PHP 5 与 7 时,此示例中实际上没有任何变化。据我所知,您可以从您的代码片段中看出,您可以将空值retrieve($sql, $values=array())作为第二个参数传递。 如果不满足方法中的
条件,则可能会发生这种情况。您可以在方法的开头定义变量,因此您不会将值放入方法的第二个参数中。
if($username != null && $username != '%')

searchBySysiCode($sysiCodes, $username = '%', $sort = 'user')
$valuessearchBySysiCodenullretrieve


推荐阅读