Précédent   Forum des professionnels en informatique > PHP > Outils > Zend > Zend Framework
Zend Framework Forum d'entraide sur la programmation PHP avec Zend Framework. Avant de poster -> FAQ ZF, Cours ZF
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 27/11/2010, 10h49   #1
Membre du Club
 
Inscription : mars 2007
Messages : 110
Détails du profil
Informations forums :
Inscription : mars 2007
Messages : 110
Points : 50
Points : 50
Par défaut Fonction du model s'exécutant deux fois et routeur

Bonjour,

Une de mes fonctions s'exécute à deux reprises (données en doublon dans la db). Après avoir chercher le problème en remontant à la source plusieurs fois, je ne trouve toujours pas d'endroit ou j'aurais fait un "double appel" à cette fonction. Étant débutant avec zend, il pourrait s'agir d'un problème relier à une mauvaise utilisation du router ou de la structure MVC.

Mon model Api a une fonction qui effectue plusieurs enregistrement dans la base de donnée.
Code :
1
2
3
public function downloadQuestionPack($userId,$questionPackId) {
        return $this->getMapper()->downloadQuestionPack($userId,$questionPackId);
    }
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public function downloadQuestionPack($userId,$questionPackId) {
 
        $questionPackModel = new Application_Model_QuestionPack();
        $questionPack = $questionPackModel->find($questionPackId);
        $questionsId = $questionPack->getAllQuestionsId();
 
        // create a userExercice for the question Pack
        $userExercice = new Application_Model_UserExercice();
        $userExercice->setUserId($userId)
                ->setCurrentQuestionId($questionsId[0])
                ->setTotalNumberOfQuestions(count($questionsId));
        $userExerciceId = $userExercice->save();
        $userExercice->setId($userExerciceId);
 
        // download each of the questions in the questionToUser table
        // every time we save a question, we need to send the nextQuestionId
        for ($index = 0;
        $index < count($questionsId);
        $index++) {
            //it the last question of the userExercice
            if ($index == count($questionsId)-1) {
                $nextQuestionId = 0;
            }
            else {
                $nextQuestionId = $questionsId[$index+1];
            }
            $this->downloadQuestion($userId, $questionsId[$index], $userExercice, $nextQuestionId);
        }
 
    }
Elle est appelée au niveau du contrôleur:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    public function downloadQuestionPackAction() {
 
        // set the user id and redirect if user not authentificated
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $userId = $auth->getIdentity()->id;
        }
        else {
            $this->_redirect('user/auth/login');
        }
 
        // set questionPackId from url param
        $questionPackId = $this->_getParam('questionPackId');
 
        $apiModel = new Application_Model_Api();
        $apiModel->downloadQuestionPack($userId, $questionPackId);
    }
Et voici le router qui appelle le contrôleur:
Code :
1
2
3
4
5
6
7
8
9
$route = new Zend_Controller_Router_Route(
                'exercice/question-packs/download-question-pack/:questionPackId/',
                array(
                    'module' => 'exercice',
                    'controller' => 'question-packs',
                    'action' => 'download-question-pack'
                )
        );
        $router->addRoute('downloadQuestionPack',$route);
et enfin le lien à partir du view:
Code :
<a href="<?php echo $this->url(array('questionPackId'=>$questionPack->getId()),'downloadQuestionPack') ?>">Add the pack to your study plan</a>
Je recherche peut-être l'erreur au mauvais endroit donc la moindre piste m'aidera beaucoup.
hackiles est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/11/2010, 19h20   #2
Membre Expert
 
Avatar de nosferapti
 
Inscription : avril 2009
Messages : 1 157
Détails du profil
Informations forums :
Inscription : avril 2009
Messages : 1 157
Points : 1 129
Points : 1 129
dans le code, affiche l'identifiant de la ligne créée dans la base de données pour voir s'il s'agit du 1er ou du 2e appel
est ce que tu as ce problème en testant en local ? si oui essaye Xdebug pour mieux suivre ce qu'il se passe :
http://jcrozier.developpez.com/tutor...veloppeurs/#LV
__________________
GNAP !
nosferapti est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/11/2010, 21h57   #3
Membre du Club
 
Inscription : mars 2007
Messages : 110
Détails du profil
Informations forums :
Inscription : mars 2007
Messages : 110
Points : 50
Points : 50
Merci pour xdebug, je ne connaissais pas et çà à l'air très utile ! (mon problème est bien en local)

