Php 对动态数据使用Zend_Config INI或XML

Php 对动态数据使用Zend_Config INI或XML,php,zend-framework,zend-config,Php,Zend Framework,Zend Config,我有这样一个用于渲染TableGear的数组: 如你所见。我使用数据库中的动态数据$this->config->resources->db->params->username和$this->estados->getEstados数据,这些数据只能以数组形式从控制器中获取 我发现这些选项太大,不需要放在控制器中。我想将Zend_Config与INI或XML文件一起使用。但我如何检索我使用的这些数据,即$this->estados->getEstados?您可以简单地合并这些数据。。。例如,假设您在

我有这样一个用于渲染TableGear的数组:

如你所见。我使用数据库中的动态数据$this->config->resources->db->params->username和$this->estados->getEstados数据,这些数据只能以数组形式从控制器中获取


我发现这些选项太大,不需要放在控制器中。我想将Zend_Config与INI或XML文件一起使用。但我如何检索我使用的这些数据,即$this->estados->getEstados?

您可以简单地合并这些数据。。。例如,假设您在模块或应用程序配置中完成了大部分配置,您可以创建一个名为configureTableGearRendering的方法:

当然,如果你不需要选择所有这些东西,oyu可以根据自己的意愿进行调整。但这允许您传入一个数组,以添加或覆盖所有顶级键的选项。您可以编写一个递归合并函数,允许您在任何级别为键提供选项,尽管结构必须相同。或者,如果只需要覆盖selects键,则只能覆盖该键

您可以做的另一件事是简单地将配置中的某些内容定义为回调,并假设config中的键是附加到控制器的变量名,回调值是方法名,例如:

<selects>
  <estados>
    <callback>getEstados</callback>
  </estados>
</select>
然后在控制器中,您可以扫描数组中的回调键,并使用call\u user\u func或call\u user\u func\u数组应用回调。。。或者直接使用动态方法


但是:为什么不创建表类

我设法创建了Tablegear模型来处理这个问题

<?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
            );
    }
}

相信我,你不会想那样做的。这样会快得多。事实上我是,但我发现了这个自动从数据库检索的TableGear。
<selects>
  <estados>
    <callback>getEstados</callback>
  </estados>
</select>
<?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
            );
    }
}
$this->tgOptions = new Application_Model_Tablegear();
$this->tgOptions->setDatabase($this->config->resources->db->params)
            ->setEstados($this->estados->getEstados());