php - Yii CRUD 更新 - 错误 400 - 复合键导致问题

标签 php yii

我在 YII php 框架中更新“bootstrap.widgets.TbGridView”时遇到问题。

由我的“bootstrap.widgets.TbGridView”的更新/铅笔图标生成的链接生成一个 错误 400。 链接如下所示

http://mysite/index.php?r=marketingchannel/update&id%5Bid%5D=119&id%5BControllingChannel_id%5D=7

我尝试过像这样手动输入链接

http://mysite/index.php?r=marketingchannel/update&id=119&ControllingChannel_id=7

然后我得到以下 CDbException

The value for the column "id" is not supplied when querying the table "marketingchannel".

堆栈跟踪(我手动输入的链接)如下所示:

0./framework/db/schema/CDbCommandBuilder.php(519): CDbCommandBuilder->createInCondition(CMysqlTableSchema, array("id", "ControllingChannel_id"), array("119") ,“t。”)
1./framework/db/ar/CActiveRecord.php(1431): CDbCommandBuilder->createPkCriteria(CMysqlTableSchema, "119", "", array(), ...)
2./protected/controllers/MarketingchannelController.php(158): CActiveRecord->findByPk("119")
3./protected/controllers/MarketingchannelController.php(89): MarketingchannelController->loadModel("119")
4. 未知(0): MarketingchannelController->actionUpdate("119")
5./framework/web/actions/CAction.php(108): ReflectionMethod->invokeArgs(MarketingchannelController, array("119"))
6./framework/web/actions/CInlineAction.php(47): CAction->runWithParamsInternal(MarketingchannelController, ReflectionMethod, array("r"=> "marketingchannel/update", "id"=> “119”,“ControllingChannel_id”=>“7”))
7./framework/web/CController.php(308): CInlineAction->runWithParams(array("r"=> "marketingchannel/update", "id"=> "119", "ControllingChannel_id "=> "7"))

这告诉我它正在查找 url 中的 id...但是无法处理它。

注意:我意识到 Gridview 正在生成一个带有 [ ] 括号 url 编码的链接。但是我不明白为什么即使我手动输入链接,它仍然不起作用。

注2:我的应用程序中有很多这样的Gridview,只有两个不起作用。 巧合的是,它们的模型通过外键相关。 然而,与这两个相关的其他 Gridview 确实可以正常工作。

更新: 我的 Controller 更新操作的代码

/**
* 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['Marketingchannel']))
{
$model->attributes=$_POST['Marketingchannel'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}

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

更新: 我的模型的代码

/**
 * This is the model class for table "marketingchannel".
 *
 * The followings are the available columns in table 'marketingchannel':
 * @property integer $id
 * @property string $MarketingChannel
 * @property integer $ControllingChannel_id
 *
 * The followings are the available model relations:
 * @property Audibeneid[] $audibenes
 * @property Audibeneid[] $audibenes1
 * @property Controllingchannel $controllingChannel
 * @property Verwendung[] $verwendungs
 */
class Marketingchannel extends CActiveRecord
{
     public $ControllingChannel_search;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Marketingchannel 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 'marketingchannel';
    }

    /**
     * @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('ControllingChannel_id', 'required'),
            array('ControllingChannel_id', 'numerical', 'integerOnly'=>true),
            array('MarketingChannel', 'length', 'max'=>100),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            //Original   --  array('id, MarketingChannel, ControllingChannel_id', 'safe', 'on'=>'search'),
            array('id, MarketingChannel, ControllingChannel_id, ControllingChannel_search', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @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(
            'audibenes' => array(self::HAS_MANY, 'Audibeneid', 'MarketingChannel_id'),
            'audibenes1' => array(self::HAS_MANY, 'Audibeneid', 'MarketingChannel_ControllingChannel_id'),
            'controllingChannel' => array(self::BELONGS_TO, 'Controllingchannel', 'ControllingChannel_id'),
            'verwendungs' => array(self::HAS_MANY, 'Verwendung', 'MarketingChannel_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'MarketingChannel' => 'Marketing Channel',
            'ControllingChannel_id' => 'Controlling Channel id',
            'ControllingChannel_search' => 'Controlling Channel',
        );
    }

    /**
     * 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->with = array( 'controllingChannel' );
        $criteria->compare('id',$this->id);
        $criteria->compare('MarketingChannel',$this->MarketingChannel,true);
        $criteria->compare('ControllingChannel_id',$this->ControllingChannel_id);
        $criteria->compare( 'controllingChannel.Controllingchannel', $this->ControllingChannel_search, true );
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
}

更新: 我的表的 SQL

CREATE TABLE IF NOT EXISTS `marketingchannel` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `MarketingChannel` varchar(100) DEFAULT NULL,
  `ControllingChannel_id` int(11) NOT NULL,
  PRIMARY KEY (`id`,`ControllingChannel_id`),
  UNIQUE KEY `MarketingChannel_UNIQUE` (`MarketingChannel`),
  KEY `fk_MarketingChannel_ControllingChannel1_idx` (`ControllingChannel_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=137 ;

GridView 代码。

<?php $this->widget('bootstrap.widgets.TbGridView',
array( 'id'=>'marketingchannel-grid', 'dataProvider'=>$model->search(), 'filter'=>$model, 'columns'=>
array( 'id', 'MarketingChannel', array( 'name'=>'ControllingChannel_search', 'value'=>'$data->controllingChannel->ControllingChannel' ), 'ControllingChannel_id', 
array( 'class'=>'bootstrap.widgets.TbButtonColumn', ), ), )); ?>

最佳答案

请将您的操作代码更改为此并再次运行。

public function actionUpdate($id)
{
$model=$this->loadModel($id);

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

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

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

原因可能是id丢失。 $model->id 可能未设置。

当您有复合主键时,您应该在数组中指定它,如下所示。

更新答案

public function actionUpdate()
{
$id = $_GET['id'];
$ControllingChannel_id = $_GET['ControllingChannel_id'];
$model = Marketingchannel::model()->findByPk(array(
'id' => $id,
'ControllingChannel_id' => $ControllingChannel_id
));

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

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

要解决 gridview 中不正确的 URL,您应该按如下方式修改代码并包含您的 url(单击铅笔图标时转到哪里)。即 updateButtonUrl =“更新操作的 URL”

$this->widget('bootstrap.widgets.TbGridView', array('id'=>'marketingchannel-grid', 'dataProvider'=>$model->search(), 'filter'=>$model,
   'columns'=>
   array('id', 'MarketingChannel', array('name'=>'ControllingChannel_search', 'value'=>'$data->controllingChannel->ControllingChannel')
      , 'ControllingChannel_id',
      array(
         'htmlOptions'=>array('nowrap'=>'nowrap'),
         'class'=>'bootstrap.widgets.TbButtonColumn',
 'updateButtonUrl'=>'Yii::app()->createUrl("Marketingchannel/Update/",array("id"=>$data->id, "ControllingChannel_id"=>$data->ControllingChannel_id))',
      ),
      array('class'=>'bootstrap.widgets.TbButtonColumn'))));

引用号:http://yiibooster.clevertech.biz/widgets/grids/view/gridview.html

关于php - Yii CRUD 更新 - 错误 400 - 复合键导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20608782/

相关文章:

php - 根据条件数排序

php - 调用数组中的特定列

php - Magento:无法登录到管理员

php - Yii2 Url::remember() 是如何工作的?

php - 使用 PHP 通知 undefined variable

php - InnoDB 安全或不安全事务

php - Facebook fbsr 和 session 不会删除

php - 如何遍历 Magento 购物车中的所有项目?

mysql - Yii中使用触发器时如何使用事务

php - Yii 如何检查查询执行是否失败