Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Pass the result to the view
  • Hi everyone,

    I've created a view with pagination, table sort and search
    capabilities.
    Everything goes well, until I need too return the data
    from the search, back to the view.
    I have a problem with passing the result to a view.

    First, here is the index method.

    <code>
    public function action_index()
        {
            $search = '';
            if (Input::method() == 'POST')
            {
                $search = Input::post('search');
                Session::set('search', $search);
            }

            $query = Model_Recipient::find();
            $number_of_recipients = $query->count();

            $per_page=Input::post('perpage', Session::get('perpage_recipients',10) );
            Session::set('perpage_recipients', $per_page);

            $config = array(
                'pagination_url' => 'admin/recipients/index/',
               ....etc.......  );

            Pagination::set_config($config);

            $config_table = array(
                'sort_by'        => 'id',
                'direction'        => 'asc',
                 ...etc........ )

            \TableSort\TableSort::set_config($config_table);

            $sort_by    = \TableSort\TableSort::$sort_by;
            $sort_dir     = \TableSort\TableSort::$direction;

            $query = Model_Recipient::find()->order_by($sort_by, $sort_dir);

            if($search)
            {
                $data['recipients']    = DB::select()->from('recipients')
                                            ->or_where_open()
                                                ->where('vorname', 'like', "%".$search."%")
                                                ->or_where('nachname', 'like', "%".$search."%")
                                                ->or_where('email', 'like', "%".$search."%")
                                            ->or_where_close()
                                            ->order_by($sort_by, $sort_dir)
                                            ->limit(Pagination::$per_page)
                                            ->offset(Pagination::$offset)
                                            ->as_object('Model_Recipients')
                                            ->execute();

                //echo "<pre>"; print_r($data['recipients']); echo "</pre>";exit;
            }
            else
            {
                $data['recipients'] = $query->limit(Pagination::$per_page)->offset(Pagination::$offset)->get();
            }

            $data['pagination'] = Pagination::create_links();
            $data['table_head'] = \TableSort\TableSort::create_table_head();

            $this->template->title = __("app.Recipients");
            $this->template->per_page = $per_page;
            $this->template->content = View::forge('admin/recipients/index', $data);
        }
    </code>

    When I search for something, I receive this error :

    Fuel\Core\FuelException [ Error ]: Database results are read-only



    COREPATH/classes/database/result.php @ line 266

     266        throw new \FuelException('Database results are read-only');



    What I am doing wrong ? How can I pass the result to the view ?

    When
    I'm doing a print_r to the result in the controller, everything is
    there, so there is no problem with the way I use the DB class.

    Thanks,
    Gabriel
  • Can you print all the stack error? The one you posted is only the last method called, print all: I think the error is some callback before.

    A little tip: convert your tag "<code>" in "[ code]" (without space) and your code will be formatted ;-) 
  • Hi,

    thanks for the tip :)

    Here is the stack error !

    Fuel\Core\FuelException [ Error ]: Database results are read-only



    COREPATH/classes/database/result.php @ line 266



    261     * @return  void
    262     * @throws  Exception
    263     */
    264    final public function offsetSet($offset$value)
    265    {
    266        throw new \FuelException('Database results are read-only');
    267    }
    268
    269    /**
    270     * Implements [ArrayAccess::offsetUnset], throws an error.
    271     *

  • Ok, I think the problem is in the line APPPATH/views/admin/template.php @ line 128 , it should be the line that begins all the stack trace error.

    Can you post some code from this point? 
  • [code]
    123<?php if (Session::get_flash('info')): ?>
    124                <div class="alert-message info">
    125                    <p>
     126                   <?php echo implode('</p><p>', (array) Session::get_flash('info')); Session::delete_flash('info'); ?>
    127                    </p>
    128                </div>
    129<?php endif; ?>
    130<?php if (Session::get_flash('error')): ?>
    131                <div class="alert-message error">
    132                   <p>
    133                    <?php echo implode('</p><p>', (array) Session::get_flash('error')); Session::delete_flash('error'); ?>
    134                    </p>
    135                </div>
    136<?php endif; ?>
    [code]
  • I don't think that's the problem :(. I've tried woth [code] but it doesn't work.
  • sorry, i've just added some new assests to my template, so now the line with the error is

    138 : <?php echo $content; ?>
  • I don't think so: that line prin out the View with the content, so the error must be in that view.
    The view should be this one: admin/recipients/index .

    Perhaps there contents are used in the wrong way. Can you post the code of that view? (I think we are going to fix it ;-) )
  • HarroHarro
    Accepted Answer
    Plain DB queries return database objects (for example of type MySQLi_Result), which are PHP internal objects that are immutable.

    All data passed to a view is by default encoded by FuelPHP as a security measure. Encoding means changing, which you can't, which causes this error.

    Solutions:
    - add as_object() to your query to use objects of type stdClass instead
    - add as_array() to your qeuery to use arrays instead of objects
    - whilelist the result class so it won't be encoded (a VERY BIG security risk!)
    - Use ORM or Model_Crud, those objects don't have the problem either
  • Hi,

    I've made it like this, and it works now :

    [code]
    $data['recipients'] = Model_Recipient::find()
                                            ->where('vorname', 'like', "%".$search."%")
                                            ->or_where('nachname', 'like', "%".$search."%")
                                            ->or_where('email', 'like', "%".$search."%")
                                            ->order_by($sort_by, $sort_dir)
                                            ->limit(Pagination::$per_page)
                                            ->offset(Pagination::$offset)
                                            ->get();
    [code]

    Many thanks for your answers.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion