2

Here is what I am try'n to do. I'm not sure which parts of the code I mite need to copy here, so let me start with some oversimplified pseudo-code to get going.

I have a main component (com_main) with model, Table, etc all working. From my view.html I can call

$modelTU = $this->getModel('TimeUnits');
$modelTU->GetSchedule($date,...);

The TU Model

public function GetSchedule($date,...){
    $query = self::getQuery();
    $items = $this->_getList((string) $query...);

    if (empty($items)){  //if non found, create new from template
        $templateModel = $this->getModel('Templates');
        $templateModel->createFromTemplate($date);   //create TU's from a tempalte

     }
     return $items;
}

The Template Model ... public function createFromTemplate($date,...){

    //****THIS fails (or =FALSE) if called from module
    //This works fine when called from the component!!
    $modelTU =  JModelLegacy::getInstance('TimeUnit', COM_MODEL_PREFIX); //THIS FAILS

    $query = self::getQueryTempalte(....);
    $items = $this->_getList((string) $query...);
    foreach ($items as $item){
       $valTimeUnit = array(
            'duration' => $item->duration,
            'price' => $item->price
             ...
        );
        $modelTU->save($valTimeUnit);   
        //I also tried store bind here
        $tblTu = $modelTU->getTable();
        $tblTu->bind(valTimeUnit);  //table = False !!!

    }
 }

I don't know how else to explain this. The bottom line: I load timeunits (TU) ... and if for the given date non exist, I call a template model to load a set of template units to be cloned into TU's

This works fine when called from within the component

If I import the models via in to the module require_once( JPATH_ROOT . '/components/com_main/models/timeunit.php'); etc

it works, I can LOAD Timeunits if they exist just find

It also calls the templateModel to clone new once until I my marker **** The final TimeUnit Model ->Save, or ->getTable(); fails

But only when called from the module

What Am I doing wrong / violating any rules? Namespace imports I don't understand how It can load and work the first two model levels, and then fail to load the last one. I already figured I can't use 'JPATH_COMPONENT_SITE' to require files within my component if I want them to work from my module, I use 'JPATH_ROOT . '/components/com_byteitbooking/'.

Any pointers what I mite miss?

Thanks

EDIT: additional code I made this test, in my timeunit model extending JModelAdmin

public function getTable($type = self::THIS_CLASS_NAME, $prefix = COM_TABLE_PREFIX,   $config = array()) {
    dumpMessage("-> admin.MODEL.TimeUnit.getTable()");
    dumpMessage("-> $type,$prefix");
    dump($config,'$config');
    $tb = JTable::getInstance($type, $prefix, $config);
    dump($tb,'$tb');
    return $tb;
    //return JTable::getInstance($type, $prefix, $config);
}

results 1st called from component, 2nd from module ...the vars are resolved correctly

  • admin.MODEL.TimeUnit.getTable()
  • TimeUnit,ByteitBookingTable
  • [array] $config = (empty)
  • [ByteitBookingTableTimeUnit object] $tb

VS

  • admin.MODEL.TimeUnit.getTable()
  • TimeUnit,ByteitBookingTable
  • [array] $config = (empty)
  • [boolean] $tb = FALSE

Do I have to copy the models/tables into the module in order to use them? Is there some kind of restriction in Joomla I am missing?

  • COM_MODEL_PREFIX is this constant defined by your component? – Gaurav Sep 26 '14 at 4:22
  • Yes, I was tired of all the strings, so this holds 'ByteitBookingTable' – Andreas Sep 26 '14 at 23:03
  • Try after passing prefix as string instead of constant. Make sure the required model class is loaded. – Gaurav Sep 27 '14 at 10:36
  • @Gaurav: I updated my code to include my modified GetTable(). As you can see in the results, BOTH times type and prefix are resolved ...yet in one, the table is null – Andreas Sep 27 '14 at 16:08
  • COM_TABLE_PREFIX, Where is it defined? – Gaurav Sep 27 '14 at 20:53
2

I think you have to include the paths both for model and table:

JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_main/models', 'ByteitBookingModel');

$modelTU = JModelLegacy::getInstance('TimeUnit', 'ByteitBookingModel');
$modelTU->addTablePath(JPATH_ADMINISTRATOR . '/components/com_main/tables');
  • I will try your suggestion as soon as I get some free time @Dmirty. I am sure I'm either missing an include, or somewhere it uses a false value. – Andreas Oct 1 '14 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.