问题描述
假设我有一个Phalcon\Mvc\Model
,我已经使用::findFirst($id)
加载了它.
Let's say I have a Phalcon\Mvc\Model
that I load using ::findFirst($id)
.
如何交换将加载模型行并在其他表上进行INNER JOIN的自定义查询?
How can I swap in a custom query that would load the model row and do INNER JOIN on some other table?
谢谢!
推荐答案
我确定您可以将查询"构建器用于简单的联接,例如:
I'm sure you can use the Query builder for simple joins like:
<?php
//Getting a whole set
$robots = $this->modelsManager->createBuilder()
->from('Robots')
->join('RobotsParts')
->orderBy('Robots.name')
->getQuery()
->execute();
//Getting the first row
$robots = $this->modelsManager->createBuilder()
->from('Robots')
->join('RobotsParts')
->orderBy('Robots.name')
->getQuery()
->getSingleResult();
或文档中的PHQL示例:
Or PHQL example from the documentation:
<?php
$phql = "SELECT Robots.*
FROM Robots JOIN RobotsParts p
ORDER BY Robots.name LIMIT 20";
$result = $manager->executeQuery($phql);
默认情况下,假定为INNER JOIN.不过,您可以在查询中指定JOIN的类型.
By default, an INNER JOIN is assumed. You can specify the type of JOIN in the query though.
参考: http: //docs.phalconphp.com/en/latest/reference/phql.html#creating-queries-using-the-query-builder
然后,我将重载模型的findFirst()方法以利用上述代码并将结果值分配给模型的属性.
Then I'd overload model's findFirst() method to utilize the above code and assign result values to model's properties.
这篇关于使用INNER JOIN加载模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!