Chapter 6 (Error on CActiveRecord.php(128))

Hi,

I am sorry if this problem has been already mentioned and solved by another member, but I am also following the Agile Web book by Jeff, and I am in chapter 6 and also seem to be stuck on an error that Yii produces. I am currently using Yii Framework 1.1.7, which is the latest stable version as of today. It seems that my problem is slightly different from the previous member’s (Sasori’s) issue, so I’ve decided to post this problem I am having.

I’ve just finished the PhpUnit testing described on page 126 (phpunit unit/ProjectTest.php), and it passed. However, when I tried to access the URL (localhost/trackstar/index.php?r=project), it gives me an error. Here is the copy of what it says:

CException

Property "Project.type_id" is not defined.

C:\Documents and Settings\kevin\My Documents\www\yii_1.1.7.r3135\framework\db\ar\CActiveRecord.php(128)

116 */

117 public function __get($name)

118 {

119 if(isset($this->_attributes[$name]))

120 return $this->_attributes[$name];

121 else if(isset($this->getMetaData()->columns[$name]))

122 return null;

123 else if(isset($this->_related[$name]))

124 return $this->_related[$name];

125 else if(isset($this->getMetaData()->relations[$name]))

126 return $this->getRelated($name);

127 else

128 return parent::__get($name);

129 }

130

131 /**

132 * PHP setter magic method.

133 * This method is overridden so that AR attributes can be accessed like properties.

134 * @param string $name property name

135 * @param mixed $value property value

136 */

137 public function __set($name,$value)

138 {

139 if($this->setAttribute($name,$value)===false)

140 {

Stack Trace

#0

  • C:\Documents and Settings\kevin\My Documents\www\yii_1.1.7.r3135\framework\db\ar\CActiveRecord.php(128): CComponent->__get("type_id")

#1

– C:\Documents and Settings\kevin\My Documents\www\trackstar\protected\views\project\_view.php(16): CActiveRecord->__get("type_id")

11 <b><?php echo CHtml::encode($data->getAttributeLabel(‘description’)); ?>:</b>

12 <?php echo CHtml::encode($data->description); ?>

13 <br />

14

15 <b><?php echo CHtml::encode($data->getAttributeLabel(‘type_id’)); ?>:</b>

16 <?php echo CHtml::encode($data->type_id); ?>

17 <br />

18

19 <b><?php echo CHtml::encode($data->getAttributeLabel(‘status_id’)); ?>:</b>

20 <?php echo CHtml::encode($data->status_id); ?>

21 <br />

What could be wrong? It seems like there is something wrong with CActiveRecord.php or _view.php file. Is this also the problem due to the Yii version difference? I’d greatly appreciate it if you could help me solve this problem.

Thank you.

Kevin

Make sure that there is type_id field in your db {project} table. {project} is your table which is attached to (model) Project.php class.

This looks like the _view file for "issue" not for "project". The basic project view file should not be referencing any types or statuses, this is only for the "issue" entity type. The _view file for projects should look something like:


<div class="view">


	<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>

	<?php echo CHtml::encode($data->name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('description')); ?>:</b>

	<?php echo CHtml::encode($data->description); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('create_time')); ?>:</b>

	<?php echo CHtml::encode($data->create_time); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('create_user_id')); ?>:</b>

	<?php echo CHtml::encode($data->create_user_id); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('update_time')); ?>:</b>

	<?php echo CHtml::encode($data->update_time); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('update_user_id')); ?>:</b>

	<?php echo CHtml::encode($data->update_user_id); ?>

	<br />




</div>

Any chance you these got switched along the way?

Jeff,

Actually, the _view file in my project directory (trackstar/protected/views/project/) is as follows:




<div class="view">


	<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->name), array('issue/view','id'=>$data->id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('description')); ?>:</b>

	<?php echo CHtml::encode($data->description); ?>

	<br />

    

    <b><?php echo CHtml::encode($data->getAttributeLabel('type_id')); ?>:</b>

    <?php echo CHtml::encode($data->type_id); ?>

    <br />

    

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

    <?php echo CHtml::encode($data->status_id); ?>

    <br />


</div>



And, I believe that the above _view file was automatically generated by Gii. This looks quite different from your _view file posted above. Once again, I used Gii of the Yii framework version 1.1.7 to generate the _view file.

elbek,

I’ve taken a look at the table, and it looks like my {project} table does not have a column name “type_id”. But, then, the Agile book (on page 63, Chapter 5) had me create a table according to the following schema:

[sql]

CREATE TABLE project (

id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,

name varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL,

description text COLLATE utf8_unicode_ci,

create_time datetime DEFAULT NULL,

create_user_id int(11) DEFAULT NULL,

update_time datetime DEFAULT NULL,

update_user_id int(11) DEFAULT NULL,

);

[/sql]

Hence, the project table does not have a column name "type_id". And, I believe that the Project model class was created based on the project table by Gii. Do I need to manually change the table column "id" to "type_id"?

Thank you for your quick responses.

Kevin

Hi we are still waiting for resolution of same issue.

plz provide solution to us.

I am working with Yii Framework 1.1.12 and stumbled upon the same issue.

Here’s how I fixed it:

The code in views/project/_view.php must be:




<?php

/* @var $this IssueController */

/* @var $data Issue */

?>


<div class="view">


	<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>

	<?php echo CHtml::encode($data->name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('description')); ?>:</b>

	<?php echo CHtml::encode($data->description); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('create_time')); ?>:</b>

	<?php echo CHtml::encode($data->create_time); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('create_user_id')); ?>:</b>

	<?php echo CHtml::encode($data->create_user_id); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('update_time')); ?>:</b>

	<?php echo CHtml::encode($data->update_time); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('update_user_id')); ?>:</b>

	<?php echo CHtml::encode($data->update_user_id); ?>

	<br />


</div>



And the code in views/issue/_view.php must be:




<?php

/* @var $this ProjectController */

/* @var $data Project */

?>


<div class="view">


	<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->name), array('issue/view', 'id'=>$data->id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('description')); ?>:</b>

	<?php echo CHtml::encode($data->description); ?>

	<br />

	

	<b><?php echo CHtml::encode($data->getAttributeLabel('type_id')); ?>:</b>

	<?php echo CHtml::encode($data->type_id); ?>

	<br />

	

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

	<?php echo CHtml::encode($data->status_id); ?>

	

</div>