2

rules()次のメソッドを持つ CActiveRecord を拡張するモデルがあります。

/**
 * @return array validation rules for model attributes.
 */
public function rules()
{
    return array(
        array('name, locationId', 'required'),
        array('locationId', 'numerical', 'integerOnly'=>true),
        array('name', 'length', 'max'=>1024),
        array('name', 'safe'),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, name, description, locationId', 'safe', 'on'=>'search'),
    );
}

それぞれのコントローラーのactionCreate()メソッドは次のとおりです。

public function actionCreate()
{
    $model = new Destination;

    if ( isset($_POST['Destination']) )
    {
      $model->attributes = $_POST['Destination'];

      // logging statement!
      Yii::log("Name: {$model->name}\nLocationID: {$model->locationId}\nAll attribs:\n".CVarDumper::dumpAsString($model->attributes), 'warning', 'application.controllers.DestinationController.actionCreate');

      if ( $model->save() )
        $this->redirect(array('view', 'id'=>$model->id));
    }

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

残念ながら、このアクションで新しいレコードを作成しようとすると、フォームに設定されているにもかかわらず、空にできないというエラーが発生しnameますlocationId。上記の のロギング ステートメントからの出力はactionCreate()、次のようになります。

2012/08/14 18:04:51 [warning]     [application.controllers.DestinationController.actionCreate] Name:
LocationID:
All attribs:
array
(
    'name' => 'Test Name'
    'description' => 'Test Description'
    'locationId' => '93'
    'contactId' => ''
    'accessId' => ''
    'fullDistance' => ''
)

ご覧のとおり、ダンプする$model->attributesと正しいデータが表示されますが、個々の属性を取得しようとすると (または を呼び出す$model->nameなど$model->locationId)、何も返されません。save()validate()などの呼び出しでこれを追跡しましたが、これがとのrequiredルールが失敗する原因になっているようです。namelocationId

何か案は?

編集:追加のログ情報

    2012/08/15 16:58:03 [info] [application] Model:
Destination#1
(
    [CActiveRecord:_md] => CActiveRecordMetaData#2
    (
        [tableSchema] => CMysqlTableSchema#3
        (
            [schemaName] => null
            [name] => 'destination'
            [rawName] => '`destination`'
            [primaryKey] => 'id'
            [sequenceName] => ''
            [foreignKeys] => array
            (
                'accessId' => array
                (
                    '0' => 'access'
                    '1' => 'id'
                )
                'contactId' => array
                (
                    '0' => 'contact'
                    '1' => 'id'
                )
                'locationId' => array
                (
                    '0' => 'location'
                    '1' => 'id'
                )
            )
            [columns] => array
            (
                'id' => CMysqlColumnSchema#4
                (
                    [name] => 'id'
                    [rawName] => '`id`'
                    [allowNull] => false
                    [dbType] => 'int(11)'
                    [type] => 'integer'
                    [defaultValue] => null
                    [size] => 11
                    [precision] => 11
                    [scale] => null
                    [isPrimaryKey] => true
                    [isForeignKey] => false
                    [autoIncrement] => true
                    [CComponent:_e] => null
                    [CComponent:_m] => null
                )
                'name' => CMysqlColumnSchema#5
                (
                    [name] => 'name'
                    [rawName] => '`name`'
                    [allowNull] => false
                    [dbType] => 'varchar(1024)'
                    [type] => 'string'
                    [defaultValue] => null
                    [size] => 1024
                    [precision] => 1024
                    [scale] => null
                    [isPrimaryKey] => false
                    [isForeignKey] => false
                    [autoIncrement] => false
                    [CComponent:_e] => null
                    [CComponent:_m] => null
                )
                'description' => CMysqlColumnSchema#6
                (
                    [name] => 'description'
                    [rawName] => '`description`'
                    [allowNull] => true
                    [dbType] => 'text'
                    [type] => 'string'
                    [defaultValue] => null
                    [size] => null
                    [precision] => null
                    [scale] => null
                    [isPrimaryKey] => false
                    [isForeignKey] => false
                    [autoIncrement] => false
                    [CComponent:_e] => null
                    [CComponent:_m] => null
                )
                'locationId' => CMysqlColumnSchema#7
                (
                    [name] => 'locationId'
                    [rawName] => '`locationId`'
                    [allowNull] => false
                    [dbType] => 'int(11)'
                    [type] => 'integer'
                    [defaultValue] => null
                    [size] => 11
                    [precision] => 11
                    [scale] => null
                    [isPrimaryKey] => false
                    [isForeignKey] => true
                    [autoIncrement] => false
                    [CComponent:_e] => null
                    [CComponent:_m] => null
                )
                'contactId' => CMysqlColumnSchema#8
                (
                    [name] => 'contactId'
                    [rawName] => '`contactId`'
                    [allowNull] => true
                    [dbType] => 'int(11)'
                    [type] => 'integer'
                    [defaultValue] => null
                    [size] => 11
                    [precision] => 11
                    [scale] => null
                    [isPrimaryKey] => false
                    [isForeignKey] => true
                    [autoIncrement] => false
                    [CComponent:_e] => null
                    [CComponent:_m] => null
                )
                'accessId' => CMysqlColumnSchema#9
                (
                    [name] => 'accessId'
                    [rawName] => '`accessId`'
                    [allowNull] => true
                    [dbType] => 'int(11)'
                    [type] => 'integer'
                    [defaultValue] => null
                    [size] => 11
                    [precision] => 11
                    [scale] => null
                    [isPrimaryKey] => false
                    [isForeignKey] => true
                    [autoIncrement] => false
                    [CComponent:_e] => null
                    [CComponent:_m] => null
                )
                'fullDistance' => CMysqlColumnSchema#10
                (
                    [name] => 'fullDistance'
                    [rawName] => '`fullDistance`'
                    [allowNull] => true
                    [dbType] => 'int(11)'
                    [type] => 'integer'
                    [defaultValue] => null
                    [size] => 11
                    [precision] => 11
                    [scale] => null
                    [isPrimaryKey] => false
                    [isForeignKey] => false
                    [autoIncrement] => false
                    [CComponent:_e] => null
                    [CComponent:_m] => null
                )
            )
            [CComponent:_e] => null
            [CComponent:_m] => null
        )
        [columns] => array
        (
            'id' => CMysqlColumnSchema#4(...)
            'name' => CMysqlColumnSchema#5(...)
            'description' => CMysqlColumnSchema#6(...)
            'locationId' => CMysqlColumnSchema#7(...)
            'contactId' => CMysqlColumnSchema#8(...)
            'accessId' => CMysqlColumnSchema#9(...)
            'fullDistance' => CMysqlColumnSchema#10(...)
        )
        [relations] => array
        (
            'location' => CBelongsToRelation#11
            (
                [joinType] => 'LEFT OUTER JOIN'
                [on] => ''
                [alias] => null
                [with] => array()
                [together] => null
                [scopes] => null
                [name] => 'location'
                [className] => 'Location'
                [foreignKey] => 'locationId'
                [select] => '*'
                [condition] => ''
                [params] => array()
                [group] => ''
                [join] => ''
                [having] => ''
                [order] => ''
                [CComponent:_e] => null
                [CComponent:_m] => null
            )
            'activities' => CManyManyRelation#12
            (
                [limit] => -1
                [offset] => -1
                [index] => null
                [through] => null
                [joinType] => 'LEFT OUTER JOIN'
                [on] => ''
                [alias] => null
                [with] => array()
                [together] => null
                [scopes] => null
                [name] => 'activities'
                [className] => 'Activity'
                [foreignKey] => 'destinationHasActivity(destinationId, activityId)'
                [select] => '*'
                [condition] => ''
                [params] => array()
                [group] => ''
                [join] => ''
                [having] => ''
                [order] => ''
                [CComponent:_e] => null
                [CComponent:_m] => null
            )
            'attributes' => CManyManyRelation#13
            (
                [limit] => -1
                [offset] => -1
                [index] => null
                [through] => null
                [joinType] => 'LEFT OUTER JOIN'
                [on] => ''
                [alias] => null
                [with] => array()
                [together] => null
                [scopes] => null
                [name] => 'attributes'
                [className] => 'Attribute'
                [foreignKey] => 'destinationHasAttribute(destinationId, attributeId)'
                [select] => '*'
                [condition] => ''
                [params] => array()
                [group] => ''
                [join] => ''
                [having] => ''
                [order] => ''
                [CComponent:_e] => null
                [CComponent:_m] => null
            )
            'surfaces' => CManyManyRelation#14
            (
                [limit] => -1
                [offset] => -1
                [index] => null
                [through] => null
                [joinType] => 'LEFT OUTER JOIN'
                [on] => ''
                [alias] => null
                [with] => array()
                [together] => null
                [scopes] => null
                [name] => 'surfaces'
                [className] => 'Surface'
                [foreignKey] => 'destinationHasSurface(destinationId, surfaceId)'
                [select] => '*'
                [condition] => ''
                [params] => array()
                [group] => ''
                [join] => ''
                [having] => ''
                [order] => ''
                [CComponent:_e] => null
                [CComponent:_m] => null
            )
            'images' => CManyManyRelation#15
            (
                [limit] => -1
                [offset] => -1
                [index] => null
                [through] => null
                [joinType] => 'LEFT OUTER JOIN'
                [on] => ''
                [alias] => null
                [with] => array()
                [together] => null
                [scopes] => null
                [name] => 'images'
                [className] => 'Image'
                [foreignKey] => 'destinationHasImage(destinationId, imageId)'
                [select] => '*'
                [condition] => ''
                [params] => array()
                [group] => ''
                [join] => ''
                [having] => ''
                [order] => ''
                [CComponent:_e] => null
                [CComponent:_m] => null
            )
        )
        [attributeDefaults] => array()
        [CActiveRecordMetaData:_model] => Destination#16
        (
            [CActiveRecord:_md] => CActiveRecordMetaData#2(...)
            [CActiveRecord:_new] => false
            [CActiveRecord:_attributes] => array()
            [CActiveRecord:_related] => array()
            [CActiveRecord:_c] => null
            [CActiveRecord:_pk] => null
            [CActiveRecord:_alias] => 't'
            [CModel:_errors] => array()
            [CModel:_validators] => null
            [CModel:_scenario] => ''
            [CComponent:_e] => null
            [CComponent:_m] => null
        )
    )
    [CActiveRecord:_new] => true
    [CActiveRecord:_attributes] => array()
    [CActiveRecord:_related] => array
    (
        'attributes' => array
        (
            'name' => 'Test name'
            'description' => ''
            'contactId' => ''
            'fullDistance' => ''
        )
    )
    [CActiveRecord:_c] => null
    [CActiveRecord:_pk] => null
    [CActiveRecord:_alias] => 't'
    [CModel:_errors] => array()
    [CModel:_validators] => null
    [CModel:_scenario] => 'insert'
    [CComponent:_e] => null
    [CComponent:_m] => null
)

安全な属性:

2012/08/15 16:58:03 [info] [application] Safe attributes:
array
(
    '0' => 'name'
    '1' => 'locationId'
    '2' => 'fullDistance'
    '3' => 'description'
)
4

1 に答える 1

2

それは名前の衝突でした。このデータベースには「属性」テーブルがあり、そのテーブルを参照するリレーション$model->attributesがあります。Yiiで生成されたコードは、次の行で大規模な割り当てを行います。

$model->attributes = $_POST['Destination'];

リレーションとして再定義され$model->attributesているため、大規模な割り当ては正しく行われません。

于 2012-08-18T00:03:27.977 に答える