Propel查询中的动态表名 [英] Dynamic Table Name in Propel Query

查看:47
本文介绍了Propel查询中的动态表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道您是否可以使推进查询的表名动态化(有点像可变变量)?一个例子是 \"DynamicVar"Query::create().我让它在下面的例子中工作,但如果更动态地制作,可以去掉很多行.这些表的设置都相同,因此可以将它们视为同一个对象,只是名称不同.

I am wondering if you can make the Table name of the propel query dynamic(sort of like a variable variable)? An example would be like \"DynamicVar"Query::create(). I have it working in ifs like in my example below, but could get rid of quite a few lines if made more dynamically. The tables are all setup the same, so they can be treated like the same object they just have different names.

目前正在发生这样的事情:

Currently have something like this happening:

//$dynamic is a result of grabbing it from a different table 
//that corresponds to values passed by AJAX
$dyanmic = "Customer"
$query = null;
If( $dynamic == "Customer" ) $query = \CustomerQuery()::create();
If( $dynamic == something2 ) $query = \Table2Query()::create();
If( $dynamic == something3 ) $query = \Table3Query()::create();

If( $query != null ) {
   $query->filterBy("something");
   $query->find();
}

推荐答案

我尝试了一些代码,只要每个表都可以被视为同一个对象,下面的代码将用于动态更改表.您可以定义 $table 并以这种方式将其用于返回所需对象的函数

I played around with the code some, and the code below will work for dynamically changing the table as long as each table can be treated like the same object. You can define your $table and use it in this fashion for a function that returns the object that you want

function Get_Table($table,$id){
    $query = null;
    $queryClass = '\\'. ucfirst($table) . 'Query';
        if ( class_exists( $queryClass ) ) {
            $$dynamics = $queryClass::create()
                ->filterById($id)
                ->find();

            if( $dynamics->getFirst() == null ) { return false; }
            /** @var \Base\.ucfirst($table) $dynamic*/
            $dynamic= $dynamics->getFirst();

            return $dynamic;
        }

        //On Failure
        else {
            return false;
        }
}

这篇关于Propel查询中的动态表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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