Commits

Harro Verton committed fe33f45

add option to allow to_json() to recurse into loaded related objects

Comments (0)

Files changed (2)

application/datamapper/json.php

 	 * @param	DataMapper $object The DataMapper Object to convert
 	 * @param	array $fields Array of fields to include.  If empty, includes all database columns.
 	 * @param	boolean $pretty_print Format the JSON code for legibility.
+	 * @param	boolean $recurse Recurse into related objects if TRUE.
 	 * @param	boolean		$no_encode		Internal use only. if true, return the result without encoding
 	 * @return	string A JSON formatted String, or FALSE if an error occurs.
 	 */
-	public function to_json($object, $fields = '', $pretty_print = FALSE, $no_encode = FALSE)
+	public function to_json($object, $fields = '', $pretty_print = FALSE, $recurse = FALSE, $no_encode = FALSE)
 	{
 		if(empty($fields))
 		{
 			// handle related fields
 			if(array_key_exists($f, $object->has_one) || array_key_exists($f, $object->has_many))
 			{
-				// each related item is stored as an array of ids
-				// Note: this method will NOT get() the related object.
+				// if $recurse = FALSE, each related item is stored as an array of ids
+				// otherwise recurse into the related obects
 				$rels = array();
 				foreach($object->{$f} as $item)
 				{
-					$rels[] = $item->id;
+					if ($recurse)
+					{
+						$rels[] = $item->to_json('', FALSE, $recurse, TRUE);
+					}
+					else
+					{
+						$rels[] = $item->id;
+					}
 				}
 				$result[$f] = $rels;
 			}
 	 * @param	DataMapper $object The DataMapper Object to convert
 	 * @param	array $fields Array of fields to include.  If empty, includes all database columns.
 	 * @param	boolean $pretty_print Format the JSON code for legibility.
+	 * @param	boolean $recurse Recurse into related objects if TRUE.
 	 * @return	string A JSON formatted String, or FALSE if an error occurs.
 	 */
-	public function all_to_json($object, $fields = '', $pretty_print = FALSE)
+	public function all_to_json($object, $fields = '', $pretty_print = FALSE, $recurse = FALSE)
 	{
 		$result = array();
 		foreach($object as $o)
 		{
-			$result[] = $o->to_json($fields, FALSE, TRUE);
+			$result[] = $o->to_json($fields, FALSE, $recurse, TRUE);
 		}
 		$json = json_encode($result);
 		if($json === FALSE)

manual/pages/extensions/json.html

 <p class="note">To enable these methods, add <dfn>'json'</dfn> to DataMapper's config, under <strong>'extensions'</strong>.</p>
 <p>Converts objects to and from a <a href="http://en.wikipedia.org/wiki/Json">JSON-encoded</a> string.</p>
 
-<h2>to_json(<i>$fields</i>, <i>$pretty_print</i>)</h2>
+<h2>to_json(<i>$fields</i>, <i>$pretty_print</i>, <i>$recurse</i>)</h2>
 <ul>
 	<li><dfn>$fields</dfn>: (Optional) If provided, only these fields will be included.  If empty or not provided, only the database columns will be included.</li>
 	<li><dfn>$pretty_print</dfn>: (Optional) If <dfn>TRUE</dfn>, the JSON string will be formatted to be easier to read.  Defaults to <dfn>FALSE</dfn>.</li>
+	<li><dfn>$recurse</dfn>: (Optional) If <dfn>TRUE</dfn>, loaded related objects will be included in the result.  Defaults to <dfn>FALSE</dfn>.</li>
 	<li><b>Returns</b>: A JSON-encoded view of the <var>$object</var>, or <dfn>FALSE</dfn> if an error occurs.</li>
 </ul>
 <p>Converts the <var>$object</var> into a JSON-encoded string.</p>
 // {"id": 1, "name":"Bob Johnson", "email":"bob.johnson@example.com"}</samp>
 </pre>
 
-<h2>all_to_json(<i>$fields</i>, <i>$pretty_print</i>)</h2>
+<h2>all_to_json(<i>$fields</i>, <i>$pretty_print</i>, <i>$recurse</i>)</h2>
 <ul>
 	<li><dfn>$fields</dfn>: (Optional) If provided, only these fields will be included.  If empty or not provided, only the database columns will be included.</li>
 	<li><dfn>$pretty_print</dfn>: (Optional) If <dfn>TRUE</dfn>, the JSON string will be formatted to be easier to read.  Defaults to <dfn>FALSE</dfn>.</li>
+	<li><dfn>$recurse</dfn>: (Optional) If <dfn>TRUE</dfn>, loaded related objects will be included in the result.  Defaults to <dfn>FALSE</dfn>.</li>
 	<li><b>Returns</b>: A JSON-encoded view of <var>$object<kbd>-></kbd><i>all</i></var>, or <dfn>FALSE</dfn> if an error occurs.</li>
 </ul>
 <p>Converts <var>$object<kbd>-></kbd><i>all</i></var> into a JSON-encoded string.  The items will be placed into an array before conversion.</p>