Welcome Guest, Not a member yet? Register   Sign In
DMZ 1.7.1 (DataMapper OverZealous Edition)

[eluser]WanWizard[/eluser]
You're using an ORM, so don't complain it uses Objects. That's how it works. Wink

Having said that, I understand your question. Could you please add this as a feature request: http://bitbucket.org/wanwizard/datamapper/issues (and add a link back to your post), I'll see if I can squeeze it in for the next update...

[eluser]Haqqi[/eluser]
I use DM 1.8.1-dev with HMVC

I need some localizations using 2 or more languages. But I have a problem while adding language for DM Model. After that, I do a change just like below:

Code:
class DM_Lang extends MY_Lang

The language could be load with no problem. But one feature that won't search for right language folder is when I try to load other model from other module. It won't automatically find the right language folder for that model (which is in another module).

Any solution?

[eluser]WanWizard[/eluser]
I don't use HMVC, so I can't help you.

MX creates a new CI instance, under which your module controllers run. This instance instantiates MX_Lang as $this->lang, which is then used in your controllers.

You can try changing the DM_Lang class in libraries/datamapper.php to extend MX_Lang instead of CI_Lang, so Datamapper can use MX's $this->lang->load() method.

[eluser]Haqqi[/eluser]
Yes, I did it. I always load some required modules just like

Code:
DataMapper::add_model_path(
        array(
          APPPATH . 'modules/user'
        )
    );

but whenever I access other modules, for example "post" module, it give an error, cannot find the required language for user model (I place the model_user_m_lang.php in modules/user/language/english folder).

My MY_Lang already extends MX_Lang from HMVC without any addition.

What I think that may be a problem is lang() method in MX_Lang. It uses only active modules to get additional language folder:

Code:
$_module = CI::$APP->router->fetch_module();
list($path, $_langfile) = Modules::find($langfile.'_lang', $_module, 'language/'.$idiom.'/');

How can I get other loaded language file from DataMapper loaded model?

[eluser]WanWizard[/eluser]
It is a restriction of MX that only files from active modules can be accessed.

You can try working around this by manually registering your module using $this->load->_add_module_paths($module).

[eluser]Haqqi[/eluser]
it doesn't work, the language won't be loaded. Because i need it quickly, I just create override method in DM_Lang just like this:

Code:
function load($langfile, $lang = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') {
    if (is_array($langfile)) {
      foreach ($langfile as $_lang)
        $this->load($_lang);
      return $this->language;
    }

    $deft_lang = CI::$APP->config->item('language');
    $idiom = ($lang == '') ? $deft_lang : $lang;

    if (in_array($langfile . '_lang' . EXT, $this->is_loaded, TRUE))
      return $this->language;

    $_module = CI::$APP->router->fetch_module();
    list($path, $_langfile) = Modules::find($langfile . '_lang', $_module, 'language/' . $idiom . '/');
    
    if($path === FALSE) {
      foreach(DataMapper::get_loaded_model_path() as $path_on) {
        list($path, $_langfile) = Modules::find($langfile. '_lang', rtrim(str_replace(APPPATH.'modules/', '', $path_on), '/'), 'language/'.$idiom. '/');
        if($path !== FALSE) {
          break;
        }
      }
    }
    
    if ($path === FALSE) {
      if (($lang = parent::load($langfile, $lang, $return, $add_suffix, $alt_path)))
        return $lang;
    } else {
      if (($lang = Modules::load_file($_langfile, $path, 'lang'))) {
        if ($return)
          return $lang;
        $this->language = array_merge($this->language, $lang);
        $this->is_loaded[] = $langfile . '_lang' . EXT;
        unset($lang);
      }
    }

    return $this->language;
  }

You see that I add these lines:

Code:
if($path === FALSE) {
      foreach(DataMapper::get_loaded_model_path() as $path_on) {
        list($path, $_langfile) = Modules::find($langfile. '_lang', rtrim(str_replace(APPPATH.'modules/', '', $path_on), '/'), 'language/'.$idiom. '/');
        if($path !== FALSE) {
          break;
        }
      }
    }

in case if the first try won't return good result. Of course it will make the process longer, but I really need it quickly. If you have any other better options, I will be glad using it.

[eluser]WanWizard[/eluser]
From the looks of it, MX only searches the current module, so nothing I can do here.

You might want to take this up with @wiredesignz, to see if he sees a better solution.