Je l'ai installé et çà fonctionne.
Voilà ce que j'ai placé à la fin de la fonction dans ApiMapper:
Code :
1
2
var_dump(xdebug_get_function_stack());
xdebug_print_function_stack( 'Your own message' );
Je ne sais pas si j'ai utilisé les bonnes fonctions mais avec celles-ci je n'arrive toujours pas à comprendre le problème (les fonctions sont appelés 1 seule fois dans le stack)
Voilà ce que j'obtiens:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
rray
  0 => 
    array
      'function' => string '{main}' (length=6)
      'file' => string '/Applications/MAMP/htdocs/education/public/index.php' (length=52)
      'line' => int 0
      'params' => 
        array
          empty
  1 => 
    array
      'function' => string 'run' (length=3)
      'class' => string 'Zend_Application' (length=16)
      'file' => string '/Applications/MAMP/htdocs/education/public/index.php' (length=52)
      'line' => int 26
      'params' => 
        array
          empty
  2 => 
    array
      'function' => string 'run' (length=3)
      'class' => string 'Zend_Application_Bootstrap_Bootstrap' (length=36)
      'file' => string '/Applications/MAMP/htdocs/education/library/Zend/Application.php' (length=64)
      'line' => int 366
      'params' => 
        array
          empty
  3 => 
    array
      'function' => string 'dispatch' (length=8)
      'class' => string 'Zend_Controller_Front' (length=21)
      'file' => string '/Applications/MAMP/htdocs/education/library/Zend/Application/Bootstrap/Bootstrap.php' (length=84)
      'line' => int 97
      'params' => 
        array
          empty
  4 => 
    array
      'function' => string 'dispatch' (length=8)
      'class' => string 'Zend_Controller_Dispatcher_Standard' (length=35)
      'file' => string '/Applications/MAMP/htdocs/education/library/Zend/Controller/Front.php' (length=69)
      'line' => int 954
      'params' => 
        array
          empty
  5 => 
    array
      'function' => string 'dispatch' (length=8)
      'class' => string 'Zend_Controller_Action' (length=22)
      'file' => string '/Applications/MAMP/htdocs/education/library/Zend/Controller/Dispatcher/Standard.php' (length=83)
      'line' => int 295
      'params' => 
        array
          empty
  6 => 
    array
      'function' => string 'downloadQuestionPackAction' (length=26)
      'class' => string 'Exercice_QuestionPacksController' (length=32)
      'file' => string '/Applications/MAMP/htdocs/education/library/Zend/Controller/Action.php' (length=70)
      'line' => int 513
      'params' => 
        array
          empty
  7 => 
    array
      'function' => string 'downloadQuestionPack' (length=20)
      'class' => string 'Application_Model_Api' (length=21)
      'file' => string '/Applications/MAMP/htdocs/education/application/modules/exercice/controllers/QuestionPacksController.php' (length=104)
      'line' => int 36
      'params' => 
        array
          empty
  8 => 
    array
      'function' => string 'downloadQuestionPack' (length=20)
      'class' => string 'Application_Model_ApiMapper' (length=27)
      'file' => string '/Applications/MAMP/htdocs/education/application/models/Api.php' (length=62)
      'line' => int 16
      'params' => 
        array
          empty
 
( ! ) Xdebug: Your own message in /Applications/MAMP/htdocs/education/application/models/ApiMapper.php on line 93
Call Stack
#	Time	Memory	Function	Location
1	0.0042	324196	{main}( )	../index.php:0
2	0.0417	1265484	Zend_Application->run( )	../index.php:26
3	0.0417	1265484	Zend_Application_Bootstrap_Bootstrap->run( )	../Application.php:366
4	0.0417	1265540	Zend_Controller_Front->dispatch( )	../Bootstrap.php:97
5	0.0441	1325944	Zend_Controller_Dispatcher_Standard->dispatch( )	../Front.php:954
6	0.0482	1436128	Zend_Controller_Action->dispatch( )	../Standard.php:295
7	0.0482	1439376	Exercice_QuestionPacksController->downloadQuestionPackAction( )	../Action.php:513
8	0.0816	1467016	Application_Model_Api->downloadQuestionPack( )	../QuestionPacksController.php:36
9	0.0869	1498764	Application_Model_ApiMapper->downloadQuestionPack( )	../Api.php:16
10	0.1285	1715196	xdebug_print_function_stack ( )	../ApiMapper.php:93
Citation:
affiche l'identifiant de la ligne créée dans la base de données pour voir s'il s'agit du 1er ou du 2e appel
C'est l'id du premier enregistrement qui s'affiche dans la vue. Mais je suis encore trop bleu avec zend pour comprendre ce qui ce passe apres l'affichage...

