Question

My DB

STUDENT TABLE

ID    NAME     PARENT_ID (VARCHAR)
1     JOHN     NULL
2     MATT     NULL
3     PETER    1,2

Display in a list of Table

ID    NAME     MENTOR NAME
1     JOHN     NULL
2     MATT     NULL
3     PETER    JOHN, MATT

How can I display "John and Matt instead of "1,2" when creating a for each loop to display the list of data on a table?

I've a StudentExtra in my Student Class within the $belongTo to display the mentor name. However, this method shows only one mentor name.

MODEL

class Student extends AppModel
{
// Mainly for PHP4 users
var $name = 'Student';
var $hasMany = array('Order','Test'); 

var $belongsTo = array
(
'StudentExtra'=>array
(
   'className'  => 'Student',
   'foreignKey' => 'parent_id'
),'School','Classroom'
);

CONTROLLER

    $this->paginate = array(
            'limit' => 10,
        'order' => array('Student.name' => 'ASC'));

    $this->set('students', $this->paginate('Student'));

VIEW

<table>
<?php foreach ($students as $student){ ?>
<tr> 
<td>
<?php $student['Student']['id'] ?>
</td>
</tr>
<tr> 
<td>
<?php $student['Student']['name'] ?>
</td>
</tr>
<tr> 
<td>
<?php $student['Student']['parent_id'] ?>
</td>
</tr>
<?php } ?>
</table>

RESULT

 Array
 (
  [0] => Array
    (
        [Student] => Array
            (
                [id] => 1190
                [customers_gender] => M
                [resumetest] => 
                [created] => 2013-04-01 19:29:08
                [modified] => 2013-04-01 19:29:08
                [customers_name] => aaa aaa
                [customers_dob] => 1970-01-01
                [customers_roles] => student
                [customers_types] => student
                [customers_parent_id] => 1055,1056
                [school_id] => 40
                [classroom_id] => 96
            )

        [StudentExtra] => Array
            (
                [id] => 1055
                [customers_gender] => F
                [created] => 2013-03-28 11:14:25
                [modified] => 2013-03-28 11:14:25
                [customers_name] => Mich
                [customers_dob] => 1970-01-01
                [customers_roles] => teacher
                [customers_types] => teacher
                [customers_parent_id] => 
                [school_id] => 40
                [classroom_id] => 96
            )

        [School] => Array
            (
                [id] => 40
                [name] => Primary School
                [description] => 
                [created] => 2013-03-28 10:47:05
                [modified] => 2013-03-28 10:47:05
            )

        [Classroom] => Array
            (
                [id] => 96
                [name] => 30G
                [year_level] => 3
                [description] => 
                [created] => 2013-03-28 10:58:25
                [modified] => 2013-03-28 10:58:25
                [school_id] => 40
            )

        [Order] => Array
            (
            )

        [Test] => Array
            (
            )

    )
Was it helpful?

Solution

Other cakephp way is as below.

You can also set association to Student model as below.

In Student Model add below code

var $belongsTo = array
(
    'StudentExtra'=>array
    (
       'className'  => 'Student',
       'foreignKey' => 'parent_id'
    )
);

In controller

$this->set('students', $this->paginate('Student'));

Now just do a pr($students); exit(); to test result in view or at the end of controller function.

You will see extra appended record array for StudentExtra which is all you need.

Now in view file display it as below.

<table>
    <?php foreach ($students as $student){ ?>
    <tr> 
        <td>
            <?php $student['Student']['id'] ?>
        </td>
    </tr>
    <tr> 
        <td>
            <?php $student['Student']['name'] ?>
        </td>
    </tr>
    <tr> 
        <td>
            <?php $student['StudentExtra']['name'] ?>
        </td>
    </tr>
    <?php } ?>
</table>

Answer Update response Code

$this->paginate = array
(
    'joins'=>array
    (
        array
        (
            'table'=>'students',
            'alias'=>'StudentExtra',
            'type' =>'inner',
            'conditions' =>array('StudentExtra.id IN (Student.parent_id)')
        )
    ),
    'limit'=> 10,
    'order' => array('Student.name' => 'ASC')
);

$this->set('students', $this->paginate('Student'));
debug($this->paginate('Student'));
exit;

OTHER TIPS

Create a students model:

class Students extends AppModel {

    public $useTable = false;

    public function getParentName($id) {
        $id = (int) $id;
        $sql = "
            SELECT NAME FROM `students` WHERE ID = (
                SELECT PARENT_ID FROM `students` WHERE ID = $id
            )
        ");
        return $this->query($sql);
    }

}

Then in your controller:

$this->Students->getParentName(2); // in this case for Peter
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top