2208

Use Yii findAll to return a model w/ all properties

Question:

I am still new to Yii and wondering how to return JSON from the $models = MyModel::model()->findAll();.

Say for example MyModel has a relation for MyChildModels in a ONE:MANY fashion.

Straight from the Rest example on the Yii site I have:

foreach ($models as $model) { $rows[] = $model->attributes; } $this->_sendResponse(200, CJSON::encode($rows), 'application/json');

I get all of the model's attributes but NOT the joined relation attributes.

Similarly, I can change the $rows line to be:

$rows[] = $model->myChildModels;

...and I get all of the myChildModels attributes for each model, but not any attributes (as I would expect).

But what I want is the full suite - the Model attributes PLUS all of the myChildModels and their attributes.

How do I accomplish this?

Answer1:

I do the same thing with Yii. Here is how I do it.

$models = MyModel::model()->findAll(); if ($models){ echo CJSON::encode($models); }

I don't normally sent a JSON header, but you can if you want.

header('Content-type: application/json');

for related models try this.

foreach ($models as $model) { $rows[] = $model->attributes; $rows[] = $model->related->attributes; }

Recommend

  • How can I substitute my own custom dynamic scaffolding methods
  • Imagemagick set interline spacing?
  • Singular Value Decomposition Implementation
  • parallelize process in missForest package
  • Enterprise Architect - Cancel Connectors Bridges
  • Two columns in subquery in where clause
  • Sort by a column in a union query in SqlAlchemy SQLite
  • Pandas: merge_asof() sum multiple rows / don't duplicate
  • R DataTable Join and constrain rows
  • Importing Excel files with a large number of columns header into mysql with c#
  • Table striping rows in CSS Grid
  • CSS Grid, position absolute an element in a css grid item: IMPOSSIBLE
  • How to check if a string contains any string of a column in MySQL and vice versa?
  • How to Add Polymorphic Comments to Feed?
  • Oracle - Second level subquery cannot see field from main query
  • how to get data attributes of dynamically generated element
  • MySQL performance when updating row with FK
  • Why the SequenceFile is truncated?
  • Row to Column conversion in Talend
  • Merging rows to columns
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • pillow imaging ImportError
  • Low TTL with Leveled Compaction, should I reduce gc_grace_seconds to improve read performance withou
  • List images(01.png) and descriptions(01.txt) from directory
  • Authentication in Play! and RestEasy
  • ListItem.Attributes.Add not working
  • jQuery ready not fired after rails link_to is clicked
  • How to change the font size of a single index for UISegmentedControl?
  • Limiting recursion to certain level - Duplicate rows
  • Bad request using file_get_contents for PUT request in PHP
  • Atlas images wrong size on iPad iOS 9
  • D3 nodes and links from JSON with nested arrays of children
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Retrieving value from sql ExecuteScalar()
  • vba code to select only visible cells in specific column except heading
  • Websockets service method fails during R startup
  • Do I've to free mysql result after storing it?
  • Transpose CSV data with awk (pivot transformation)
  • Matrix multiplication with MKL
  • Sorting a 2D array using the second column C++