Page 1 of 1

how to fetch another model inside a view

Posted: Wed Oct 03, 2007 10:28 pm
by ghostrifle
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

Re: how to fetch another model inside a view

Posted: Thu Oct 04, 2007 11:49 am
by zian974
Hi,

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

Re: how to fetch another model inside a view

Posted: Thu Oct 04, 2007 12:00 pm
by ghostrifle
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

Re: how to fetch another model inside a view

Posted: Thu Oct 04, 2007 12:06 pm
by zian974
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

Re: how to fetch another model inside a view

Posted: Thu Oct 04, 2007 12:34 pm
by ghostrifle
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

Re: how to fetch another model inside a view

Posted: Fri Oct 05, 2007 5:06 am
by zian974
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.