我是yii1.1.5的新手。我在tbl_project表中添加了一个新文件,并更改了模型和视图。但是,当我想保存输入到mysql的数据时,出现错误,提示“未定义属性“ Project.balance”。“
我试图清理收银员,但无法解决此问题。
我的model.php
<?php
/**
* This is the model class for table "tbl_project".
*
* The followings are the available columns in table 'tbl_project':
* @property integer $id
* @property string $name
* @property string $description
* @property string balance
* @property string $create_time
* @property integer $create_user_id
* @property string $update_time
* @property integer $update_user_id
*/
class Project extends TrackStarActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @return Project the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tbl_project';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('balance,create_user_id, update_user_id', 'numerical', 'integerOnly'=>true),
array('name', 'length', 'max'=>128),
array('description,balance,create_time, update_time', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, name, description,balance,create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),
array('name', 'required'),
array('description', 'required'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'issues'=>array(self::HAS_MANY, 'Issue', 'project_id'),
'users'=>array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id,user_id)'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'name' => '姓名',
'description' => '描述',
'balance'=>'余额',
'create_time' => '创建时间',
'create_user_id' => '创建用户ID',
'update_time' => '更新时间',
'update_user_id' => '更新用户ID',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('description',$this->description,true);
$criteria->compare('balance',$this->balance,true);
$criteria->compare('create_time',$this->create_time,true);
$criteria->compare('create_user_id',$this->create_user_id);
$criteria->compare('update_time',$this->update_time,true);
$criteria->compare('update_user_id',$this->update_user_id);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
public function getUserOptions()
{
$usersArray=CHtml::listData($this->users,'id','username');
return $usersArray;
}
public function associateUserToRole($role, $user_id)
{
$sql = "INSERT INTO tbl_project_user_role (project_id, user_id,role) VALUES (:project_id, :user_id, :role)";
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":project_id", $this->id, PDO::PARAM_INT);
$command->bindValue(":user_id", $user_id, PDO::PARAM_INT);
$command->bindValue(":role", $role, PDO::PARAM_STR);
return $command->execute();
}
public function removeUserFromRole($role, $user_id)
{
$sql="DELETE FROM tbl_project_user_role WHERE project_id=:project_id AND user_id=:user_id AND role=:role";
$command=Yii::app()->db->createCommand($sql);
$command->blindValue(":project_id",$this->id, PDO::PARAM_INT);
$command->blindValue(":user_id", $user_id, PDO::RARAM_INT);
$command->blindValue(":role", $role, PDO::PARAM_STR);
return $command->execute();
}
/* public function isUserInRole($role)
{
$sql="SELECT role FROM tbl_project_user_role WHERE project_id=:project_id AND user_id=:user_id AND role=:role";
$command=Yii::app()->db->createCommand($sql);
$command->bindValue(":project_id",$this->id, PDO::PARAM_INT);
$command->bindValue(":user_id", Yii::app()->user->getId(),PDO::PARAM_INT );
$command->bindValue(":role",$role, PDO::PAPAM_STR);
return $command->execute()==1?true:false;
} */
public function isUserInRole($role)
{
$sql = "SELECT role FROM tbl_project_user_role WHERE project_id=:projectId AND user_id=:userId AND role=:role";
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":projectId", $this->id, PDO::PARAM_INT);
$command->bindValue(":userId", Yii::app()->user->getId(),PDO::PARAM_INT);
$command->bindValue(":role", $role, PDO::PARAM_STR);
return $command->execute()==1 ? true : false;
}
public static function getUserRoleOptions()
{
return CHtml::listData(Yii::app()->authManager->getRoles(),'name','name');
}
public function associateUserToProject($user)
{
$sql="INSERT INTO tbl_project_user_assignment(project_id,user_id) VALUES (:project_id,:user_id)";
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":project_id", $this->id, PDO::PARAM_INT);
$command->bindValue(":user_id", $user->id, PDO::PARAM_INT);
return $command->execute();
}
/* public function isUserInProject($user)
{
$sql="SELECT user_id FROM tbl_project_user_assignment WHERE project_id=:project_id AND user_id=:user_id";
$command=Yii::app()->db->createCommand($sql);
var_dump($command);exit;
$command->blindValue(":project_id",$this->id,PDO::PARAM_INT);
$command->blindValue(":user_id",$user->id,PDO::PARAM_INT);
return $command->execute()==1 ? true:false;
} */
public function isUserInProject($user)
{
$sql ="SELECT user_id FROM tbl_project_user_assignment WHERE project_id=:projectId AND user_id=:userId";
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":projectId", $this->id, PDO::PARAM_INT);
$command->bindValue(":userId", $user->id, PDO::PARAM_INT);
return $command->execute()==1;
}
}
My view.php
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'project-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'姓名'); ?>
<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'余额'); ?>
<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'balance'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'描述'); ?>
<?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
My Mysql:
-- phpMyAdmin SQL Dump
-- version 4.6.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jun 27, 2019 at 10:15 AM
-- Server version: 5.7.14
-- PHP Version: 5.6.25
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `yxbeauty`
--
-- --------------------------------------------------------
--
-- Table structure for table `tbl_project`
--
CREATE TABLE `tbl_project` (
`id` int(11) NOT NULL,
`name` varchar(128) CHARACTER SET utf8 DEFAULT NULL,
`description` text CHARACTER SET utf8,
`create_time` datetime DEFAULT NULL,
`create_user_id` int(11) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
`update_user_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `tbl_project`
--
INSERT INTO `tbl_project` (`id`, `name`, `description`, `create_time`, `create_user_id`, `update_time`, `update_user_id`) VALUES
(1, '饼子脸', '测试', NULL, NULL, '2019-06-27 15:07:23', 12),
(2, 'Test Project15', '测试', '2018-12-16 13:56:34', 5, '2018-12-16 14:38:28', 12),
(3, '天天华盛顿', '天天华盛顿', '2019-06-27 15:42:56', 12, '2019-06-27 15:42:56', 12);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tbl_project`
--
ALTER TABLE `tbl_project`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `tbl_project`
--
ALTER TABLE `tbl_project`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
CException
Description
Property "Project.balance" is not defined.
Source File
E:\projects\yxbeauty\framework\db\ar\CActiveRecord.php(113)
00101: */
00102: public function __get($name)
00103: {
00104: if(isset($this->_attributes[$name]))
00105: return $this->_attributes[$name];
00106: else if(isset($this->getMetaData()->columns[$name]))
00107: return null;
00108: else if(isset($this->_related[$name]))
00109: return $this->_related[$name];
00110: else if(isset($this->getMetaData()->relations[$name]))
00111: return $this->getRelated($name);
00112: else
00113: return parent::__get($name);
00114: }
00115:
00116: /**
00117: * PHP setter magic method.
00118: * This method is overridden so that AR attributes can be accessed like properties.
00119: * @param string $name property name
00120: * @param mixed $value property value
00121: */
00122: public function __set($name,$value)
00123: {
00124: if($this->setAttribute($name,$value)===false)
00125: {
Stack Trace
#0 E:\projects\yxbeauty\framework\db\ar\CActiveRecord.php(113): CComponent->__get('balance')
#1 E:\projects\yxbeauty\framework\validators\CNumberValidator.php(56): CActiveRecord->__get('balance')
#2 E:\projects\yxbeauty\framework\validators\CValidator.php(184): CNumberValidator->validateAttribute(Object(Project), 'balance')
#3 E:\projects\yxbeauty\framework\base\CModel.php(150): CValidator->validate(Object(Project), NULL)
#4 E:\projects\yxbeauty\framework\db\ar\CActiveRecord.php(766): CModel->validate(NULL)
#5 E:\projects\yxbeauty\yxbeauty\protected\controllers\ProjectController.php(100): CActiveRecord->save()
#6 E:\projects\yxbeauty\framework\web\actions\CInlineAction.php(57): ProjectController->actionCreate()
#7 E:\projects\yxbeauty\framework\web\CController.php(300): CInlineAction->run()
#8 E:\projects\yxbeauty\framework\web\filters\CFilterChain.php(133): CController->runAction(Object(CInlineAction))
#9 E:\projects\yxbeauty\framework\web\filters\CFilter.php(41): CFilterChain->run()
#10 E:\projects\yxbeauty\framework\web\CController.php(1084): CFilter->filter(Object(CFilterChain))
#11 E:\projects\yxbeauty\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))
#12 E:\projects\yxbeauty\framework\web\filters\CFilterChain.php(130): CInlineFilter->filter(Object(CFilterChain))
#13 E:\projects\yxbeauty\framework\web\CController.php(283): CFilterChain->run()
#14 E:\projects\yxbeauty\framework\web\CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)
#15 E:\projects\yxbeauty\framework\web\CWebApplication.php(324): CController->run('create')
#16 E:\projects\yxbeauty\framework\web\CWebApplication.php(121): CWebApplication->runController('project/create')
#17 E:\projects\yxbeauty\framework\base\CApplication.php(135): CWebApplication->processRequest()
#18 E:\projects\yxbeauty\yxbeauty\index.php(13): CApplication->run()
#19 {main}
2019-06-27 10:02:05 Apache/2.4.23 (Win32) PHP/5.6.25 Yii Framework/1.1.5
答案 0 :(得分:0)
我通过
解决了1。将该字段添加到物理表中(定义数据类型,utf8等)。 2.更改模型文件。 3.更改视图文件。
我有两个错误
1。我必须在模型文件中添加一个公共属性,如下所示。
class Project extends TrackStarActiveRecord
{
public $balance;
...
2。我的视图文件输入错误。
<div class="row">
<?php echo $form->labelEx($model,'余额'); ?>
<?php echo $form>textField($model,'balance', // here must be 'balance'
array('size'=>60,'maxlength'=>128));?>
<?php echo $form->error($model,'balance'); ?>
</div>