Yii Framework Forum: Get one attribute list from table - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Get one attribute list from table Rate Topic: -----

#1 User is offline   dreeger 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 12-April 17

Posted 21 April 2017 - 05:30 AM

Hi. I have for example 2 tables.
Lang: id, name, code
User lang: id, userid, langid

In admin lang table im writing a function, which should return all lang codes which user can edit for example.
So i need to select all langids from admin langs table by user id, then i should get lang codes from lang table. How can i do it?
0

#2 User is offline   dragan.zivkovic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 13-September 12

Posted 21 April 2017 - 07:56 AM

You can do that with relations :
in UserLang, define relation to Lang
'lang' => [self::BELONGS_TO, 'Lang', 'langid']


Then in User model you need relation to UserLang
'userLang' => [self::HAS_MANE, 'UserLang', ['userid' => 'id']


Now:
select user you want to work with:
example:
$user = User::model()->findByPk(1);


// userLanguages will be array of UserLang models (for wanted user)
$userLanguages = $user->userLang;


In each model that is in $userLanguages you can access Lang model trough relation
foreach ($userLanguages as $language) {
   echo $language->lang->code; // to get lang.code
}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users