【问题标题】:All-In-One Active Record get() method or ORM (PHP-Active Record)多合一 Active Record get() 方法或 ORM(PHP-Active Record)
【发布时间】:2011-02-06 20:26:58
【问题描述】:

像下面这样的通用 get() 方法是个好主意吗

function universal_get( $select=NULL, $from=NULL, $where=NULL, $group=NULL, $having=NULL, $order=NULL, $limit=NULL )
{

    // condition checks
    if ( is_string( $select ) OR is_array ( $select ))      { $this->db->select( $select ); }
    if ( is_string( $from ) )                               { $this->db->from( $from ); }
    if ( is_array( $where ) )                               { $this->db->where( $where ); }
    if ( is_string( $group ) OR is_array( $group ) )        { $this->db->group_by( $group ); }
    if ( is_array( $having ) )                              { $this->db->having( $having ); }
    if ( is_array( $order ) )                               { $this->db->order_by( $order ); }
    if ( is_string( $limit ) OR is_int( $limit ) )          { $this->db->limit( $limit ); }

    $result = $this->db->get( $from );

    if ( $result->num_rows() > 0 )     { return $result->result(); }
    else                               { return FALSE; }
}

或者像 PHP-ActiveRecord 这样的 ORM 会是更好的选择吗?

谢谢

【问题讨论】:

    标签: orm codeigniter activerecord


    【解决方案1】:

    海峡前进:否

    如果你是一名程序员,那就写出好的代码。当我开始使用 CI 时,我到处都在制作这样的函数,这非常令人困惑。 CI 是一个框架,所以基本上你是在框架之外构建一个框架。

    要记住的另一件事是,您需要始终知道代码中发生了什么。功能太多会导致难以追踪的错误,并将可用性限制在特定应用程序中。例如,如果您想使用联接怎么办?所有设计合理的数据库最终都会使用连接。

    【讨论】:

    • 我怀疑我太过分了,在框架中构建了一个框架,这就是为什么我决定在这里发布它并征求其他程序员的意见。
    • 您提出了一个有效的观点,即始终需要知道代码中发生了什么。谢谢迈克尔
    【解决方案2】:

    我认为这不是一个好主意。 CI 的 Active Record 会处理您不想处理的事情......添加斜线、链接 where 和其他一些......只需查看他们的类。

    http://codeigniter.com/user_guide/database/active_record.html

    【讨论】:

    • 他显然知道他们的课程,因为他在他的例子中使用了它。
    猜你喜欢
    • 2010-11-09
    • 2018-07-24
    • 2011-02-17
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多