Yii Framework Forum: got stuck at Chapter 6 - Yii Framework Forum

Jump to content

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

got stuck at Chapter 6 help

#1 User is offline   sasori 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 297
  • Joined: 05-November 10

Posted 06 November 2010 - 12:30 PM

I got stuck on Page 131 of the book. when i go to this URL,
http://yii/trackstar/index.php?r=project/view&id=2


I'm getting an error, as described at the screen shot at the bottom. but I did followed the instructions
the exact code snippets to move on., what should I do to fix this and move forward ? :unsure:
Posted Image
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 06 November 2010 - 12:38 PM

I guess somwhere in your code you do

$model = $this->loadModel();


Try this instead:

$model = $this->loadModel($_GET['id']);

0

#3 User is offline   sasori 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 297
  • Joined: 05-November 10

Posted 06 November 2010 - 12:51 PM

View PostY!!, on 06 November 2010 - 12:38 PM, said:

I guess somwhere in your code you do

$model = $this->loadModel();


Try this instead:

$model = $this->loadModel($_GET['id']);



it didn't helped :( I checked the errata page of the book from packtpub, there's none that has something to do that is related in my situation.
here's the exact code that I got I followed on the book until I got stuck
<?php

class ProjectController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
//		$this->render('view',array(
//			'model'=>$this->loadModel($id),
//		));
		$issueDataProvider = new CActiveDataProvider('Issue', array(
				'criteria' => array(
					'condition' => 'project_id=:projectId',
					'params'=> array(':projectId'=>$this->loadModel()->id),
				),
				'pagination'=>array(
					'pageSize' => 1,
				),
			));
			
	
		$this->render('view', array(
			 'model' => $this->loadModel(),
			'issueDataProvider' => $issueDataProvider,
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Project;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Project']))
		{
			$model->attributes=$_POST['Project'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Project']))
		{
			$model->attributes=$_POST['Project'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'index' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Project');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Project('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Project']))
			$model->attributes=$_GET['Project'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * 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.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Project::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='project-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}


0

#4 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 06 November 2010 - 12:54 PM

In actionView() method do:

                $this->render('view', array(
                         'model' => $this->loadModel($id),
                        'issueDataProvider' => $issueDataProvider,
                ));


1

#5 User is offline   sasori 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 297
  • Joined: 05-November 10

Posted 06 November 2010 - 01:05 PM

View PostY!!, on 06 November 2010 - 12:54 PM, said:

In actionView() method do:

                $this->render('view', array(
                         'model' => $this->loadModel($id),
                        'issueDataProvider' => $issueDataProvider,
                ));




yes, I did this also $this->loadModel($_GET['id']) , based from your first reply, but
still am getting the same error, I tried :(
I really don't know what's wrong with it, the error just says "missing argument for ProjectController::loadModel()" at line 59 where line 59 is this part of the actionView()
'params'=> array(':projectId'=>$this->loadModel()->id),


I tried
'params' => array(':projectId' => $this->loadModel($_GET['id'])),
OR
'params' => array(':projectId' => $this->loadModel($_GET['id']->id,

but when i tried this
'params' => array(':projectId' => $this->loadModel($_GET[$id]),

and refreshed the same url, the page says
Posted Image

what's wrong with it ? :(
0

#6 User is offline   sasori 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 297
  • Joined: 05-November 10

Posted 06 November 2010 - 01:08 PM

solved the problem now

	public function actionView($id)
	{
//		$this->render('view',array(
//			'model'=>$this->loadModel($id),
//		));
		$issueDataProvider = new CActiveDataProvider('Issue', array(
				'criteria' => array(
					'condition' => 'project_id=:projectId',
					'params'=> array(':projectId'=>$this->loadModel($id)->id),
				),
				'pagination'=>array(
					'pageSize' => 1,
				),
			));
			
	
		$this->render('view', array(
			 'model' => $this->loadModel($id),
			'issueDataProvider' => $issueDataProvider,
		));
	}


you're right...and i'll report this to the errata for the book
1

#7 User is offline   Bob Monteverde 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 06-November 10
  • Location:New York

Posted 06 November 2010 - 03:06 PM

If I'm not mistaken, I believe this is due to Gii auto generating different code between 1.1.2 (I think thats what the book is based on) and likely the stable release 1.1.4 (or if you are anal like me, you are using the most recent dev snapshot courtesy of dmtrs's unofficial git repo).

If you wanted to follow the book 100% step by step, using 1.1.2 for now might be a decent idea, but I think it's better to have small mistakes for you to figure out and become more involved in the actual development process.

Anyway, good luck, I'm about where you are, starting chapter 7.
1

#8 User is offline   pietrop 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 24-February 11

Posted 10 March 2011 - 06:42 AM

Just in case you are wondering, what actually did the trick:

it's the line
'params'=>array(':projectId'=>$this->loadModel($id)->id),


In the book there is no $id in loadModel().

Don't forget to call the loadModel method with the required parameter 'id'.
This can be found in the stack trace of the debug output, when the error occurs.

Cheers,
Peter
0

#9 User is offline   maha 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 29-January 11
  • Location:Thanjavur, Tamilnadu, India

Posted 15 April 2011 - 07:48 AM

Thanks Friends i got same problem, now sloved ...thanks you once again friends.... :rolleyes:
0

#10 User is offline   deanf7 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 13-April 11

Posted 18 April 2011 - 11:38 AM

Thanks for posting the fix. I hit the problem using 1.1.7. I figured there might have been a framework change, but had no real idea how to fix it The problem still doesn't seem to be in the errata, though they probably don't consider it a problem if it works with the version used in the book.

Thanks again.
0

#11 User is offline   mjmitche 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 26-May 11

Posted 29 May 2011 - 12:46 AM

I got the same error as in the original post. However, when I tried the fix that the original poster used, I got more errors

Here's the code that the OP used to solve the problem

public function actionView($id)
	{
		$issueDataProvider=new CActiveDataProvider('Issue', array(
			'criteria'=>array(
		 		'condition'=>'project_id=:projectId',
		 		'params'=>array(':projectId'=>$this->loadModel($id)->id),
		 	),
		 	'pagination'=>array(
		 		'pageSize'=>1,
		 	),
		 ));
		
		$this->render('view',array(
			'model'=>$this->loadModel($id),
			'issueDataProvider'=>$issueDataProvider,
		));



This is the error message I got when I tried the solution



]CException

Issue does not have a method named "getTypeText".

/Applications/MAMP/htdocs/framework/base/CComponent.php(266)

254     public function __call($name,$parameters)
255     {
256         if($this->_m!==null)
257         {
258             foreach($this->_m as $object)
259             {
260                 if($object->getEnabled() && method_exists($object,$name))
261                     return call_user_func_array(array($object,$name),$parameters);
262             }
263         }
264         if(class_exists('Closure', false) && $this->canGetProperty($name) && $this->$name instanceof Closure)
265             return call_user_func_array($this->$name, $parameters);
266         throw new CException(Yii::t('yii','{class} does not have a method named "{name}".',
267             array('{class}'=>get_class($this), '{name}'=>$name)));
268     }
269 
270     /**
271      * Returns the named behavior object.
272      * The name 'asa' stands for 'as a'.
273      * @param string $behavior the behavior name
274      * @return IBehavior the behavior object, or null if the behavior does not exist
275      * @since 1.0.2
276      */
277     public function asa($behavior)
278     {[/b]

	}


And this is the stack trace

Stack Trace
#0
+ /Applications/MAMP/htdocs/framework/db/ar/CActiveRecord.php(213): CComponent->__call("getTypeText", array())
#1
– /Applications/MAMP/htdocs/trackstar/protected/views/issue/_view.php(13): CActiveRecord->__call("getTypeText", array())
08 <?php echo CHtml::encode($data->description); ?>
09 <br />
10
11 <b><?php echo CHtml::encode($data->getAttributeLabel('type_id')); ?>:</b>
12 <?php //echo CHtml::encode($data->type_id); ?>
13 <?php echo CHtml::encode($data->getTypeText()); ?>
14 <br />
15
16 <b><?php echo CHtml::encode($data->getAttributeLabel('status_id')); ?>:</b>
17 <?php //echo CHtml::encode($data->status_id); ?>
18 <?php echo CHtml::encode($data->getStatusText()); ?>
#2
– /Applications/MAMP/htdocs/trackstar/protected/views/issue/_view.php(13): Issue->getTypeText()
08 <?php echo CHtml::encode($data->description); ?>
09 <br />
10
11 <b><?php echo CHtml::encode($data->getAttributeLabel('type_id')); ?>:</b>
12 <?php //echo CHtml::encode($data->type_id); ?>
13 <?php echo CHtml::encode($data->getTypeText()); ?>
14 <br />
15
16 <b><?php echo CHtml::encode($data->getAttributeLabel('status_id')); ?>:</b>
17 <?php //echo CHtml::encode($data->status_id); ?>
18 <?php echo CHtml::encode($data->getStatusText()); ?>

0

#12 User is offline   mjmitche 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 26-May 11

Posted 29 May 2011 - 01:29 AM

problem fixed. The solution of OP worked for me, but I had problem in other file.
0

#13 User is offline   LeoCS 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 01-October 11

Posted 13 October 2011 - 01:07 PM

Hi, I'm reading the book right now.

Instead of loading the model using the ID that is already known, to then get this same ID, using this code:
'params'=>array(':projectId'=>$this->loadModel($id)->id),


I passed the ID directly using this code:
'params'=>array(':projectId'=>$id,


...There is something bad with my code? ???
0

#14 User is offline   samba 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 26-November 11

Posted 26 November 2011 - 09:47 PM

Thanks for the solution. It worked like a charm.
0

#15 User is offline   hnam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 19-September 10
  • Location:BSP

Posted 17 December 2011 - 05:22 AM

If you still having issue with your display after using this solution code due to some parse error like

Parse error: syntax error, unexpected '?' in C:\xampp\htdocs\trackstar\protected\views\issue\_view.php on line 16

Plese dont blindly copy and paste from book pdf file there is a mistake of ">" in _view.php

In book this is
<b><?php echo CHtml::encode($data->getAttributeLabel('status_id')); ?:</b>
<?php echo CHtml::encode($data->status_id); ?>

alter this with following:-

<b><?php echo CHtml::encode($data->getAttributeLabel('status_id')); ?>:</b>
<?php echo CHtml::encode($data->status_id); ?>
On revolutions !
0

#16 User is offline   DenizB 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 11-May 12

Posted 12 May 2012 - 09:26 PM

Thank you so much. Solution OP worked for me too.
0

#17 User is offline   Joemaxwell 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 13-February 13
  • Location:Fiji Via Melbourne

Posted 25 March 2013 - 10:36 PM

View Postpietrop, on 10 March 2011 - 06:42 AM, said:

Just in case you are wondering, what actually did the trick:

it's the line
'params'=>array(':projectId'=>$this->loadModel($id)->id),


In the book there is no $id in loadModel().

Don't forget to call the loadModel method with the required parameter 'id'.
This can be found in the stack trace of the debug output, when the error occurs.

Cheers,
Peter

0

#18 User is offline   savariya 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 19-February 13
  • Location:india

Posted 09 April 2013 - 06:02 AM

thanks LeoCS.................. :D
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