php - Relationships in Phalcon -
i'm working on multilanguage project , need help on getting proper language database.
in session have stored lang code , id database tabe:
db table languages
id lang_code lang 1 en-us english language (us) 2 es-es spanish (es) 3 de-de deutsch (de)
then have 2 tables menu
db table menu
id parent link 1 null intro/ 2 null about/ 3 null terms/
and db table menu_translation
id menu_id lang_id title 1 1 1 intro 2 2 1 3 3 1 terms & conditions 4 1 2 intro
in models did this
model languages
<?php class languages extends phalcon\mvc\model { public $id; public $lang_code; public $lang; }
model menu
<?php class menu extends phalcon\mvc\model { public $id; public $parent; public $link; public function initialize() { $this->hasmany('id', 'menu', 'parent'); } }
this parent because have nested menus need set menu belong menu.
model menutranslation
<?php class menutranslation extends phalcon\mvc\model { public $id; public $menu_id; public $lang_id; public $title; }
my question how set relation can menutranslation language set in session later can display like:
{% item in menu %} {{ menu.menutranslation.title }} {% endfor %}
is possible , if how can this?
you have wrong relation in menu model.
model menu
<?php class menu extends phalcon\mvc\model { public $id; public $parent; public $link; public function initialize() { $this->hasmany('id', 'menutranslation', 'menu_id'); } }
model menutranslation
<?php class menutranslation extends phalcon\mvc\model { public $id; public $menu_id; public $lang_id; public $title; public function initialize() { $this->hasmany('menu_id', 'menu', 'id'); $this->hasmany('lang_id', 'languages', 'id'); } }
template
{% item in menu %} {{ menu.getmenutranslation().getlanguages().title }} {% endfor %}
also, recomend create custom query join'ing 3 tables in 1 query.
php relationship phalcon
No comments:
Post a Comment