Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

First code: $result=ResPlaces::model()->findall($criteria);

         /*foreach($result as $value)
     {
        $model_name=ResRegistration::model()->findByAttributes(array('id'=>$value->user_id));
        $model_image=ResImages::model()->findByAttributes(array('place_id'=>$value->id),array('limit'=>'1'));


     }*/

     }
      echo CJSON::encode($result);

?>

i need to add model_name->company_name & $model_image->image
to my echo json array

share|improve this question
    
Are these both set up as a relation in your ResPlaces model? –  chris--- May 11 at 11:46
    
i update my question yes ( have relation ) –  Ali Adel May 11 at 11:54
add comment

1 Answer

up vote 0 down vote accepted

Try to load the relations within the findAll so you don't need the foreach.

ResPlaces::model()->with('ResRegistration', 'ResImages')->together()->findAll($criteria);

For ResRegistration and ResImages use the relation names as defined in your model. If you don't need all fields of these relations, you can specify a select in your $criteria.

See the guide for more info.

edit: I am not quite sure, why you cannot use relation. However you will need a loop, like you already have. Here is how you do it without relations:

First add the fields you want to use to your model. In this case

class ResPlaces extends CActiveRecord
{
    public $name; // check that these do not collide with your models db fields
    public $image;

    [...]
}

In your controller do the loop like you did before:

foreach($result as $key => $value)
{

    $result[$key]->name = ResRegistration::model()->findByAttributes(array('id' => $value->user_id));

    // findByAttributes returns only one record, so you don't need the limit here
    // if you want multiple records, you have to use findAllByAttributes
    $result[$key]->image = ResImages::model()->findByAttributes(array('place_id' => $value->id), array('limit' => '1'));
}

That should do it. However I wouldn't recommend this way, because you have lots of additional database requests. If your $result is populated with say 100 records, you have in sum 200 additional queries which are not nessassary with a relation.

Note also that if you need these two other fields more often, it may be better to put these two queries which are now in the controller in your model. The afterFind() would be the right place.

share|improve this answer
    
ResRegistration has id and ResPlaces has user_id i use return array('company_name'=>array(self::HAS_ONE, 'ResRegistration', 'id'), to make relation but $result don't show ResRegistration information –  Ali Adel May 11 at 13:35
    
Well what is the output then? Do a var_dump. Also posting your models may help. –  chris--- May 11 at 17:14
    
chris i can't make yii relation so from my question can you help me to make all output in one json array? –  Ali Adel May 11 at 17:23
    
Okay I updated the answer. –  chris--- May 11 at 18:45
    
thx for your full reply i think you help me but i need final step echo CJSON::encode($result); which have all variables from $result/$result[$key]->name/$result[$key]->image i need them in one json echo –  Ali Adel May 11 at 18:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.