• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
<p>Message: Cannot use object of type stdClass as array</p>

#1
Code:
I have this in the view


{foreach from=$menus.Licensees item=this_licen name=licensees}
                                {if $this_licen.FranchiseeID <> $this_licen.PreviousFranchiseeID}
                                {if $smarty.foreach.licensees.first}
                                   
                                    {else}
                                    </select>
                                    {/if}
                                {if $vars.Franchisee == $this_licen.FranchiseeID}
                                {literal}
                                    <script type="text/javascript">
current_open = {/literal}{$this_licen.FranchiseeID}{literal};
</script>
                                    {/literal}
                                {/if}
                                <select class="LicenseeSelect" onchange="insert_licensee(this.value);" id="vars[Licensee][{$this_licen.FranchiseeID}]"  name="vars[SelectLicensee]" style="{if $vars.Franchisee == $this_licen.FranchiseeID}display: block;{/if}">
                                    <option onclick="insert_licensee(this.value);" value="">All Licensees</option>
                                    <option onclick="insert_licensee(this.value);" {if $vars.Licensee==$this_licen.FranchiseeID} selected="selected"{/if} value="{$this_licen.FranchiseeID}">No Licensee</option>
                                {/if}
                                    <option onclick="insert_licensee(this.value);" value="{$this_licen.ID}"{if $vars.Licensee==$this_licen.ID} selected="selected"{/if}>{$this_licen.Name}</option>
{/foreach}





I am getting <p>Message: Cannot use object of type stdClass as array</p>





I have this in my controller



$query = "
Code:
                    SELECT        *
Code:
                    FROM        Users
Code:
                    WHERE        Type = 'Licensee'
Code:
                    AND            Status = 'Active'
Code:
                    ORDER BY    FranchiseeID";
Code:
            $menus["Licensees"]=array();
Code:
            $prev_id = 0;
Code:
            $total_licensees_elements = 0;
Code:
            if($result=$this->db->query($query)){
Code:
                if($result->num_rows() > 0) {
Code:
                    while ($row = $result->unbuffered_row()){
Code:
                        if($row->FranchiseeID <> $prev_id){
Code:
                            $total_licensees_elements++;
Code:
                        }
Code:
                        $row->PreviousFranchiseeID = $prev_id;
Code:
                        $menus["Licensees"][]=$row;
Code:
                        $prev_id = $row->FranchiseeID;
Code:
                    }
Code:
                }
Code:
            }


How do I get $menus["Licensees"] in the right format please


Thanks
Reply

#2
Because you are trying to assign an object to an array.

I use these to helper when I need to convert one to the other.

PHP Code:
// -----------------------------------------------------------------------

if ( ! function_exists('objectToArray'))
{
    /**
     * objectToArray ()
     * -------------------------------------------------------------------
     *
     * @param  $data
     * @return array
     */
    function objectToArray(object $data) : array
    {
        if (is_object($data))
        {
            /**
             * Gets the properties of the given object
             * with get_object_vars function
             */
            $data get_object_vars($data);
        }

        /**
         * Return array converted to object Using __FUNCTION__ (Magic constant)
         * for recursive call
         */
        return (is_array($data)) ? array_map(__FUNCTION__$data) : $data;
    }
}

// -----------------------------------------------------------------------

if ( ! function_exists('arrayToObject'))
{
    /**
     * arrayToObject ()
     * -------------------------------------------------------------------
     *
     * @param  $data
     * @return object
     */
    function arrayToObject(array $data) : object
    
{
        /**
         * Return array converted to object Using __FUNCTION__ (Magic constant)
         * for recursive call
         */
        return (is_array($data)) ? (object) array_map(__FUNCTION__$data) : $data;
    }


Place these in one of your helpers and load it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(11-23-2020, 12:53 PM)InsiteFX Wrote: Because you are trying to assign an object to an array.

I use these to helper when I need to convert one to the other.

PHP Code:
// -----------------------------------------------------------------------

if ( ! function_exists('objectToArray'))
{
    /**
     * objectToArray ()
     * -------------------------------------------------------------------
     *
     * @param  $data
     * @return array
     */
    function objectToArray(object $data) : array
    {
        if (is_object($data))
        {
            /**
             * Gets the properties of the given object
             * with get_object_vars function
             */
            $data get_object_vars($data);
        }

        /**
         * Return array converted to object Using __FUNCTION__ (Magic constant)
         * for recursive call
         */
        return (is_array($data)) ? array_map(__FUNCTION__$data) : $data;
    }
}

// -----------------------------------------------------------------------

if ( ! function_exists('arrayToObject'))
{
    /**
     * arrayToObject ()
     * -------------------------------------------------------------------
     *
     * @param  $data
     * @return object
     */
    function arrayToObject(array $data) : object
    
{
        /**
         * Return array converted to object Using __FUNCTION__ (Magic constant)
         * for recursive call
         */
        return (is_array($data)) ? (object) array_map(__FUNCTION__$data) : $data;
    }


Place these in one of your helpers and load it.

Yep I know Sad

So I jsut need to wrap objectToArray() around $row then when I assign it and it will work? That was where I was stuck. Thanks
Reply

#4
I have changed it to


$menus["Licensees"][]=objectToArray($row);

But I am now getting


Message: Argument 1 passed to objectToArray() must be an object, string given?
Reply

#5
Your using twig templates so not really sure how twig is doing it but the foreach is nothing like CodeIgniters.

you can use this helper to see what your object is looking like, if it's a CodeIgniter database returned object
then it will be an object in an object.

PHP Code:
<?php

// -----------------------------------------------------------------------

/**
 * varDebug () - Add this method to a CodeIgniter Helper.
 * I named mine - debug_helper.php
 * -----------------------------------------------------------------------
 *
 * Formatted output of var_dump() etc;
 */
if ( ! function_exists('varDebug'))
{
    
/**
     * Debug Helper
     * -------------------------------------------------------------------
     * Outputs the given variable(s) with color formatting and location
     *
     * @param    mixed    - variables to be output
     */
    
function varDebug()
    {
        list(
$callee) = debug_backtrace();

        
$arguments func_get_args();

        
$total_arguments func_num_args();

        echo 
'<div><fieldset style="background: #fefefe !important; border:1px red solid; padding:15px">';
        echo 
'<legend style="background:lightgrey; padding:5px;">'.$callee['file'].' @line: '.
        
     $callee['line'].'</legend><pre><code>';

        
$i 0;
        foreach (
$arguments as $argument) {
            echo 
'<strong>Debug #'.++$i.' of '.$total_arguments.'</strong>: '.'<br>';
            
var_dump($argument);
        }

        echo 
"</code></pre></fieldset><div><br>";

        exit;
    }
}

/**
 * -----------------------------------------------------------------------
 * Filename: debug_helper.php
 * Location: ./app/Helpers/debug_helper.php
 * -----------------------------------------------------------------------
 */ 

Pass in your object that is giving you an error and this will give you a formatted output of it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.