Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I asume you can't decode them later, if you would change <code>$company_pk</code> to public it should encode correctly. Problem is with your object cast to array which adds NULL byte to your private member <code>$company_pk</code>. In <code>echo CJSON::encode((array)$ret_val[0]);</code> as it's array of OBJECTS. </p> <p>You can do nasty. </p> <pre><code>$json = '{"\u0000CompanyModel\u0000company_pk":"2"}'; $json = str_replace('\u0000', '', $json); var_dump(json_decode($json)); </code></pre> <p>Or you pass object to <code>CJSON::encode</code> i dont know much of YII but it should handle object, as per manual <a href="http://www.yiiframework.com/doc/api/1.1/CJSON" rel="nofollow">http://www.yiiframework.com/doc/api/1.1/CJSON</a>.</p> <p>Here is example to reproduce issue:</p> <pre><code>class test { private $private = 1; public $publiv = 2; } $obj = new test(); $array = (array) $obj; $json = json_encode($array); var_dump($json); var_dump(json_decode($json)); </code></pre> <p><strong>EDIT</strong>:</p> <p>From the manual <a href="http://www.php.net/manual/en/language.types.array.php" rel="nofollow">http://www.php.net/manual/en/language.types.array.php</a>:</p> <blockquote> <blockquote> <p>If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.</p> </blockquote> </blockquote> <p>I did quick look to <code>CJSON::encode</code> from YII and you can use object directly, but your object must be traversable, so you must implement Iterator interface. <a href="http://www.php.net/manual/en/class.iterator.php" rel="nofollow">http://www.php.net/manual/en/class.iterator.php</a>.</p> <p>EDIT:2 </p> <p>Implementing interface could be tricky, there is another option to call <code>get_object_vars</code> from within object and in this case you will get array which will work.</p> <pre><code>class CompanyModel extends CComponent { private $company_pk; public function getCompany_pk() { return $this-&gt;company_pk; } public function setCompany_pk($value) { $this-&gt;company_pk = $value; } public function export() { return get_object_vars($this); } </code></pre> <p>}</p> <p>And then:</p> <pre><code>$ret_val = $model-&gt;getCompanyList(); echo CJSON::encode($ret_val-&gt;export()); </code></pre> <p>Problem why it does not work for you with normal object because YII uses <code>get_object_vars</code> internally and it can't access private properties when it's in different scope. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload