Problème Entre Modèle Et Controller Avec Csqldataprovider

Bonjour,

Je dois afficher une liste de résultats issue d’une requete sql complexe (environ 80 lignes et plusieurs tables sans clés étrangères)

Pour cela j’ai construit mon MVC dans un nouveau module en utilisant CSqlDataProvider.

Je n’arrive pas à le faire fonctionner.

Ci-dessous le détail(j’ai simplifié la réquete sql) :

controllers/DefaultController.php


<?php

class DefaultController extends Controller

{

	public function actionIndex()

	{

		$dataProvider=new CArrayDataProvider('ListDemandes');

		$this->render('index',array(

			'dataProvider'=>ListDemandes::model()->getListAllDemandes(),

		));

	}

}

?>

models/ListDemandes.php


<?php

class ListDemandes extends CActiveRecord{

	 

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}

	

	public function getListAllDemandes() {

		$count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM demande_ou_projet dem')->queryScalar();

		$sql='

				SELECT iddemande,Libelle

				FROM demande_ou_projet dem

				';

		$dataProvider=new CSqlDataProvider($sql, array(

				'totalItemCount'=>$count,

				'sort'=>array(

						'attributes'=>array(

								'iddemande', 'libelle',

						),

				),

				'pagination'=>array(

						'pageSize'=>20,

				),

		));

		return $dataProvider->getData();


	}

}

?>

views/index.php


<?php

/* @var $this DefaultController */




$this->widget('zii.widgets.grid.CGridView', array(

        'dataProvider'=>$dataProvider,

		'columns'=>array(

				array(

						'header'=>'ID',

						'name' => 'id',

						'type'=>'raw',

						'value' => '$data["iddemande"]',

				),

				array(

						'header'=>'libelle',

						'name' => 'id',

						'type'=>'raw',

						'value' => '$data["libelle"]',

				),

				

		),

));

?>

Je vous remercie d’avance pour votre aide car je tourne un peu en rond.

[list=1]

[*]Contrôleur


    public function actionIndex()

    {

        $model = new ListDemandes('getListAllDemandes');

        $model->unsetAttributes();


        if (isset($_GET['ListDemandes'])) {

            $model->attributes = $_GET['ListDemandes'];

        }


        $this->render('index', array(

            'model' => $model,

        ));

    }



[*]Modèle


    public function getListAllDemandes()

    {

        …


        return new CArrayDataProvider(Yii::app()->db->createCommand($sql)->queryAll(), array(

            'keyField' => 'iddemande',

            'pagination' => array('pageSize' => 20),

        ));

    }



[*]Vue

A priori, ce que tu as mis est bon

[/list]

Merci bennouna, je teste demain et je te tiens au courant

Bonjour Bennouna,

En appliquant, tes recommandations je rencontre le problème suivant :


CDbException


The table "ListDemandes" for active record class "ListDemandes" cannot be found in the database.

Et dans le stack trace :


C:\xampp\htdocs\iGama\protected\modules\fub\controllers\DefaultController.php(6): CActiveRecord->__construct("getListAllDemandes")

01 <?php

02 class DefaultController extends Controller

03 {

04     public function actionIndex()

05     {

06         $model = new ListDemandes('getListAllDemandes');

07         $model->unsetAttributes();

08 

09         if (isset($_GET['ListDemandes'])) {

10             $model->attributes = $_GET['ListDemandes'];

11         }

As tu une idée pour corriger ce problème?

Bonjour Ludwig,

Est-ce que tu as réellement une table [font="Courier New"]ListDemandes[/font] dans ta BDD ? Car la classe [font="Courier New"]CActiveRecord[/font] définit une méthode standard [font="Courier New"]tableName[/font] qui cherche une table associée au modèle, qui par défaut a le même nom que ta classe.

Si tu disposes donc d’une table associée, mais qui a un nom différent, tu peux surcharger la méthode comme suit :


public function tableName()

{

    return '{{nom_reel_de_la_table_associee}}';

}

ou encore, si la différence du nom de ta table se limité à un préfixe configuré par ailleurs dans le fichier config :


public function tableName()

{

    return '{{ListDemandes}}';

}

S’il n’y a absolument aucune table spécifiquement associée à ton modèle, il ne faut pas utiliser CActiveRecord mais plutôt CModel ou CFormModel

Pour plus de précisions : http://www.yiiframework.com/doc/guide/1.1/en/form.model