Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
Bonjour,
j'ai une erreur sur une méthode de mon modèle, plus précisément lorsque j'utilise le mot clé "current()".
Voici la méthode qui pose problème. (ProfilTable.php) :
[lang=php] public function exist($id) { $select = new \Zend\Db\Sql\Select; $resultSet = $this->tableGateway->select(function($select) use($id){ $select->columns(array('num' => new \Zend\Db\Sql\Expression('COUNT(*)'), 'profil_id', 'profil_first_name', 'profil_last_name', 'profil_cp', 'profil_city', 'profil_adr', 'profil_tel', 'profil_compl', 'profil_zone', 'profil_user_id')) ->where('profil_user_id='.$id); }); return $resultSet->current(); }
Voici mon modèle Profil.php
[lang=php] <?php namespace Application\Model; use Zend\Db\Adapter\AdapterInterface; class Profil { private $profil_id; private $profil_first_name; private $profil_last_name; private $profil_cp; private $profil_city; private $profil_adr; private $profil_tel; private $profil_compl; private $profil_zone; private $profil_ip; private $profil_user_id; public $num; public function getId() { return $this->profil_id; } public function setId($id) { $this->profil_id = $id; } public function getFirstName() { return $this->profil_first_name; } public function setFirstName($firstName) { $this->profil_first_name = $firstName; } public function getLastName() { return $this->profil_last_name; } public function setLastName($lastName) { $this->profil_last_name = $lastName; } public function getCp() { return $this->profil_cp; } public function setCp($cp) { $this->profil_cp = $cp; } public function getCity() { return $this->profil_city; } public function setCity($city) { $this->profil_city = $city; } public function getAdr() { return $this->profil_adr; } public function setAdr($adr) { $this->profil_adr = $adr; } public function getTel() { return $this->profil_tel; } public function setTel($tel) { $this->profil_tel = $tel; } public function getCompl() { return $this->profil_compl; } public function setCompl($compl) { $this->profil_compl = $compl; } public function getZone() { return $this->profil_zone; } public function setZone($zone) { $this->profil_zone = $zone; } public function getIp() { return $this->profil_ip; } public function setIp($ip) { $this->profil_ip = $ip; } public function getUserId() { return $this->profil_user_id; } public function setUserId($userId) { $this->profil_user_id = $userId; } }
Voici l'erreur que j'obtiens lorsque j'appel cette méthode dans mon contrôlleur ($infos = $this->_profilTable->exist($user_id) :
Fichier: C:\wamp\www\zf2test\vendor\zendframework\zendframework\library\Zend\Stdlib\Hydrator\ArraySerializable.php:78 Message: Zend\Stdlib\Hydrator\ArraySerializable::hydrate expects the provided object to implement exchangeArray() or populate()
Voici mon fichier Module.php :
[lang=php] public function getServiceConfig() { return array( 'factories' => array( 'Application\Model\ProfilTable' => function($sm) { $tableGateway = $sm->get('ProfilTableGateway'); $table = new ProfilTable($tableGateway); return $table; }, 'ProfilTableGateway' => function ($sm) { $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter'); $resultSetPrototype = new HydratingResultSet( new ArraySerializableHydrator(), new Profil() ); return new TableGateway('mel_profil', $dbAdapter, null, $resultSetPrototype); }, ) ); }
Merci de votre aide
Dernière modification par romsVLM (12-11-2014 18:36:06)
Hors ligne
Bonjour,
Comme le dit l'erreur, pour utiliser ArraySerializableHydrator il faut que ton objet ait soit une methode exchangeArray soit populate, de sorte a definir le comportement d'hydratation (que mettre ou).
Tu peux aussi choisir un autre hydrateur, dans ton cas pourquoi pas un Zend\Stdlib\Hydrator\ClassMethods :
Any data key matching a setter method will be called in order to hydrate; any method matching a getter method will be called for extraction.
Hors ligne
Merci effectivement mes méthodes fonctionnent avec le exchangeArray().
Mais j'ai d'autres soucis maintenant avec l'un de mes formulaires.
Celui -ci utilise un Fieldset, et peux soit afficher un formulaire avec les champs vides pour une insertion, soit un formulaire avec les champs pré-remplis avec les données récupérés en BDD dans le cas d'une modification.
Problème : lors d'une modification les champs du formulaire ne sont pas pré-remplies (comme si le bind n'avait pas lieu)
Je vais vous poster mes sources, dites moi s'il vous plait si s'est la bonne façon de coder ceci, ou sont mes erreurs.
Modèle "Profil.php" :
[lang=php] <?php namespace Application\Model; use Zend\Db\Adapter\AdapterInterface; use Zend\InputFilter\InputFilterAwareInterface; use Zend\InputFilter\InputFilter; class Profil implements InputFilterAwareInterface { public $profil_id; public $profil_first_name; public $profil_last_name; public $profil_cp; public $profil_city; public $profil_adr; public $profil_tel; public $profil_compl; public $profil_zone; public $profil_ip; public $profil_user_id; public $num; protected $inputFilter; public function exchangeArray($data) { $this->profil_id = (isset($data['profil_id'])) ? $data['profil_id'] : null; $this->profil_first_name = (isset($data['profil_first_name'])) ? $data['profil_first_name'] : null; $this->profil_last_name = (isset($data['profil_last_name'])) ? $data['profil_last_name'] : null; $this->profil_cp = (isset($data['profil_cp'])) ? $data['profil_cp'] : null; $this->profil_city = (isset($data['profil_city'])) ? $data['profil_city'] : null; $this->profil_adr = (isset($data['profil_adr'])) ? $data['profil_adr'] : null; $this->profil_tel = (isset($data['profil_tel'])) ? $data['profil_tel'] : null; $this->profil_compl = (isset($data['profil_compl'])) ? $data['profil_compl'] : null; $this->profil_zone = (isset($data['profil_zone'])) ? $data['profil_zone'] : null; $this->profil_ip = (isset($data['profil_ip'])) ? $data['profil_ip'] : null; $this->profil_user_id = (isset($data['profil_user_id'])) ? $data['profil_user_id'] : null; $this->num = (isset($data['num'])) ? $data['num'] : null; } public function getArrayCopy() { return get_object_vars($this); } public function getId() { return $this->profil_id; } public function setId($id) { $this->profil_id = $id; } public function getFirstName() { return $this->profil_first_name; } public function setFirstName($firstName) { $this->profil_first_name = $firstName; } public function getLastName() { return $this->profil_last_name; } public function setLastName($lastName) { $this->profil_last_name = $lastName; } public function getCp() { return $this->profil_cp; } public function setCp($cp) { $this->profil_cp = $cp; } public function getCity() { return $this->profil_city; } public function setCity($city) { $this->profil_city = $city; } public function getAdr() { return $this->profil_adr; } public function setAdr($adr) { $this->profil_adr = $adr; } public function getTel() { return $this->profil_tel; } public function setTel($tel) { $this->profil_tel = $tel; } public function getCompl() { return $this->profil_compl; } public function setCompl($compl) { $this->profil_compl = $compl; } public function getZone() { return $this->profil_zone; } public function setZone($zone) { $this->profil_zone = $zone; } public function getIp() { return $this->profil_ip; } public function setIp($ip) { $this->profil_ip = $ip; } public function getUserId() { return $this->profil_user_id; } public function setUserId($userId) { $this->profil_user_id = $userId; } public function setInputFilter(\Zend\InputFilter\InputFilterInterface $inputFilter) { throw new \Exception("Not used"); } public function getInputFilter() { if (!$this->inputFilter) { $inputFilter = new InputFilter(); $inputFilter->add( array( 'name' => 'profil_first_name', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 2, 'max' => 80, ), ), array( 'name' => 'Regex', 'options' => array( 'pattern' => '/^[\p{L}-\s]+$/u', 'message' => 'Seulement le \'-\' et les caracteres alphabetique sont autorises', ), ), ), ) ); $inputFilter->add( array( 'name' => 'profil_last_name', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 2, 'max' => 80, ), ), array( 'name' => 'Regex', 'options' => array( 'pattern' => '/^[\p{L}-\s]+$/u', 'message' => 'Seulement le \'-\' et les caracteres alphabetique sont autorises', ), ), ), ) ); $inputFilter->add( array( 'name' => 'profil_cp', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 5, 'max' => 5, ), ), array( 'name' => 'Regex', 'options' => array( 'pattern' => '/^[0-9]+$/i', 'message' => 'Seulement les caracteres numeriques sont autorises', ), ), ), ) ); $inputFilter->add( array( 'name' => 'profil_city', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 2, 'max' => 80, ), ), array( 'name' => 'Regex', 'options' => array( 'pattern' => '/^[\p{L}-\s]+$/u', 'message' => 'Seulement le \'-\' et les caracteres alphabetique sont autorises', ), ), ), ) ); $inputFilter->add( array( 'name' => 'profil_adr', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 10, 'max' => 400, ), ), ), ) ); $inputFilter->add( array( 'name' => 'profil_tel', 'required' => true, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 10, 'max' => 10, ), ), array( 'name' => 'Regex', 'options' => array( 'pattern' => '/^[0-9]+$/i', 'message' => 'Seulement les caracteres numeriques sont autorises', ), ), ), ) ); $inputFilter->add( array( 'name' => 'profil_compl', 'required' => false, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 1, 'max' => 500, ), ), ) ) ); $inputFilter->add( array( 'name' => 'profil_zone', 'required' => false, 'filters' => array( array('name' => 'StripTags'), array('name' => 'StringTrim'), ), 'validators' => array( array( 'name' => 'StringLength', 'options' => array( 'encoding' => 'UTF-8', 'min' => 3, 'max' => 6, ), ), ) ) ); $this->inputFilter = $inputFilter; } return $this->inputFilter; } }
Modèle "ProfilTable.php" :
[lang=php] public function saveProfil(Profil $profil, $user_id, $options) { if($options == 'Sur-place') { $profil->setZone(NULL); } $id = (int)$profil->profil_id; if ($id == 0) { $data = array( 'profil_id' => $profil->profil_id, 'profil_first_name' => $profil->profil_first_name, 'profil_last_name' => $profil->profil_last_name, 'profil_cp' => $profil->profil_cp, 'profil_city' => $profil->profil_city, 'profil_adr' => $profil->profil_adr, 'profil_tel' => $profil->profil_tel, 'profil_compl' => $profil->profil_compl, 'profil_zone' => $profil->profil_zone, 'profil_ip' => $this->get_ip(), 'profil_user_id' => $user_id ); var_dump($data); exit(); $this->tableGateway->insert($data); } else { if ($this->getProfil($id)) { $data2 = array( 'profil_first_name' => $profil->profil_first_name, 'profil_last_name' => $profil->profil_last_name, 'profil_cp' => $profil->profil_cp, 'profil_city' => $profil->profil_city, 'profil_adr' => $profil->profil_adr, 'profil_tel' => $profil->profil_tel, 'profil_compl' => $profil->profil_compl, 'profil_zone' => $profil->profil_zone, 'profil_ip' => $this->get_ip(), ); var_dump($data2); exit(); $this->tableGateway->update($data2, array('profil_id' => $id)); } else { throw new \Exception('Form id does not exist'); } } }
ProfilFieldset.php
[lang=php] <?php namespace Application\Form; use Application\Model\Profil; use Zend\Form\Fieldset; use Zend\InputFilter\InputFilterProviderInterface; use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator; class ProfilFieldset extends Fieldset implements InputFilterProviderInterface { public function __construct() { parent::__construct ( 'profil_fieldset' ); $this->setHydrator ( new ClassMethodsHydrator ( false ) )->setObject ( new Profil () ); $this->add ( array ( 'name' => 'profil_id', 'type' => 'hidden', 'attributes' => array ( 'class' => 'form-control', 'id' => 'inputId3' ) ) ); $this->add ( array ( 'name' => 'profil_first_name', 'options' => array ( 'label' => 'Prenom' ), 'attributes' => array ( 'class' => 'form-control', 'id' => 'inputPrenom3', 'type' => 'text', 'required' => true ) ) ); $this->add ( array ( 'name' => 'profil_last_name', 'options' => array ( 'label' => 'Nom' ), 'attributes' => array ( 'class' => 'form-control', 'id' => 'inputNom3', 'type' => 'text', 'required' => true ) ) ); $this->add ( array ( 'name' => 'profil_cp', 'options' => array ( 'label' => 'Code postal' ), 'attributes' => array ( 'class' => 'form-control', 'id' => 'inputCP3', 'type' => 'text', 'required' => true ) ) ); $this->add ( array ( 'name' => 'profil_city', 'options' => array ( 'label' => 'Ville' ), 'attributes' => array ( 'class' => 'form-control', 'id' => 'inputVille3', 'type' => 'text', 'required' => true ) ) ); $this->add ( array ( 'type' => 'Zend\Form\Element\Textarea', 'name' => 'profil_adr', 'options' => array ( 'label' => 'Adresse' ), 'attributes' => array ( 'class' => 'form-control', 'required' => true, 'id' => 'inputAdr3' ) ) ); $this->add ( array ( 'name' => 'profil_tel', 'options' => array ( 'label' => 'Telephone' ), 'attributes' => array ( 'class' => 'form-control', 'id' => 'inputTel3', 'type' => 'text', 'required' => true ) ) ); $this->add ( array ( 'type' => 'Zend\Form\Element\Textarea', 'name' => 'profil_compl', 'options' => array ( 'label' => 'Informations Complementaires' ), 'attributes' => array ( 'class' => 'form-control', 'id' => 'inputComp3' ) ) ); $this->add ( array ( 'name' => 'profil_zone', 'type' => 'Select', 'attributes' => array ( 'id' => 'inputZone3', 'class' => 'form-control', 'required' => false ), 'options' => array ( 'label' => 'Zone de livraison', 'value_options' => array ( 'Zone 1' => 'Zone 1', 'Zone 2' => 'Zone 2', 'Zone 3' => 'Zone 3', 'Zone 4' => 'Zone 4', 'Ext' => 'Exterieur aux zones' ), 'empty_option' => '--- Sélectionnez une zone ---' ) ) ); } /** * * @return array \ */ public function getInputFilterSpecification() { return array ( 'profil_first_name' => array ( 'required' => true, 'filters' => array ( array ( 'name' => 'StripTags' ), array ( 'name' => 'StringTrim' ) ), 'validators' => array ( array ( 'name' => 'StringLength', 'options' => array ( 'encoding' => 'UTF-8', 'min' => 2, 'max' => 80 ) ), array ( 'name' => 'Regex', 'options' => array ( 'pattern' => '/^[\p{L}-\s]+$/u', 'message' => 'Seulement le \'-\' et les caracteres alphabetique sont autorises' ) ) ) ), 'profil_last_name' => array ( 'required' => true, 'filters' => array ( array ( 'name' => 'StripTags' ), array ( 'name' => 'StringTrim' ) ), 'validators' => array ( array ( 'name' => 'StringLength', 'options' => array ( 'encoding' => 'UTF-8', 'min' => 2, 'max' => 80 ) ), array ( 'name' => 'Regex', 'options' => array ( 'pattern' => '/^[\p{L}-\s]+$/u', 'message' => 'Seulement le \'-\' et les caracteres alphabetique sont autorises' ) ) ) ), 'profil_cp' => array ( 'required' => true, 'filters' => array ( array ( 'name' => 'StripTags' ), array ( 'name' => 'StringTrim' ) ), 'validators' => array ( array ( 'name' => 'StringLength', 'options' => array ( 'encoding' => 'UTF-8', 'min' => 5, 'max' => 5 ) ), array ( 'name' => 'Regex', 'options' => array ( 'pattern' => '/^[0-9]+$/i', 'message' => 'Seulement les caracteres numeriques sont autorises' ) ) ) ), 'profil_city' => array ( 'required' => true, 'filters' => array ( array ( 'name' => 'StripTags' ), array ( 'name' => 'StringTrim' ) ), 'validators' => array ( array ( 'name' => 'StringLength', 'options' => array ( 'encoding' => 'UTF-8', 'min' => 2, 'max' => 80 ) ), array ( 'name' => 'Regex', 'options' => array ( 'pattern' => '/^[\p{L}-\s]+$/u', 'message' => 'Seulement le \'-\' et les caracteres alphabetique sont autorises' ) ) ) ), 'profil_adr' => array ( 'required' => true, 'filters' => array ( array ( 'name' => 'StripTags' ), array ( 'name' => 'StringTrim' ) ), 'validators' => array ( array ( 'name' => 'StringLength', 'options' => array ( 'encoding' => 'UTF-8', 'min' => 10, 'max' => 400 ) ) ) ), 'profil_tel' => array ( 'required' => true, 'filters' => array ( array ( 'name' => 'StripTags' ), array ( 'name' => 'StringTrim' ) ), 'validators' => array ( array ( 'name' => 'StringLength', 'options' => array ( 'encoding' => 'UTF-8', 'min' => 10, 'max' => 10 ) ), array ( 'name' => 'Regex', 'options' => array ( 'pattern' => '/^[0-9]+$/i', 'message' => 'Seulement les caracteres numeriques sont autorises' ) ) ) ), 'profil_compl' => array ( 'required' => false, 'filters' => array ( array ( 'name' => 'StripTags' ), array ( 'name' => 'StringTrim' ) ), 'validators' => array ( array ( 'name' => 'StringLength', 'options' => array ( 'encoding' => 'UTF-8', 'min' => 1, 'max' => 500 ) ) ) ), 'profil_zone' => array ( 'required' => false, 'filters' => array ( array ( 'name' => 'StripTags' ), array ( 'name' => 'StringTrim' ) ), 'validators' => array ( array ( 'name' => 'StringLength', 'options' => array ( 'encoding' => 'UTF-8', 'min' => 3, 'max' => 6 ) ) ) ) ); } }
ProfilForm.php
[lang=php] <?php namespace Application\Form; use Zend\Form\Form; use Zend\InputFilter\InputFilter; use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator; class ProfilForm extends Form { public function __construct() { parent::__construct('profil'); $this->setAttribute('method', 'post') ->setHydrator(new ClassMethodsHydrator(false)) ->setInputFilter(new InputFilter()); $this->add(array( 'name' => 'profil_fieldset', 'type' => 'Application\Form\ProfilFieldset', 'options' => array( 'use_as_base_fieldset' => true ) )); $this->setValidationGroup( array( 'profil_fieldset' => array( 'profil_first_name', 'profil_last_name', 'profil_cp', 'profil_city', 'profil_adr', 'profil_tel', 'profil_compl', 'profil_zone', ), 'options', 'captcha', 'csrf', 'submit', ) ); $this->add( array( 'name' => 'options', 'type' => 'Select', 'attributes' => array( 'id' => 'inputOptions3', 'class' => 'form-control', 'required' => true ), 'options' => array( 'label' => 'Options de commande', 'value_options' => array( 'Livraison' => 'Livraison', 'Sur-place' => 'Sur place', ), 'empty_option' => '--- Sélectionnez une option de commande ---' ), ) ); $this->add(array( 'type' => 'Zend\Form\Element\Captcha', 'name' => 'captcha', 'options' => array( 'label' => 'Captcha', 'captcha' => array( 'class' => 'figlet', 'wordLen' => 6, 'timeout' => 300, ), ), 'attributes' => array( 'class' => 'form-control', 'id' => 'inputCaptcha3', 'required' => true, 'placeholder' => 'Recopiez le texte ci-dessus', ), )); $this->add(array( 'name' => 'csrf', 'type' => 'Csrf', 'options' => array( 'csrf_options' => array( 'timeout' => 600 ) ) )); $this->add(array( 'name' => 'submit', 'type' => 'Submit', 'attributes' => array( 'value' => 'Suivant', 'id' => 'submit', 'class' => 'btn btn-primary btn-lg' ), )); } }
ProfilController.php
[lang=php] public function indexAction() { $form = new ProfilForm(); // vérifie si le profil de l'utilisateur existe // s'il existe -> récupère ses informations $infos = $this->getProfilTable()->exist($user_id); // Insertion if($infos->num == 0) { $profil = null; } else { // Modification $profil = $infos; } // Insertion if(!$profil) { $profil = new Profil(); } else { // Modification $form->bind($profil); } $request = $this->getRequest(); if ($request->isPost()) { $form->setInputFilter($profil->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $profil = $form->getData(); $options = $request->getPost('options'); $this->getProfilTable()->saveProfil($profil, $user_id, $options); // Redirection vers la dernière étape return $this->redirect()->toRoute('profil', array( 'action' => 'commande', 'id' => $user_id, 'validate' => 'OK', 'opt' => $options )); } } return new ViewModel(array( 'form' => $form )); }
Merci de votre aide
Dernière modification par romsVLM (11-11-2014 18:20:07)
Hors ligne