1

我的数据库

学生桌

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

在表格列表中显示

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

在为每个循环创建一个在表格上显示数据列表时,如何显示“John and Matt 而不是“1,2”?

我在 $belongTo 的 Student Class 中有一个 StudentExtra 来显示导师姓名。但是,此方法仅显示一个导师姓名。

模型

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'
);

控制器

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

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

看法

<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>

结果

 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
            (
            )

    )
4

2 回答 2

2

其他 cakephp 方式如下。

您还可以将关联设置为Student 模型,如下所示。

学生模型中添加以下代码

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

在控制器中

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

现在只需pr($students); exit();在视图中或控制器功能的末尾做一个测试结果。

您将看到 StudentExtra 的额外附加记录数组,这就是您所需要的。

现在在视图文件中显示如下。

<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>

回答 更新响应代码

$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;
于 2013-01-23T04:27:58.797 回答
1

创建学生模型:

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);
    }

}

然后在你的控制器中:

$this->Students->getParentName(2); // in this case for Peter
于 2013-01-23T01:26:29.383 回答