[eluser]Jacob F.[/eluser]
Hiya,

I'm trying to use a deep relationship
Code:
$groups->include_related('user/order',array('tweight','tprice'), TRUE)->get();


These are legacy tables from before my time that I'm in the early stages of re-factoring (but can't change them yet). The orders table has a different/nonsense name, there are no foreign key constraints, and the field-names where there should be foreign keys have non-DataMapper-friendly names (like `id_user`).

The path from groups to users works fine (it's new), but when jumping from users to orders, DataMapper is convinced there is a many-to-many relationship (it's really one-to-many). I specified the following:
Code:
#User

$table = 'appuser';
$has_many = array(
'order' => array(
  'join_table' => 'oldorders',
  'join_self_as' => 'id_users'
)
);
Code:
#Order

$table = 'oldorders';
$has_one = array(
'user' => array(
  'join_table' => 'oldorders'
)
);

I feel like it's a hack to specify the join_table like that, but then DataMapper kind of accepts the one-to-many relationship: it eliminated the join for a relational table between users and orders, but it still uses the computed name of the non-existent relational table. Also, it doesn't completely respect the join_self_as: it takes the value and appends it with _id.

Here's the SQL DataMapper spits out:

Code:
#Without 'join_table'

SELECT
`groups`.*,
`oldorders`.`tweight` AS appuser_order_tweight,
`oldorders`.`tprice` AS appuser_order_tprice
FROM (`groups`)
LEFT OUTER JOIN `groups_appuser` groups_appuser
  ON `groups`.`id` = `groups_appuser`.`group_id`
LEFT OUTER JOIN `appuser` appuser
  ON `appuser`.`id` = `groups_appuser`.`appuser_id`
LEFT OUTER JOIN `oldorders_appuser` appuser_oldorders_appuser
  ON `appuser`.`id` = `appuser_oldorders_appuser`.`appuser_id`
LEFT OUTER JOIN `oldorders` appuser_oldorders
  ON `appuser_oldorders`.`id` = `appuser_oldorders_appuser`.`id_appuser_id`
ORDER BY `groups`.`name`;
Code:
#With 'join_table'

SELECT
`groups`.*,
`appuser_oldorders`.`tweight` AS appuser_order_tweight,
`appuser_oldorders`.`tprice` AS appuser_order_tprice
FROM (`groups`)
LEFT OUTER JOIN `groups_appuser` groups_appuser
  ON `groups`.`id` = `groups_appuser`.`group_id`
LEFT OUTER JOIN `appuser` appuser
  ON `appuser`.`id` = `groups_appuser`.`appuser_id`
LEFT OUTER JOIN `oldorders` appuser_oldorders
  ON `appuser`.`id` = `appuser_oldorders`.`id_appuser_id`
ORDER BY `groups`.`name`;

Code:
#Correct SQL

SELECT
`groups`.*,
`oldorders`.`tweight` AS order_tweight,
`oldorders`.`tprice` AS order_tprice
FROM (`groups`)
LEFT OUTER JOIN `groups_appuser` groups_appuser
  ON `groups`.`id` = `groups_appuser`.`group_id`
LEFT OUTER JOIN `appuser` appuser
  ON `appuser`.`id` = `groups_appuser`.`appuser_id`
LEFT OUTER JOIN `oldorders` oldorders
  ON `appuser`.`id` = `oldorders`.`id_appusers`
ORDER BY `groups`.`name`;

EDIT: I tried changing both models to use $has_one and it spat out the same code. Also, it seems to ignore any declaring I do to user in the order model.

[eluser]WanWizard[/eluser]
That will not work, all FK's are suffixed with '_id', hardcoded.

I've looked at it, but it's going to be very complex to change this, and changing this will break all existing applications that use this (and expect the suffix to be there).

Changing this behaviour is on the roadmap for 2.0 (no ETA), but that doesn't help you now.

[eluser]Jacob F.[/eluser]
That's what I figured :/

Is that fk issue what's causing DataMapper to think there is a many-to-many relationship?

[quote author="WanWizard" date="1341654991"]That will not work, all FK's are suffixed with '_id', hardcoded.

I've looked at it, but it's going to be very complex to change this, and changing this will break all existing applications that use this (and expect the suffix to be there).

Changing this behaviour is on the roadmap for 2.0 (no ETA), but that doesn't help you now.[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB