0

Ok I have working code using ajax submit to capture and insert records in my db. Currently though I have to go from one models view to another to have this execute. I am trying to combine all the code and execute from a view in the first model though.

Below is my code attempting that. It is displayed as the logic flows.

In this code below I am using a tab and render partial in ModelA and then eventually call the ajaxSubmitButton from ModelB and use ModelB controller to input data into my db.

The problem that I am running into now is it always inserts a record, but instead of inputting the right record it will insert the first record that is displayed in the render partial (ItemId = 1 regardless of which Item I execute the code from).

<?php
//Model A View 
$tabs = array(
array('id' => 'tab1', 'label' => 'tab', 'content' => $this->renderPartial('/modelB/_modelBView', array('model' => $model), true)),
);

$this->widget('bootstrap.widgets.TbTabs', array(
'type' => 'tabs',
'tabs' => $tabs,
));


//_modelBView
if (!Yii::app()->user->isGuest)
$this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
    'template' => '{items}}',
    )); 


//_view
echo $data->checkItem;


//ModelB
public function getCheckItem ()
{
    if (something...)
        {
            echo 'Status Good'; 
        }
    else {
        echo CHtml::ajaxSubmitButton(
                'Get Item',          
                array('/modelB/getItem'), 
                array(
                    'type'=>'POST',
                    'data' => array('ItemId' => $this->ItemId, 'Type' => $this->Type),
                    'success' => "function(){window.location='".Yii::app()->createUrl('/modelC/list') . "'}",
                )
            ); 
    }
}


//ModelB Controller
public function actionGetItem() 
{
    $connection = yii::app()->db;
    $transaction=$connection->beginTransaction();
    try 
    {   
        $connection = yii::app()->db;
        $sql = "INSERT INTO modelC (ItemId, Type)
                        VALUES(:item, :type)";
        $command=$connection->createCommand($sql);
        $command->bindValue(":item", $_POST['ItemId']);
        $command->bindValue(":type", $_POST['Type']);
        $command->execute();

        $transaction->commit();
    }
    catch(Exception $e)
    {
        $transaction->rollBack();
    }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
enfield
  • 841
  • 3
  • 16
  • 42
  • I've not found what wrong with your end yet, but was there the typo when you called data->checkItem but in the model B you define the function name getCheckItem()? I have also tried to make the same your code on myself and everything worked fine. The possible thing which I guess that maybe there has something was wrong with attribute ItemId when you define it on model B, that I could not see here – Telvin Nguyen Jul 26 '13 at 14:19
  • @Telvin Nguyen, how I call checkItem works just fine outside of the tab/renderPartial, as does everything else. For some reason it just does not work through the tab/renderPartial on a different models view. – enfield Jul 27 '13 at 16:02

0 Answers0