如何在Cakephp 3.x中编写延迟连接?

时间:2015-06-28 14:45:16

标签: cakephp cakephp-3.0

我想要这个查询。

List<T>

这是我到目前为止所尝试过的。

public class JarOfSweets : IJarOfSweets{
    public List<ISweet> _list = new List<ISweet>();

    public void Shuffle()
    {
        throw new NotImplementedException();
    }
    public ISweet TakeSweetFromJar()
    {
        throw new NotImplementedException();
    }

    public void Add(Sweet sweet)
    {
        _list.Add(sweet);
    }

    public int Count
    {
        get { return _list.Count; }
    }

    public IEnumerator<ISweet> GetEnumerator()
    {
        return _list.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return _list.GetEnumerator();
    }
}

这会产生以下查询,该查询由于子查询中的SELECT Films.id AS `Films__id`, Films.name AS `Films__name` FROM films Films INNER JOIN (SELECT id FROM films ORDER BY released DESC LIMIT 10 OFFSET 100) AS Films2 ON Films.id = Films2.id ORDER BY Films.released DESC 别名而产生错误。我不知道如何更改别名。

    $subquery = $this->find()->select(['id']);
    $query = $this->find()
        ->innerJoin(['Films2' => $subquery], ['Films.id = Films2.id'])
        ->order(['released' => 'DESC']);

1 个答案:

答案 0 :(得分:1)

您只需要从子查询中为返回的列添加别名。由于条件为Films.id = Films2.id,因此需要在子查询中返回别名为id的列。这是如何:

$subquery = $this->find()->select(['id' => 'id']); // Avoid automatic aliasing
$query = $this->find()
    ->innerJoin(['Films2' => $subquery], ['Films.id = Films2.id'])
    ->order(['released' => 'DESC']);