Sais-tu si il y a d'autres fonctions de xdebug qui pourraient m'aider à mieux comprendre ce qui passe.
hackiles est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/11/2010, 07h25   #4
Rédacteur/Modérateur
 
Avatar de MaitrePylos
 
Homme Gérard Ernaelsten
DBA & Dev PHP
Inscription : juin 2005
Messages : 3 167
Détails du profil
Informations personnelles :
Nom : Homme Gérard Ernaelsten
Âge : 39
Localisation : Belgique

Informations professionnelles :
Activité : DBA & Dev PHP
Secteur : Service public

Informations forums :
Inscription : juin 2005
Messages : 3 167
Points : 6 451
Points : 6 451
Donc si je comprend bien ceci, ne devrais boucler qu'une fois ?

Code :
1
2
3
4
 
 for ($index = 0;
        $index < count($questionsId);
        $index++) {
Si la réponse est positive, vérifie ce que retourne
Code :
1
2
 
  $questionsId = $questionPack->getAllQuestionsId();
MaitrePylos est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/11/2010, 17h42   #5
Membre du Club
 
Inscription : mars 2007
Messages : 110
Détails du profil
Informations forums :
Inscription : mars 2007
Messages : 110
Points : 50
Points : 50
D'après les enregistrement en db, c'est toute la fonction downloadQuestionPack qui s'exécute 2 fois.
Code php :
$userExercice->save();
n'est pas dans la boucle et me cré deux enregistrement.

La boucle semble bien fonctionner:
Code php :
count($questionsId)
me retourne '2' quand je souhaite effectuer les opérations sur 2 objets différents.
Citation:
( questionId[0] et questionId[2] )
hackiles est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/12/2010, 15h18   #6
Membre Expert
 
Avatar de nosferapti
 
Inscription : avril 2009
Messages : 1 157
Détails du profil
Informations forums :
Inscription : avril 2009
Messages : 1 157
Points : 1 129
Points : 1 129
Citation:
Envoyé par hackiles Voir le message
D'après les enregistrement en db, c'est toute la fonction downloadQuestionPack qui s'exécute 2 fois.
et cette fonction est appelé par une action ?

pour Xdebug tu peux mettre ça dans le fichier php.ini pour suivre tous les appel de code PHP
tu auras alors un fichier par appel dans le répertoire "/tmp/php"
Code x :
1
2
3
4
5
6
7
8
9
[xdebug]

xdebug.collect_params = 4
xdebug.collect_return = 1
xdebug.show_mem_delta = 1
xdebug.trace_output_dir = "/tmp/php"
xdebug.trace_output_name = "apache-trace.%u.%R"

xdebug.auto_trace = 1
__________________
GNAP !
nosferapti est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 13/12/2010, 11h31   #7
Membre du Club
 
Inscription : mars 2007
Messages : 110
Détails du profil
Informations forums :
Inscription : mars 2007
Messages : 110
Points : 50
Points : 50
Merci beaucoup! En effet, la fonction est appelé par une action.

Xdebug m'a permis de comprendre un peu mieux le problème (qui est plus global que je ne le pensais):
2 fichiers de 2.4 Mo sont créés à chaque appel d'une action (peu importe l'action).

A moins que çà soit normal, je dois avoir un problème avec de configuration.
Il y a t-il un moyen simple pour savoir si il faut regarder côté apache (j'utilise MAMP sur Mac Os 10.5), config de zend ou routeur.
hackiles est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/12/2010, 23h05   #8
Membre du Club
 
Inscription : mars 2007
Messages : 110
Détails du profil
Informations forums :
Inscription : mars 2007
Messages : 110
Points : 50
Points : 50
la réponse ici:
http://www.sitepoint.com/forums/showthread.php?t=362404
hackiles est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 21h57.


 
 
 
 
Partenaires

Hébergement Web