我有这样一个数组来呈现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如你所见。我使用动态数据$this->config->resources->db->params->username和$this->estados->getEstados() (来自我的数据库的数据),这些数据只能在数组格式的数据中进入控制器。
我发现这些选项太大了,而且没有必要在控制器中。我想在INI或XML文件中使用Zend_Config。但是如何检索我使用的这些数据(即$this->estados->getEstados())?
发布于 2010-09-28 23:44:40
我设法创建了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
);
}
}然后在我的控制器中设置方法。
$this->tgOptions = new Application_Model_Tablegear();
$this->tgOptions->setDatabase($this->config->resources->db->params)
->setEstados($this->estados->getEstados());发布于 2010-09-28 22:29:49
你可以简单地把事情合并..。例如,假设您在模块或应用程序配置中的大部分配置,您可以创建一个名为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;
}当然,如果你不需要选择,所有这些事情,奥尤可以调整,你认为合适。但是,这允许您传入一个数组来添加或覆盖所有顶级键的选项。您可以编写递归合并函数,以允许您在任何级别向键提供选项,尽管结构必须相同。或者,如果您只需要覆盖selects密钥,则只能执行该操作。
您可以做的另一件事是简单地将配置中的某些内容定义为回调,并假设配置中的键是附加到控制器的变量名,而回调值是方法名,例如:
<selects>
<estados>
<callback>getEstados</callback>
</estados>
</select>然后,在您的控制器中,您可以扫描数组中的回调键,并使用call_user_func或call_user_func_array应用回调。或者用直接的动态方法。
但是:为什么不创建表类呢!?
https://stackoverflow.com/questions/3817374
复制相似问题