Zend 框架 ->数据库表字段前缀,如 users.us_name [英] Zend framework -> database table field prefix like users.us_name

查看:40
本文介绍了Zend 框架 ->数据库表字段前缀,如 users.us_name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在处理包含许多表的数据库,有许多字段前缀(每个表的两个第一个字母),所以当我有用户表时,我不能使用名称"属性($user->name),但我可以使用:$用户->us_name.


I'm dealing with database containing of many tables, with many field prefixes (two first letters of every table), so when I have users table I cannot use "name" property ($user->name) but I can use: $user->us_name.


我有没有办法简化事情并为表的每个字段设置自动前缀?


I's there a way to simplify things and set automagic prefix for every field of a table ?

推荐答案

您必须扩展 Zend_Db_Table_Row 才能完成此操作.幸运的是,ZF 包含一个专门用于此目的的 _transformColumn() 方法.但我正在超越自己.首先,设置您的表类.这假设您的数据库有一个名为foo_mytable"的表:

You'd have to extend Zend_Db_Table_Row to accomplish this. Fortunately, ZF includes a _transformColumn() method expressly for this purpose. But I'm getting ahead of myself. First, set up your table class. This assumes your database has a table called "foo_mytable":

class MyTable extends Zend_Db_Table_Abstract {
    protected $_name = 'foo_mytable';
    protected $_rowClass = 'My_Db_Table_Row';
}

接下来,创建您的自定义 Row 类:

Next, create your custom Row class:

class My_Db_Table_Row extends Zend_Db_Table_Row {
    protected function _transformColumn($columnName) {
        $columnName = parent::_transformColumn($columnName);
        $prefix = 'us_';
        return $prefix . $columnName;
    }
}

现在,您可以执行以下操作(为简单起见,此示例忽略了 MVC 设计理念):

Now, you can do something like this (for simplicity, this example ignores MVC design ideals):

$table = new MyTable();
$records = $table->fetchAll();
foreach ($records as $record) {
    echo $record->name;
}

假设您的表有一个名为us_name"的列,这应该可以工作.我自己测试过.请注意,在自定义表行中,您可能希望从配置文件中获取表前缀.如果您已将其存储在注册表中,则可以将 $prefix = 'us_'; 替换为 $prefix = Zend_Registry::get('tablePrefix');.

Assuming your table has a column named "us_name", this should work. I tested it myself. Note that in your custom table row, you might want to grab the table prefix from a config file. If you've got it stored in your registry, you could replace $prefix = 'us_'; with $prefix = Zend_Registry::get('tablePrefix');.

这篇关于Zend 框架 ->数据库表字段前缀,如 users.us_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