一张表与另一张表的同一个字段多次关联,如何表示

请看下面的表,MessageList表中有ToUserID和FromUserID两个字段,分别表示信息的收件人和发件人,与UserList表中的UserID关联。




DROP TABLE IF EXISTS `study`.`messagelist`;

CREATE TABLE  `study`.`messagelist` (

  `MessageId` int(11) NOT NULL AUTO_INCREMENT,

  `Title` varchar(60) NOT NULL,

  `Content` mediumtext NOT NULL,

  `ToUserID` int(11) NOT NULL,

  `IsRead` tinyint(1) DEFAULT '0',

  `PostDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

  `FromUserID` int(11) NOT NULL,

  PRIMARY KEY (`MessageId`)

) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8;


DROP TABLE IF EXISTS `study`.`userlist`;

CREATE TABLE  `study`.`userlist` (

  `UserID` tinyint(4) NOT NULL AUTO_INCREMENT,

  `UserName` varchar(20) NOT NULL,

  `Pwd` varchar(80) NOT NULL,

  `RealName` varchar(10) NOT NULL,

  `LoginCount` int(11) DEFAULT '0',

  `LastLogin` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

  PRIMARY KEY (`UserID`)

) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;



MessageList对应的Model如下:




<?php


class messagelist extends CActiveRecord

{

	const STATUS_READ=1;

	const STATUS_UNREAD=0;

        //其它省略

	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			//TODO:(jeri) a message belongs to an user whose class is UserList and the relationship

			//is base on the ToUserID

			'userList' => array(self::BELONGS_TO, 'UserList', 'ToUserID'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'Title' => 'Title',

			'Content' => 'Content',

			'ToUserID' => 'To User',

		);

	}

}



显示部分如下:




<table class="dataGrid">

<tr>

	<th class="label"><?php echo CHtml::encode($model->getAttributeLabel('Title')); ?>

</th>

    <td><?php echo CHtml::encode($model->Title); ?>

</td>

</tr>

<tr>

	<th class="label"><?php echo CHtml::encode($model->getAttributeLabel('ToUserID')); ?>

</th>

    <td><?php echo CHtml::encode($model->ToUserID); ?>

</td>

</tr>

<tr>

	<th class="label"><?php echo CHtml::encode($model->getAttributeLabel('FromUserID')); ?>

</th>

    <td><?php echo CHtml::encode($model->userList->UserName); ?>

</td>

</tr>

</table>



问一下,需要改动哪里的代码就可以像显示FromUserID关联的用户名一样直接显示ToUserID对应的用户名?

谢谢。

你仿照FromUserID关联声明ToUserID关联就可以了。

因为是MessageList表中的ToUserID和FromUserID都是与UserList表中的UserID关联得到对应的UserName,如果是做连接查询,需要对连接查询结果中的两个UserName字段重命名才会运行,现在是通过这种配置不知道如何解决。

models/messagelist.php


public function relations()

	{

		return array(

			'userList' => array(self::BELONGS_TO, 'UserList', 'ToUserID'),

			'userList' => array(self::BELONGS_TO, 'UserList', 'FromUserID'),

		);

	}

views/messagelist/list.php:


<table class="dataGrid">

<tr>

        <th class="label"><?php echo CHtml::encode($model->getAttributeLabel('Title')); ?>

</th>

    <td><?php echo CHtml::encode($model->Title); ?>

</td>

</tr>

<tr>

        <th class="label"><?php echo CHtml::encode($model->getAttributeLabel('ToUserID')); ?>

</th>

    <td><?php echo CHtml::encode($model->ToUserID); ?>

</td>

</tr>

<tr>

        <th class="label"><?php echo CHtml::encode($model->getAttributeLabel('FromUserID')); ?>

</th>

    <td><?php echo CHtml::encode($model->userList->UserName); ?>

</td>

</tr>

</table>

按照这种做法,ToUser和FromUser的用户名都是一样的了。


'userList' => array(self::BELONGS_TO, 'UserList', 'ToUserID'),

                        'userList' => array(self::BELONGS_TO, 'UserList', 'FromUserID'),

你试试看 把这两个的名字区分开,不要两个都用userList命名。

谢谢,按照楼上的提示,问题得到解决。