简体   繁体   中英

Using Zend_Config INI or XML with dynamic data

I have such an array for rendering TableGear :

array(
            "database" => array(
                'username' => $this->config->resources->db->params->username,
                'password' => $this->config->resources->db->params->password,
                'name' => $this->config->resources->db->params->dbname,
                'table'    => "projetos",
                'fields' => array(
                    'pro_cliente',
                    'pro_area',
                    'pro_evento',
                    'estado',
                    'cidade',
                    'pro_mes',
                    'pro_montagem_inicio',
                    'pro_montagem_fim',
                    'pro_evento_inicio',
                    'pro_evento_fim',
                    'pro_desmontagem_inicio',
                    'pro_desmontagem_fim',
                    'pro_atendimento',
                    'pro_projeto',
                    'pro_situacao'
                )
                //"noAutoQuery" => true
            ),
            "selects" => array(
                'pro_situacao' => array('Aberto', 'Em Andamento', 'Fechado', 'Cancelado'),
                'estado' => $this->estados->getEstados()
            ),
            "formatting" => array(
                'pro_valor' => 'currency[prefix=R$ ,pad]',
                'pro_montagem_inicio' => 'date[d/m]',
                'pro_montagem_fim' => 'date[d/m]',
                'pro_evento_inicio' => 'date[d/m]',
                'pro_evento_fim' => 'date[d/m]',
                'pro_desmontagem_inicio' => 'date[d/m]',
                'pro_desmontagem_fim' => 'date[d/m]'
            ),
            'headers' => array(
                'pro_id' => 'ID',
                'pro_cliente' => 'Cliente',
                'pro_area' => 'Area',
                'pro_evento' => 'Evento',
                'estado' => 'UF',
                'cidade' => 'Cidade',
                'pro_mes' => 'Mes',
                'pro_montagem_inicio' => 'Inicio Montagem',
                'pro_montagem_fim' => 'Fim Montagem',
                'pro_evento_inicio' => 'Inicio Evento',
                'pro_evento_fim' => 'Fim Evento',
                'pro_desmontagem_inicio' => 'Inicio Desmontagem',
                'pro_desmontagem_fim' => 'Fim Desmontagem',
                'pro_atendimento' => 'Atendimento',
                'pro_projeto' => 'Projeto',
                'pro_situacao' => 'Situacao',
                'pro_valor' => 'Valor',
                'DELETE' => 'Deletar'
            ),
            'columns' => array(
                'pro_montagem_inicio' => 'date-picker',
                'pro_montagem_fim' => 'date-picker',
                'pro_evento_inicio' => 'date-picker',
                'pro_evento_fim' => 'date-picker',
                'pro_desmontagem_inicio' => 'date-picker',
                'pro_desmontagem_fim' => 'date-picker'
            ),
            'allowDelete' => false,
            'editable' => 'none'
        ); // End of Tablegear

As you can see. I use dynamic data $this->config->resources->db->params->username and $this->estados->getEstados() (data from my database) which I can only get inside the controller in an array-form data.

I found these options too large and unnecessary to be in the controller. I'd like to use Zend_Config with a INI or XML file. But how can I retrieve these data I use (ie $this->estados->getEstados() )?

Well you could simply merge things... for example lets say you the majority of your configuration within module or application configuration you could make a method called configureTableGearRendering :

protected function configureTableGearRendering(array $selects, $database = array(), $formatting = array(), $headers = array(), $allowDelete = null, $editable = null)
{
   $config = $this->config->tableGear;
   $config['selects'] = array_merge($config, $selects);
   $config['database'] = array_merge($config, $database);
   $config['fromatting'] = array_merge($config, $formatting);

   if(null !== $allowDelete)
   {
     $config['allowDelete'] = $allowDelete;
   }

   if(null !== $editable)
   {
     $config['editable'] = $editable;
   }

   return $config;
}

Of course if you don need to option all those things oyu can adjust as you see fit. But this allows you to pass in an array to add or override options for all your top level keys. You could als write a recursive merging function to allow you to supply options to a key at any level, though the structures would have to be the same. Or, if you only need to override the selects key you could do only that one.

