【问题标题】:PhalconPHP Joining Models EchoPhalconPHP 加入模型 Echo
【发布时间】:2015-06-01 14:54:51
【问题描述】:

好的,所以我有两张桌子。一个叫Properties,另一个叫PrInfo

Properties 是基表,PrInfo 是另一个包含额外信息的表。

我想将它们连接在一起,以便在网站上显示信息。

    $query = Properties::query()
        ->leftJoin('PrInfo','pr.pr_id = Properties.id','pr')
        ->execute();

在我的脑海中,每个查询的索引都应该与 PrInfo 表中的一行连接,因为声明了“pr.pr_id = Properties.id”

但是,当我键入“print_r($query[1]);”时,我可以在执行的查询中看到 确实包含了 pr_info 表,但是我不确定它是否与属性表中的行匹配。

我尝试过print_r($query[1]['PrInfo'],但是我得到了一个错误。

有什么想法吗?

中有以下内容
          protected '_hasManyToManySingle' => null
      protected '_initialized' => 
        array (size=2)
          ...
      protected '_sources' => null
      protected '_schemas' => null
      protected '_behaviors' => null
      protected '_lastInitialized' => 
        object(PrInfo)[51]
          ...
      protected '_lastQuery' => null
      protected '_reusable' => null
      protected '_keepSnapshots' => null
      protected '_dynamicUpdate' => null
      protected '_namespaceAliases' => null
  protected '_modelsMetaData' => null
  protected '_errorMessages' => null

【问题讨论】:

  • 为什么不直接做一个var_dump($query); 看看里面有什么?
  • 底部有以下内容:
  • 检查更新版本^^^
  • @Crembo 我真的不知道对象是什么意思(PrInfo)[51]

标签: php mysql phalcon


【解决方案1】:

您应该在模型中定义适当的关系,而不是使用leftJoin() 手动加入:

class Properties extends Model
{
    ...

    public function initialize()
    {
        $this->hasMany("id", "PrInfo", "pr_id");
    }

}

在以下位置了解更多信息: http://docs.phalconphp.com/en/latest/reference/models.html#relationships-between-models

【讨论】:

  • 但这仍然不允许我在查询时获取数据
  • 什么意思?例如,只要 prInfo 也是一个模型,您就可以这样做$prop = Properties::findFirst(); $prop->prInfos
  • 添加另一个查询
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
  • 2015-06-11
  • 1970-01-01
  • 2017-06-11
  • 1970-01-01
相关资源
最近更新 更多