要在嵌套集中获取树,我们必须做一个笛卡尔积(每个与每个)。
select * from tree as t1, tree as t2
或者
select * from tree as t1 cross join tree as t2
使用 zf1 很容易,但是在 zf2 中使用\Zend\Db\Sql\Select对象时没有交叉连接?
有没有办法使用 Select() 来实现它?还是纯 sql 查询是唯一的方法?
不幸的是,谷歌搜索失败了,所以提前感谢您的任何建议。
要在嵌套集中获取树,我们必须做一个笛卡尔积(每个与每个)。
select * from tree as t1, tree as t2
或者
select * from tree as t1 cross join tree as t2
使用 zf1 很容易,但是在 zf2 中使用\Zend\Db\Sql\Select对象时没有交叉连接?
有没有办法使用 Select() 来实现它?还是纯 sql 查询是唯一的方法?
不幸的是,谷歌搜索失败了,所以提前感谢您的任何建议。
可以将连接类型作为最后一个参数传递
class Select extends AbstractSql implements SqlInterface, PreparableSqlInterface
{
const JOIN_INNER = 'inner';
const JOIN_OUTER = 'outer';
const JOIN_LEFT = 'left';
const JOIN_RIGHT = 'right';
const SQL_STAR = '*';
const ORDER_ASCENDING = 'ASC';
const ORDER_DESENDING = 'DESC';
public $where; // @param Where $where
public function __construct($table = null);
public function from($table);
public function columns(array $columns, $prefixColumnsWithTable = true);
public function join($name, $on, $columns = self::SQL_STAR, $type = self::JOIN_INNER);
public function where($predicate, $combination = Predicate\PredicateSet::OP_AND);
public function group($group);
public function having($predicate, $combination = Predicate\PredicateSet::OP_AND);
public function order($order);
public function limit($limit);
public function offset($offset);
}
所以,zf1 方法joinCross(table, [columns])
等价于 zf2join($name, $on, $columns, 'cross')
CROSS JOIN 操作可以替换为 (INNER) JOIN,其中 join 子句的计算结果始终为 true (1=1)
select * from tree as t1 inner join tree as t2 on 1=1
Zf2 示例:
$select = new Sql\Select;
$select ->from(array('node' => 'nested_category'))
->columns(array('node_name' => new Sql\Expression('CONCAT(REPEAT(" ", COUNT(parent.name)-1), node.name)')))
->join(array('parent' => 'nested_category'), new Sql\Expression('1=1'), array('id'))
->where('node.lft BETWEEN parent.lft AND parent.rgt')
->group('node.name')
->order('node.lft');
$resultSet = $this->selectWith($select);