how to fetch another model inside a view

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
ghostrifle
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Wed Aug 22, 2007 8:41 am
Location: Germany
Contact:

how to fetch another model inside a view

Post by ghostrifle » Wed Oct 03, 2007 10:28 pm

Hi there,

I'm coding at present the admin interface of a demo component. In the view works so far... but the model (database table) is using another table too... so I thought I write another model to store the data of the second table. Storing the data works without any problems... but now getting them in the view.

Here's the structure:

Code: Select all

 *advertisement
 ** advertisement images - holding a foreign key to advertisement


Well... I'm just getting a null pointer when I try to fetch the advertisement image model:

Code: Select all

<?php
   function display($tpl = null)
   {
      //taking the advertisement
       $rl_ad        =& $this->get('Data');
       $isNew        = ($rl_ad->id < 1);
      
      // and now the images via the adiamage model...
      $adimage_model = $this->getModel('adimage'); // here I'm getting the null pointer !
      $rl_ad_images =& $adimage_model->getAllOfAdvertisement($rl_ad->id);

       $text = $isNew ? JText::_( 'New' ) : JText::_( 'Edit' );
       JToolBarHelper::title(   JText::_( 'Advertisement' ).': <small><small>[ ' . $text.' ]</small></small>' );
       JToolBarHelper::save();
       if ($isNew)  {
           JToolBarHelper::cancel();
       } else {
           // for existing items the button is renamed `close`
           JToolBarHelper::cancel( 'cancel', 'Close' );
       }
      
      $body_editor =& JFactory::getEditor();
      
      $this->assignRef( 'body_editor', $body_editor );
       $this->assignRef('rl_ad', $rl_ad);
       $this->assignRef('rl_ad_images', $rl_ad_images);
       parent::display($tpl);
   }
?>


I hope, someone can help me out....

Bye, Alex
o

zian974
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Mon Sep 10, 2007 9:41 am

Re: how to fetch another model inside a view

Post by zian974 » Thu Oct 04, 2007 11:49 am

Hi,

where did you put (the folder) your model "adimage"?

ghostrifle
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Wed Aug 22, 2007 8:41 am
Location: Germany
Contact:

Re: how to fetch another model inside a view

Post by ghostrifle » Thu Oct 04, 2007 12:00 pm

Hi  :)

It's in the same directory as the other models from my component:

Code: Select all

models:
 adimage.php
 redlight.php
 redlights.php


Bye, Alex
o

zian974
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Mon Sep 10, 2007 9:41 am

Re: how to fetch another model inside a view

Post by zian974 » Thu Oct 04, 2007 12:06 pm

Re,

And you don't have any error messages???. Joomla find the class???

sorry but i don't know what you mean when you say
here I'm getting the null pointer

ghostrifle
Joomla! Apprentice
Joomla! Apprentice
Posts: 16
Joined: Wed Aug 22, 2007 8:41 am
Location: Germany
Contact:

Re: how to fetch another model inside a view

Post by ghostrifle » Thu Oct 04, 2007 12:34 pm

Well,

I don't get any error message except that I can't call the method getAllOfAdvertisement() of the model I wanted to fetch.

Code: Select all

$adimage_model = $this->getModel('adimage'); 


$adimage_model is NULL... an emtpy var ! Therefore I say I'm receiving a null pointer...

Bye,
Alex
o

zian974
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Mon Sep 10, 2007 9:41 am

Re: how to fetch another model inside a view

Post by zian974 » Fri Oct 05, 2007 5:06 am

Hello,

The model you use in a view must be registred in the controller.

You can see all the available model in the view with this code in the XXXViewXXX class:

Code: Select all

echo '<pre>';
print_r($this->_models);
echo '</pre>';



The controller:

Code: Select all

class XXXController extends JController
{
   
  /**
   * Constructeur de la classe.
   *
  */
  function __construct()
  {
    parent::__construct();
  }
 /**
  * Method to display the view
  *
  * @access    public
 */
 function display()
 {   
   $adimage_model =& $this->getModel('adimage');

   $view = & $this->getView('name of your view', 'html');
   /* or reference to the default view */
   $view = &$this->getView('', 'html');

   /* assign to the view another model */
   $view->setModel($adimage_model,'false');   
     
   // Affichage
   parent::display();
 }



The view:

Code: Select all

class XXXViewXXX extends JView
{
 
  function display( $tpl = null )
  { 
   $model = &$this->getModel('adImage');
   $x = $model->getAllOfAdvertisement($rl_ad->id);
 
    /*
     * Affichage de la vue
     */
    parent::display($tpl);
  }
 

I hope this will help you.

But i have one question.

In the controller, to get the default view, i use the following code

Code: Select all

$view = &$this->getView('', 'html');


But in the class JController, i saw that the 'type' parameter is optionnal. But if i try

Code: Select all

$view = &$this->getView();
it doesn't work.

PS: sorry for the langage, i'm french and it's difficult to explain in english.
Last edited by zian974 on Fri Oct 05, 2007 5:22 am, edited 1 time in total.


Post Reply