Yii Framework Forum: loadModel() without parameter, is allowed? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

loadModel() without parameter, is allowed?

#1 User is offline   adipurwo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 05-March 12
  • Location:Indonesia

Posted 20 March 2012 - 01:50 AM

in chapter 5 page 129 there is code:
$issueDataProvider=new CActiveDataProvider('Issue', array(
'criteria'=>array(
'condition'=>'project_id=:projectId',
'params'=>array(':projectId'=>$this->loadModel()->id),
),
'pagination'=>array(
'pageSize'=>1,
),
));


its call loadModel() method without parameter and cause a following error:
Error 500

Missing argument 1 for ProjectController::loadModel(), called in F:\wamp\www\trackstar\protected\controllers\ProjectController.php on line 56 and defined


i think the writer was made the mistake or me that was wrong? can anyone tell me what is the problem?
0

#2 User is offline   Ivica 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 317
  • Joined: 25-May 11

Posted 20 March 2012 - 03:56 AM

Change actionView method to accept parameter $id
public function actionView($id)


and then modify CActiveDataProvider parameters as follow:
$issueDataProvider = new CActiveDataProvider('Issue', array(
    'criteria'=>array(
        'condition'=>'project_id = :projectId',
        'params'=>array(':projectId'=>$id),
    ),
    'pagination'=>array(
        'pageSize'=>1,
    ),
));

This should work.
Zurmo: Yii Powered Open Source CRM
zurmo.org
0

#3 User is offline   adipurwo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 05-March 12
  • Location:Indonesia

Posted 20 March 2012 - 04:09 AM

View PostIvica, on 20 March 2012 - 03:56 AM, said:

Change actionView method to accept parameter $id
public function actionView($id)


and then modify CActiveDataProvider parameters as follow:
$issueDataProvider = new CActiveDataProvider('Issue', array(
    'criteria'=>array(
        'condition'=>'project_id = :projectId',
        'params'=>array(':projectId'=>$id),
    ),
    'pagination'=>array(
        'pageSize'=>1,
    ),
));

This should work.


thank you Ivica. another think that i want to make sure is, why the book wrote '$this->loadModel()->$id' instead of just '$id'?
0

#4 User is offline   Ivica 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 317
  • Joined: 25-May 11

Posted 21 March 2012 - 02:58 AM

I check code that comes with book, and I think they have different solution:

/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
				$this->_model=Project::model()->findbyPk($_GET['id']);
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}


You can try to see if it works.

There are many errors in this book, however, I think it is still very good book to start with Yii framework and TDD.
Zurmo: Yii Powered Open Source CRM
zurmo.org
0

#5 User is offline   adipurwo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 05-March 12
  • Location:Indonesia

Posted 21 March 2012 - 06:34 PM

View PostIvica, on 21 March 2012 - 02:58 AM, said:

I check code that comes with book, and I think they have different solution:

/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 */
	public function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['id']))
				$this->_model=Project::model()->findbyPk($_GET['id']);
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}


You can try to see if it works.

There are many errors in this book, however, I think it is still very good book to start with Yii framework and TDD.


You right Ivica, i have check the code resource belongs to the writer in github and it turns out the loadModel() has been overwrote. but i not remember the book tell me to do it in the previous pages :D
0

#6 User is offline   Marta 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 19-December 11

Posted 24 March 2012 - 11:45 AM

The reason why the code from the book doesn't work for you is because the book was written using the older Yii 1.1.2 while you are using a newer version. The implementation of loadModel has changed in versions after version 1.1.2.
1

#7 User is offline   adipurwo 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 05-March 12
  • Location:Indonesia

Posted 23 July 2012 - 08:24 AM

View PostMarta, on 24 March 2012 - 11:45 AM, said:

The reason why the code from the book doesn't work for you is because the book was written using the older Yii 1.1.2 while you are using a newer version. The implementation of loadModel has changed in versions after version 1.1.2.


thanks! it the reason! :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users