Bravo Net
Bravo Net

Reputation: 805

Object of class stdClass could not be converted to string in codeigniter

there are two sample here,

1.

in controller

 $result_article = $this->cms->get_content($event_id);
 if($result_article->num_rows() == 1){
 $data['row'] = $result_article->row();
}

in my view

<?php echo $row->title; ?>

print_r($row)/print_r($result_article->row()); output

stdClass Object ( [id] => 43 [title] => Grant visit by Prime Minister [content] => this is the content [create_date] => 2012-09-21 [last_update] => 2012-09-22 19:27:12 )

with error appeared

A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: libraries/Parser.php
Line Number: 101

2.

in controller

$result_article = $this->cms->get_content($event_id);

                    if($result_article->num_rows() == 1){
                        $row = $result_article->row();

                          $data = array(
                             'title' => $row->title
                          );
}

in my view

<?php echo $title; ?>

print_r($title) output

Grant visit by Prime Minister

no error.

what is the different between the 1st controller and 2nd code, it suppose to be the same output right? i am confused!

to make it more clear, i have simplified the code

here is the model

function get_content($event_id){
     $this->db->select('id,title,content,create_date,last_update');
     $query = $this->db->get_where('events',array('id' => $event_id));
     return $query;
}

the controller

$data['query']  = $this->cms->get_content($this->uri->segment(3));

if($data['query']->num_rows() == 1){
$row = $data['query']->row();

<!--with this code, no error appear-->
$data = array(
'title' => $row->title,
'content' => $row->content,
'create_date' => $row->create_date,
'last_update' => $row->last_update,
'event_id' => $row->id
              );
<!--with this code, no error appear--> 

if is only these code

$data['query']  = $this->cms->get_content($this->uri->segment(3));

    if($data['query']->num_rows() == 1){
    $row = $data['query']->row();

error happen,

A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: libraries/Parser.php
Line Number: 101

Upvotes: 0

Views: 13593

Answers (3)

For those that have this issue, replace in \system\libraries\Parser.php:

$template = $this->_parse_single($key, (string)$val, $template);

To:

if (!is_object($val)) $template = $this->_parse_single($key, (string)$val, $template);

Upvotes: 0

mav
mav

Reputation: 1

Try to serialize() the 'row' before gettin the title. Because your query, I guess, returns a xml format (correct me if I'm wrong).

Upvotes: 0

JvdBerg
JvdBerg

Reputation: 21856

In your view in situation 1 it should be

<?php echo $article->row->title; ?>

to make the code the same as in situation 2.

As I see it, $article is a collection of rows, so you have to access a row from a article, and then get the title of that row.

Upvotes: 1

Related Questions