PhalconPHP数据库加入ORM [英] PhalconPHP database Joins In ORM

查看:96
本文介绍了PhalconPHP数据库加入ORM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解加入Phalcon的情况.我不确定我是否甚至需要使用联接,或者是否以某种方式自动映射了相关表.过去,我一直避免使用ORM,因此这个概念对我来说有点新!!

I'm having difficulty understanding joins in Phalcon. I'm not sure if I even need to use joins or if related tables are somehow mapped automatically. I've always avoided working with ORMs in the past, so the concept is all a bit new to me!

我有两个表-页面和部分

I have two tables - pages and sections

 class Pages extends \Phalcon\Mvc\Model {

    public $page_id;
    public $page_section_id;
    public $page_title;
    public $page_url;
    public $page_description;
    public $page_tags;
    public $page_content;
    public $page_time;
    public $page_author;
    public $page_views;

    public function initialize() {
        $this->belongsTo('page_author', 'users', 'id');
        $this->belongsTo('page_section_id', 'sections', 'section_id');
    }

}

class Sections extends \Phalcon\Mvc\Model{

    public $section_id;
    public $section_title;
    public $section_url;
    public $section_description;
    public $section_tags;

    public function initialize(){
        $this->hasMany('section_id', 'pages', 'page_section_id');
    }
}

然后在我的控制器中有

$pages = $this->modelsManager->createBuilder()
        ->from('Baseapp\Backend\Models\Pages')
        ->leftJoin('Baseapp\Backend\Models\Sections', 'section.section_id = Baseapp\Backend\Models\Pages.page_section_id', 'section')
        ->orderBy('page_time DESC')
        ->getQuery()
        ->execute()
        ->toArray();
    print_r($pages);exit; // <- let's see what we get here

我正在使用$this->modelsManager->createBuilder(),因为我似乎无法使用它进行连接!

I'm using $this->modelsManager->createBuilder() as I don't seem to be able to do joins with out it!

在该视图中,我将使用

{% for page in pages %}
    <div class="item">
        <div class="content">
            <div class="header">
                {{ page.page_title }} - {{ page.section_title }}
            </div>
        </div>
    </div>
{% endfor %}

这里最大的问题是,它仅返回页面数据,而不返回相关的节数据.换句话说,联接似乎没有用.

The big issue here though, is that it is only returning page data and not the related section data. In other words the join just doesn't seem to be working.

那我该怎么做简单的联接,还是我忽略了一些更好的方法使联接变得多余?

So how do I do simple joins, or is there some better method that I'm overlooking that makes joins redundant?

推荐答案

在模型中,您需要创建一个方法,例如

In model you need to create a method, for example

/**
 * Returns users which doesn't belong to any organization.
 *
 * @return mixed
 */
public static function getSomething()
{
    $query = User::query()
        ->columns(__NAMESPACE__ . '\Pages.*')
        ->leftJoin(__NAMESPACE__ . '\Sections', __NAMESPACE__ . '\Pages.page_section_id = ps.section_id', 'ps')
        ->where('do something')
        ->execute();

    if ($query->count()) {
        return $query;
    }

    return false;
}

这篇关于PhalconPHP数据库加入ORM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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