getModel() error

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Locked
arjanflipphi
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Fri Oct 24, 2008 9:41 am

getModel() error

Post by arjanflipphi » Fri Oct 24, 2008 10:19 am

Hi,

I'm a newbie when it comes to making my own components, modules etc.
I have downloaded and PDF with an tutorial for making an restaurant reviews component
(Joomla! Extension Development!.pdf).

Every thing is gone well but now i'm at the chapter for using models, views and controllers.
This is where i'm getting an error.

these aer my scripts:

controller.php:
<?php
defined( '_JEXEC' ) or die('Restricted Access');
jimport('joomla.application.component.controller');

class ReviewController extends JController {

function display() {
$document =& JFactory::getDocument();
$viewName = JRequest::getVar('view', 'all');
$viewType = $document->getType();

$view =& $this->getView($viewName, $viewType);
$model =& $this->getModel($viewName, $viewType);

if(JError::isError( $model )) {
$view->setModel($model,true);
}

$view->setLayout('default');
$view->display();
}

function comment() {
global $option;
$row =& JTable::getInstance('commment', 'Table');

if(!$row->bind(JRequest::get('post'))) {
echo "<script> alert('".$row->getError()."');
window.history.go(-1); </script>\n";
exit();
}

$row->comment_date = date('Y-m-d H:i:s');
$user =& JFactory::getUser();

if($user->_table->id) {
$row->user_id = $user->_table->id;
}

if(!$row->store()) {
echo "<script> alert('".$row->getError()."');
window.history.go(-1); </script>\n";
exit();
}

$this->setRedirect('index.php?option='.$option.'&id='.$row->review_id.'$view=review', 'Comment Added');
}
}
?>

reviews.php:
<?php
defined( '_JEXEC' ) or die ( 'Restricted Access' );
require_once( JPATH_COMPONENT.DS.'controller.php');
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_reviews'.DS.'tables');

echo '<div class="componentheading">Restaurant Reviews</div>';
$controller = new ReviewController();
$controller->execute( JRequest::getVar('task' ));
$controller->redirect();
?>

in the folder of the component i have an models en views folder.
in the views folder there are 2 folders named 'all' and 'review'.
there are 2 files with the same name in them

this is the file in all
<?php
defined( '_JEXEC' ) or die ( 'Restricted Access' );
jimport( 'joomla.application.component.view');

class ReviewViewAll extends JView {

function display($tpl = null) {
global $option;

$model =& $this->getModel();
$list = $model->getList();

for($i = 0; $i < count($list); $i++) {
$row =& $list[$i];
$row->link = JRoute::_('index.php?option='.$option.'&id='.$row->id.'&view=review');
}
$this->assignRef('list', $list);
parent::display($tpl);
}
}
?>

and this is the one in review:
<?php
defined( '_JEXEC' ) or die ( 'Restricted Access' );
jimport( 'joomla.application.component.view');

class ReviewViewReview extends JView {

function display($tpl = null) {
global $option, $mainframe;

$user =& JFactory::getDBO();
$model =& $this->getModel();
$review = $model->getReview();
$comments = $model->getComments();
$pathway =& $mainframe->getPathWay();
$backlink = JRoute::_('index.php?option='.$option.'&view=all');

$review->review_date = JHTML::Date($review->review_date);
if($review->smoking == 1) {
$review->smoking = 'Yes';
} else {
$review->smoking = 'No';
}

for($i = 0; $i < count($comments); $i++) {
$row =& $comments[$i];
$row->comment_date = JHTML::Date($row->comment_date);
}
$pathway->addItem($review->name, '');

$this->assignRef('review', $review);
$this->assignRef('comments', $comments);
$this->assignRef('backlink', $backlink);
$this->assignRef('option', $option);
$this->assignRef('name', $user->name);
parent::display($tpl);
}
}
?>

The error i'm getting is:
Call to a member function getReview() on a non-object in C:\xampp\htdocs\Joomla\components\com_reviews\views\review\view.html.php on line 12

can someone help me with this error??

thnx alot

User avatar
bass28
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 137
Joined: Mon Apr 17, 2006 5:11 pm

Re: getModel() error

Post by bass28 » Mon Oct 27, 2008 1:41 pm

First, do you actually have a model in the models folder? If so you can use $this->getModule('model_name') to load a specific model.

secteur
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 146
Joined: Tue Nov 08, 2005 9:32 am
Location: Malaysia

Re: getModel() error

Post by secteur » Mon Oct 27, 2008 2:50 pm

hi,

Your call for the model is wrong because of $viewType. The second parameter should be your component prefix.
Or, as bass68 said, leave it blank. But beware of default naming, if you call this from a controller with a different name it won't work.
To be safe, use

Code: Select all

$model = $this->getModel( 'modelname', 'prefixModel' ); // & not needed with php5
this grabs the model called PrefixModelModelname as in "parameter 2 plus parameter 1" builds the class name.

Hope this helps.

sulakshana
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Wed Oct 08, 2008 10:49 am

Re: getModel() error

Post by sulakshana » Fri Dec 26, 2008 9:56 am

HI
On the same code i am getting this type of error.


Fatal error: Call to a member function bind() on a non-object in C:\wamp\www\my-joomla1.5\components\com_reviews\reviews.php on line 72

please help

mlaitinen
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Wed Jul 02, 2008 11:48 am

Re: getModel() error

Post by mlaitinen » Fri Jan 16, 2009 1:34 pm

I think you have a little typo around here.
$row =& JTable::getInstance('commment', 'Table');
I'm having the same problem (following the book written by J.LeBlanc), but I don't have a typo..

aimeeabbott
Joomla! Apprentice
Joomla! Apprentice
Posts: 11
Joined: Fri Oct 19, 2007 4:19 am

Re: getModel() error

Post by aimeeabbott » Wed Mar 28, 2012 4:14 am

I realize this is a very old thread and maybe no one is even watching it any more but I am working through Joseph LeBlanc's book on Joomla Extension Development and I am wondering how you got around the error that comes up with view being embedded in the classname.

JView::getName() : Your classname contains the substring 'view'. This causes problems when extracting the classname from the name of your objects view. Avoid Object names with the substring 'view'.

I can see where in application/component/view.php this is happening. But, I can't really figure out where all I would need to remove the "view" part of the class ReviewViewReview. If I just change it to ReviewViewInfo, for instance I then get the error:

500 - View class not found [class, file]: reviewViewreview,

But, I can't see anywhere in my code where ReviewViewReview is even being called.

The component does work, and this only seems to be a warning, not a hard error. But, I would like to get rid of it. It does show up on the page in a red box.


Locked

Return to “Joomla! 1.5 Coding”