本文介绍了如何从关系中获取数据与 yii 中的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Yii 的初学者,所以我在问这个问题.

I am beginner in Yii, So I am asking this question.

我有三张不同的桌子.

第一张桌子

第一个表是language(id, language_name)

//id 是主键.

第二表

第二个表是verse(id, topic_id, surah_id, verse_text)

//id 是主键,

第三张表

第三个表是verse_translations(id, verse_id, language_id, translations_text)

//id是主键,language_id是外键与语言表的引用,

// id is primary key, language_id is foreign key references with language table,

//verse_id 是带有 verse 表的外键引用.

// verse_id is foreign key references with verse table.

现在我的问题是.

我想获取具有特定 verse_id 的可用翻译的语言列表.?为此,我想在 verse 模型文件中建立关系,在我看来,它将返回可用语言,那么如何在视图中也获得结果.?如果发生任何变化,诗歌模型、视图和控制器会发生什么变化.

I want to get the list of languages of available translations with specific verse_id. ? For that i want to make relations in verse model file that will return an available languages in my view, so how to get result in view also.? and what will be the changes in verse model, view and controller if any changes occur.

我已经编写了下面的 MySQL 查询.

I have written MySQL query which is in below.

SELECT language.language_name from language 
Inner Join verse_translations ON  verse_translations.language_id = language.id
Where verse_translations.verse_id = 1

但我在 Yii 中需要这个.

But i need this in Yii.

我已经通过gii代码生成器生成了诗歌模型.

I have generated verse model through gii code generator.

我的诗歌模型关系功能.

My Verse Model Relations function.

public function relations()
{
 return array(
    'sorah' => array(self::BELONGS_TO, 'Sorah', 'sorah_id'),
    'topic' => array(self::BELONGS_TO, 'Topic', 'topic_id'),
    'verseFeedbacks' => array(self::HAS_MANY, 'VerseFeedback', 'verse_id'),
    'verseImages' => array(self::HAS_MANY, 'VerseImages', 'verse_id'),
    'verseLinks' => array(self::HAS_MANY, 'VerseLinks', 'verse_id'),
    'verseTafseers' => array(self::HAS_MANY, 'VerseTafseer', 'verse_id'),
    'verseTranslations' => array(self::HAS_MANY, 'VerseTranslations', 'verse_id'),
    'language_name' => array(self::HAS_MANY, 'Language', 'id'),
 );
}

推荐答案

您可以从语言表中轻松获取可用翻译语言的列表.

You can easily get the list of available translated languages from language table.

先看看吧.

'verseTranslations' => array(self::HAS_MANY, 'VerseTranslations', 'verse_id'),

此关系将获取特定诗歌 id 的所有诗歌翻译行,这意味着如果您有 10 种不同语言的 10 种不同翻译,其中 verse_id 为 1,它将显示所有.现在你可以看到有问题的 verse_translation 表有 language_id.

this relation will take all the rows of verse translation of specific verse id, mean if you have 10 different translation in 10 different languages with verse_id 1, it will display all. Now you can see in question verse_translation table have language_id.

所以我们可以通过那个 language_id 获取所有语言.

So we can get all languages by that language_id.

现在我们通过verseTranslations建立另一个与language_id相关的关系,这个关系将显示所有翻译的语言.

Now we make another relation which is relating to language_id through verseTranslations, and this relation will display all the translated languages.

'verse_lang' => array(self::HAS_MANY, 'Language', array('language_id'=>'id'), 'through'=>'verseTranslations'),

所以我写了一个Sql Query就相当于这两个关系.

So as i have written a Sql Query is equivalent to these two relations.

'verseTranslations' => array(self::HAS_MANY, 'VerseTranslations', 'verse_id'),
'verse_lang' => array(self::HAS_MANY, 'Language', array('language_id'=>'id'), 'through'=>'verseTranslations'),

在视图中,我们可以通过var_dump($data->verse_lang)

On view, we can easily access it by var_dump($data->verse_lang)

就是这样.

用于理解关系.您可以仔细阅读此链接.

for understanding relations. You may read carefully to this link.

http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-through

希望它会有所帮助.

如果您需要任何帮助,请在评论框中留言.

If you need any help then leave a message in comment box.

谢谢.

这篇关于如何从关系中获取数据与 yii 中的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 21:15