phpyiicdetailview

Print all values of an array in CDetailView Yii


I'm currently new to Yii and have some problems in my app.

I have 2 AR (Employee and Children) with the relations is Employee has_many Children. Now when we go into the view of Employee (in the view&id=blablabla not in index), I want to list all the children that the Employee have.

I have made a function in EmployeeController class to retrieve the children from my db

$anak = Anak::Model()->findAll(array(
                            'condition'=>'id_karyawan=:id_karyawan',
                            'params'=>array(':id_karyawan'=>$id_karyawan)));
    //return $anak;
    foreach ($anak as $data){
        return $data->namaanak;
    }

the problem is, it only showing 1 data (the first one, to be exact) of the children, even though in my db the Employee has 3 children. When I try to count of query result, it's showing 3.

My employee/view is like this

$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
    'id_karyawan',
    'nama',
    array(
        'name'=>'idjabatan',
        'value'=>$model->findByPk($model->id_karyawan)->jabatan->namajabatan,
    ),
    array(
        'name'=>'gaji',
        'value'=>Yii::app()->numberFormatter->formatCurrency($model->findByPk($model->id_karyawan)->jabatan->gaji, 'Rp'),
    ),
    'alamat',
    array(
        'name'=>'ttl',
        'value'=>$model->tempatlahir.', '.Yii::app()->dateFormatter->format("dd MMMM yyyy", $model->tgllahir),
    ),
    array(
        'name'=>'Istri',
        'value'=>Istri::model()->getNama($model->id_karyawan),
        'type'=>'raw'
        ),
    array(
        'name'=>'Anak',
        'value'=>$this->getAnak($model->id_karyawan),
        'type'=>'raw'
    )
),

I have tried to google it, but i couldn't find any working answer :(

PS: karyawan = employee, anak = children.


Solution

  • You are only getting the first related value because you are returning it right away inside your foreach loop in the function. Gather all the related models first then return that and work with that data.

    You can also skip that extra function inside your controller. If you use Gii, it should have already set up the proper Employee HAS_MANY children relation for you, so you could use that to pull all related models for each Employee model you are looking at. See below for a quick example

    Employee model:

    public function relations()
    {
        return array(
            'children' => array(self::HAS_MANY, 'Child', 'employee_id'),
        );
    }
    

    View file, passing an employee model to CDetailView:

    $this->widget('zii.widgets.CDetailView', array(
        'data' => $model,
        'attributes'=>array(
            array(
                'name' => 'Children',
                'value' => function ($data) {
                    // Get all related children using the relation defined in the Employee model and use CHtml::listData to store data inside the $children variable as an array using `id` as key and `child_name` as value
                    $children = CHtml::listData($data->children, 'id', 'child_name');
                    // Return names as a comma separated list
                    return implode(', ', $children);
                },
                'type'=>'raw'
            )
        )
    );
    

    My tables/column names probably don't match up with what you have but hopefully this helps.