Another thing you could do is simply define certain thing sin the configuration as callbacks and assume that the key in config is the variable name attached to the controller and that the callback value is the method name for example:

<selects>
  <estados>
    <callback>getEstados</callback>
  </estados>
</select>

Then in your controller you can scan for callback keys in the array and apply the call back with call_user_func or call_user_func_array ... or with straight up dynamic methods.

However: WHY ARE YOU NOT CREATING TABLE CLASSES!?

I managed to create the Tablegear model to handle this problem

<?php

class Application_Model_Tablegear
{
protected $_name = null;
protected $_username = null;
protected $_password = null;
protected $_estados = null;
protected $_allowDelete = false;
protected $_editable = 'all';

public function allowDelete() {
    $this->_allowDelete = true;
    return $this;
}

public function setEditable($fields) {
    $this->_editable = $fields;
    return $this;
}

public function setEstados($estados) {
    $this->_estados = $estados;
    return $this;
}

public function setDatabase($params) {
    $this->_username = $params->username;
    $this->_password = $params->password;
    $this->_name = $params->dbname;
    return $this;
}

public function getOptions() {
    return array(
            "database" => array(
                'username' => $this->_username,
                'password' => $this->_password,
                'name' => $this->_name,
                'table'    => "projetos",
                'fields' => array(
                    'pro_cliente',
                    'pro_area',
                    'pro_evento',
                    'estado',
                    'cidade',
                    'pro_mes',
                    'pro_montagem_inicio',
                    'pro_montagem_fim',
                    'pro_evento_inicio',
                    'pro_evento_fim',
                    'pro_desmontagem_inicio',
                    'pro_desmontagem_fim',
                    'pro_atendimento',
                    'pro_projeto',
                    'pro_situacao'
                )
                //"noAutoQuery" => true
            ),
                "selects" => array(
                    'pro_situacao' => array('Aberto', 'Em Andamento', 'Fechado', 'Cancelado'),
                    'estado' => $this->_estados
                ),
                "formatting" => array(
                    'pro_valor' => 'currency[prefix=R$ ,pad]',
                    'pro_montagem_inicio' => 'date[d/m]',
                    'pro_montagem_fim' => 'date[d/m]',
                    'pro_evento_inicio' => 'date[d/m]',
                    'pro_evento_fim' => 'date[d/m]',
                    'pro_desmontagem_inicio' => 'date[d/m]',
                    'pro_desmontagem_fim' => 'date[d/m]'
                ),
                'headers' => array(
                    'pro_id' => 'ID',
                    'pro_cliente' => 'Cliente',
                    'pro_area' => 'Area',
                    'pro_evento' => 'Evento',
                    'estado' => 'UF',
                    'cidade' => 'Cidade',
                    'pro_mes' => 'Mes',
                    'pro_montagem_inicio' => 'Inicio Montagem',
                    'pro_montagem_fim' => 'Fim Montagem',
                    'pro_evento_inicio' => 'Inicio Evento',
                    'pro_evento_fim' => 'Fim Evento',
                    'pro_desmontagem_inicio' => 'Inicio Desmontagem',
                    'pro_desmontagem_fim' => 'Fim Desmontagem',
                    'pro_atendimento' => 'Atendimento',
                    'pro_projeto' => 'Projeto',
                    'pro_situacao' => 'Situacao',
                    'pro_valor' => 'Valor',
                    'DELETE' => 'Deletar'
                ),
                'columns' => array(
                    'pro_montagem_inicio' => 'date-picker',
                    'pro_montagem_fim' => 'date-picker',
                    'pro_evento_inicio' => 'date-picker',
                    'pro_evento_fim' => 'date-picker',
                    'pro_desmontagem_inicio' => 'date-picker',
                    'pro_desmontagem_fim' => 'date-picker'
                ),
                'allowDelete' => $this->_allowDelete,
                'editable' => $this->_editable
            );
    }
}

Then I set the methods in my controller

$this->tgOptions = new Application_Model_Tablegear();
$this->tgOptions->setDatabase($this->config->resources->db->params)
            ->setEstados($this->estados->getEstados());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM