开发者

Kohana 2 ORM: ->with() for has_many relations

开发者 https://www.devze.com 2023-04-07 06:46 出处:网络
We have a table for articles, and a table for locale data for articles. class Article_Model extends ORM {

We have a table for articles, and a table for locale data for articles.

class Article_Model extends ORM {
    protected $has_many = array('article_translations');
开发者_高级运维

[...]

class Article_Translation_Model extends ORM {

[...]

'articles_translations' table contain 'article_id', 'locale_id', 'heading', 'body', etc. columns.

Is it possible to do

SELECT article.id, article_translations.heading, article_translations.body
FROM articles
JOIN articles_translations ON (articles_translations.article_id = article.id)
WHERE articles_translations.locale_id = 'en_US.UTF-8'

using Kohana 2 ORM?

The ->with() construction works only for has_one relations. The articles_translations.article_id and articles_translations.locale_id is UNIQUE, of course.


You could go the other way though:

$article_translation = ORM::factory('article_translation')
    ->where('locale_id', 'en_US.UTF-8')
    ->find();

Then you can reference the article columns as:

$article_translation->body;
$article_translation->heading;
$article_translation->article->id;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号