Zend FR

Consultez la FAQ sur le ZF avant de poster une question

Vous n'êtes pas identifié.

#1 22-08-2017 23:11:41

ktraore
Nouveau membre
Date d'inscription: 15-08-2016
Messages: 3

zend-db

salut,
je viens de commencer avec zend framework 3, je me suis de cette tutorial   https://docs.zendframework.com/tutorial … d-models/, tout va tres bien, tout marche correctement. le probleme est que qunad je essaye de faire une requete entre plusieurs table galere je ne sais pas d'ou vienne mes erreur. par exemple j'ai 2 tables: t_clients (cl_id, name_cl,contant) et  t_commands (cmd_id,date_cmd,cl_id), avec cl_id comme cle etrangere dans la table commands.
donc, l'idee est d'afichier la liste des commandes avec les noms des clients par ordre decroissant de la date de commands (date_cmd).
aidez moi svp! je utilise zend-db, j'ai essaye pas mal de tuto et forums mais je ne pas trouver la solution.


voila le fichier  Telimani/src/Model/CommadsTable.php



Code:

[lang=php]<?php

namespace Telimani\Model;

use RuntimeException;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\TableGateway\TableGatewayInterface;
use Zend\Db\ResultSet\ResultSet;
//use Zend\Db\Sql\Select;
//use Zend\Db\Sql\Sql;
use Zend\Db\TableGateway\TableGateway;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;

class CommandsTable  
{
    private $tableGateway;
    

    public function __construct(TableGatewayInterface $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    /* public function __construct(AdapterInterface $adapter)
    {
        $this->adapter = $adapter;
    }*/
    
   
    public function fetchAll()
    {
      // $db=Zend_Db_Table_Abstract::getDefaultAdapter();
       //$db->setFetchMode(Zend_Db::FETCH_OBJ);
        /*$resultSet = $this->adapter->query('SELECT * FROM `commands` WHERE `cmd_id` => 2', array(2));
      return $resultSet;*/
        /*$sql = new Sql($this->tableGateway->getAdapter());
        $select = $sql->select();
        $Select ->from('commands');

      $result = $this->tableGateway->select($select);
      
      return $result;*/
      //$sql=query('SELECT * FROM commands WHERE cmd_id => 2');
      $resultSet = $this->tableGateway->query('SELECT * FROM `user` WHERE `id` = ?', [2]);
      
      return  $resultSet;
      //return  $this->tableGateway->select();
       
    }
}

le fichier  Telimani/src/Controller/CommadsController.php


Code:

[lang=php]
<?php
   namespace Telimani\Controller;
   
   use Telimani\Form\CommandsForm;
   use Telimani\Model\Commands;
   use Telimani\Model\CommandsTable;
   use Zend\Mvc\Controller\AbstractActionController;
   use Zend\View\Model\ViewModel;


   class CommandsController extends AbstractActionController
   {
      // Add this property:
       private $table;

       // Add this constructor:
       public function __construct(CommandsTable $table)
       {
           $this->table = $table;
       }

       public function indexAction()
       {
            return new ViewModel([
               'commands' => $this->table->fetchAll(),
           ]);
       }

      
   }

fichier  Telimani/src/Modele.php


Code:

[lang=php]
<?php
   namespace Telimani;
   
    use Zend\Db\Adapter\Adapter;
   use Zend\Db\Adapter\AdapterInterface;
   use Zend\Db\ResultSet\ResultSet;
   //use Zend\Db\Sql\Select;
   //use Zend\Db\Sql\Sql;
   use Zend\Db\TableGateway\TableGateway;
   use Zend\ModuleManager\Feature\ConfigProviderInterface;
   
   
   
   class Module implements ConfigProviderInterface
   {
      // getConfig() method:
       public function getConfig()
       {
           return include __DIR__ . '/../config/module.config.php';
       }
        // getServiceConfig method:
       public function getServiceConfig()
       {
           return [
               'factories' => [
                   //module for commands
                   Model\CommandsTable::class => function($container) {
                       $adapter = $container->get(Model\CommandsTableGateway::class);
                       return new Model\CommandsTable($adapter);
                   },
                   Model\CommandsTableGateway::class => function ($container) {
                       $dbAdapter = $container->get(AdapterInterface::class);
                       $resultSetPrototype = new ResultSet();
                       $resultSetPrototype->setArrayObjectPrototype(new Model\Commands());
                       return new TableGateway('commands', $dbAdapter, null, $resultSetPrototype);
                   },
               ],
           ];
       }
       
       // getControllerConfig method:
       public function getControllerConfig()
       {
           return [
               'factories' => [
                    // ControllerConfig pour AboutController
                   Controller\CommandsController::class => function($container) {
                       return new Controller\CommandsController(
                           $container->get(Model\CommandsTable::class)
                       );
                   },
                   //end
               ],
           ];
       }


   }

fichier  Telimani/config/module.config.php


Code:

[lang=php]
<?php
  namespace Telimani;

use Zend\Router\Http\Segment;
//use Zend\ServiceManager\Factory\InvokableFactory;

return [
    /*'controllers' => [
        'factories' => [
            Controller\AlbumController::class => InvokableFactory::class,
        ],
    ],
*/
    // The following section is new and should be added to your file:
    'router' => [
        'routes' => [
            //new route to controller contact
            'commands' => [
                'type'    => Segment::class,
                'options' => [
                    'route' => '/commands[/:action[/:cmd_id]]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'cmd_id'     => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => Controller\CommandsController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            //end route
        ],
    ],
    
     
      
      
      
    //========
    
     'navigation' => [
        'default' => [
            [
                'label' => 'Home',
                'route' => 'home',
            ],
             //menu access to contact controller
            [
                'label' => 'Commands',
                'route' => 'commands',
                'pages' => [
                    [
                        'label'  => 'Add',
                        'route'  => 'commands',
                        'action' => 'add',
                    ],
                    [
                        'label'  => 'Edit',
                        'route'  => 'commands',
                        'action' => 'edit',
                    ],
                    [
                        'label'  => 'Delete',
                        'route'  => 'commands',
                        'action' => 'delete',
                    ],
                ],
            ],
        ],
    ],

    

    'view_manager' => [
        'template_path_stack' => [
            'telimani' => __DIR__ . '/../view',
        ],
    ],
];

fichier  config/autoload/global.php

Code:

[lang=php]
<?php
/**
 * Global Configuration Override
 *
 * You can use this file for overriding configuration values from modules, etc.
 * You would place values in here that are agnostic to the environment and not
 * sensitive to security.
 *
 * @NOTE: In practice, this file will typically be INCLUDED in your source
 * control, so do not include passwords or other sensitive information in this
 * file.
 */

return array(
   'db' => array(
       'driver'    => 'Pdo',
       'dsn'       => "pgsql:host=localhost;dbname=db_fastfoods",
       'username'  => 'milano',
       'password'  => 'cheva7',
   ),
   );

votre aide serra la bienvenue,
merci!

Dernière modification par ktraore (22-08-2017 23:15:02)

Hors ligne

 

Pied de page des forums

Propulsé par PunBB
© Copyright 2002–2005 Rickard Andersson
Traduction par punbb.fr

Graphisme réalisé par l'agence Rodolphe Eveilleau
Développement par Kitpages