Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Problem With Relation Bound To Itself

$
0
0
Hi,

I try to make same things as mentionned here in "through on self" section : http://www.yiiframework.com/doc/guide/1.1/en/database.arr

But that don't work for me :(

Could you help me please ?

My tables :

CREATE TABLE IF NOT EXISTS `client` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `login` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `firstName` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `login` (`login`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;



CREATE TABLE IF NOT EXISTS `clientfriend` (
  `accept` tinyint(1) NOT NULL,
  `clientId` int(11) NOT NULL,
  `clientFriendId` int(11) NOT NULL,
  PRIMARY KEY (`clientId`,`clientFriendId`),
  KEY `FK_ClientFriend_clientFriendId` (`clientFriendId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


ALTER TABLE `clientfriend`
  ADD CONSTRAINT `FK_ClientFriend_clientFriendId` FOREIGN KEY (`clientFriendId`) REFERENCES `client` (`id`),
  ADD CONSTRAINT `FK_ClientFriend_clientId` FOREIGN KEY (`clientId`) REFERENCES `client` (`id`);


I add same relations in Client model :

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(
			//'clientfriends' => array(self::HAS_MANY, 'Clientfriend', 'clientFriendId'),
			//'clientfriends1' => array(self::HAS_MANY, 'Clientfriend', 'clientId'),
			'clientfriends'=>array(
                self::HAS_MANY,'Clientfriend','clientId','joinType'=>'INNER JOIN'
            ),
            'clientfriends1'=>array(
                self::HAS_MANY,'Client',array('clientFriendId'=>'id'),
                    'through'=>'clientfriends','joinType'=>'INNER JOIN'
            ),
		);
	}


I try to display friends of a client by using this code :

$model = Client::model()->findByPk(1);
$friends=$model->clientfriends1;


$friends is always empty !

Viewing all articles
Browse latest Browse all 18717

Trending Articles