Ao tentar criar Master/Detalhe não tenho o retorno desejado. Retorna todos os itens e não os itens do pedido em $ID.
Meu Controller:
<?php
namespace app\controllers;
use app\models\Fatura;
use app\models\FaturaSearch;
use app\models\ItemPedido;
use Yii;
use app\models\Pedido;
use app\models\PedidoSearch;
use app\models\CheckoutForm;
use yii\web\Controller;
use yii\filters\AccessControl;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\ItemPedidoSearch;
/**
* PedidoController implements the CRUD actions for Pedido model.
*/
class PedidoController extends Controller
{
public function behaviors()
{
return [
'access'=>[
'class'=>AccessControl::className(),
'only'=>['index','view','create','update'],
'rules'=>[
[
'allow'=>true,
'roles'=>['@']
],
]
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
*
* Tela de CheckOut
*
*/
public function actionCheckout()
{
$model = new Pedido();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['item-pedido/create', 'pedidoid' => $model->id]);
} else {
return $this->render('checkout', [
'model' => $model,
]);
}
}
/**
*
* Lista detalhes
*
*/
public function actionListaitens()
{
$model = new ItemPedido();
//corrigir data antes de mostrar na View
list($y,$m,$d) = explode("-",$this->create_data);
$this->create_data = $d.'/'.$m.'/'.$y;
return $this->render('listaitens', [
'model' => $model,
]);
}
/**
* Imprimir pedido
* @param integer $id
* @return mixed
*/
public function actionImprimir($id)
{
$itensModel = new ItemPedidoSearch();
$faturasModel = new FaturaSearch();
$dataProviderItens = $itensModel->search(Yii::$app->request->queryParams);
$dataProviderFaturas = $faturasModel->search(Yii::$app->request->queryParams);
return $this->render('imprimir', [
// 'model' => $this->findModel($id),
// 'itensModel' => $itensModel,
// 'dataProviderItens' => $dataProviderItens,
// 'dataProviderFaturas' => $dataProviderFaturas,
]);
}
/**
* Lista todos os Pedidos models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new PedidoSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Mostra um Pedido pelo ID.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
$itensModel = new ItemPedidoSearch();
$faturasModel = new FaturaSearch();
$dataProvider2 = $itensModel->search(Yii::$app->request->queryParams);
$dataProvider3 = $faturasModel->search(Yii::$app->request->queryParams);
return $this->render('view', [
'model' => $this->findModel($id),
'itensModel' => $itensModel,
'dataProvider2' => $dataProvider2,
'dataProvider3' => $dataProvider3,
]);
}
/**
* Cria um novo Pedido.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Pedido();
//corrigir data antes de lancar no BD
list($d, $m, $y) = explode("/", $model->create_date);
$model->create_date = $y . '-' . $m . '-' . $d;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Pedido model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('checkout', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Pedido model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$citens = ItemPedido::find()->where(['pedido_id'=>$id])->count();
$cfaturas = Fatura::find()->where(['pedido_id'=>$id])->count();
if ($citens >0) {
throw new NotFoundHttpException('Existem Itens no pedido! Exclua antes de excluir o Pedido.');
} elseif ($cfaturas =0) {
throw new NotFoundHttpException('Existem Faturas no pedido! Exclua antes de excluir o Pedido.');
} else {
$this->findModel($id)->delete();
return $this->redirect(['index']);
};
}
/**
* Finds the Pedido model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Pedido the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Pedido::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}