简体   繁体   English

Yii 框架没有将字段的值保存到数据库

[英]Yii Framework not saving value for field to DB

I am fairly new to PHP, and I am working on a project that uses the Yii framework.我对 PHP 相当陌生,我正在开发一个使用 Yii 框架的项目。 I have added a new field to our database, and for some reason it isn't being passed to the database.我在我们的数据库中添加了一个新字段,但由于某种原因它没有被传递到数据库。

ALTER TABLE profiles
ADD organization_id INT(10);

I altered the validation, which I thought was the issue to include the field.我更改了验证,我认为这是包含该字段的问题。

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
            array('participant_list_id, organization_id', 'numerical', 'integerOnly'=>true),
            array('participant_list_name, organization_id', 'required'),
            array('participant_list_name', 'unique', 'message' => 'The name already exists.'),
            array('participant_list_name', 'length', 'max'=>64),
            array('participant_list_desc', 'length', 'max'=>255),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('participant_list_id, organization_id, participant_list_name, participant_list_desc', 'safe', 'on'=>'search'),
            array('input_type','safe'),
    );
}

I included it in the form on the page by using the following:我使用以下内容将其包含在页面上的表单中:

<?php echo $form->hiddenField($model,'organization_id',array('value'=>Yii::app()->getModule('user')->user()->profile->organization_id)); ?>

which renders as:呈现为:

<input value="1" name="ParticipantList[organization_id]" id="ParticipantList_organization_id" type="hidden">

Clearly there is something I am missing, but I am not sure what it could be.显然,我缺少某些东西,但我不确定它可能是什么。 I made the field "not null" in the DB, and I get an error saying that the insert failed, because organization_id requires a value.我在数据库中创建了“非空”字段,但我收到一条错误消息,指出插入失败,因为 organization_id 需要一个值。

I discovered the issue.我发现了这个问题。 Someone changed the method being called, and replaced $model->attributes=$_POST['ParticipantList'];有人改变了被调用的方法,并替换了 $model->attributes=$_POST['ParticipantList']; with $model->participant_list_name=($_POST['ParticipantList']['participant_list_name'‌​]);与 $model->participant_list_name=($_POST['ParticipantList']['participant_list_name'‌ ]); so it was ignoring all the other fields.所以它忽略了所有其他领域。 As it stands we weren't able to add all the attributes, because it caused it to save some nested data that was being validated, so I explicitly added the org ID.就目前而言,我们无法添加所有属性,因为这会导致它保存一些正在验证的嵌套数据,因此我明确添加了组织 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM