Kohana 2 ORM: ->with() for has_many relations
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;
精彩评论