繁体   English   中英

Phalcon hasManyToMany数据加载非常慢

[英]Phalcon hasManyToMany data load very slow

我的hasManyToMany模型加载慢日期有问题。

我有代码:

class TvguideChannel extends Model{

public function initialize() {
    $this->setSource('tvguide_channel');
    $this->setConnectionService('db');

    $this->hasManyToMany(
        'code', 
        __NAMESPACE__.'\Tvguide', 
        "ch_code",
        'ch_code', 
        __NAMESPACE__.'\Chgrtv', 
        'ch_code',
        ['alias' => 'tvguide']
    );
    //$this->hasMany('code', __NAMESPACE__.'\Chgrtv', 'ch_code', ['alias' => 'tvgg']);
}

  public function getSource() {
    return 'tvguide_channel';
  }
}

表电视指南具有更多记录(1kk +),但电视指南频道具有228条记录

当我想要从表TvguideChannel输出记录时:

$data = TvguideChannel::find();

我得到加载页面再5秒钟。 我怎样才能正确地与hasManyToMany关系输出所有记录?

您可以通过使用预先加载来解决此问题。 孵化器允许这样做,只需将其包含在您的项目中,然后使用“ with”而不是“ find”即可。

composer require phalcon/incubator

https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Mvc/Model

<?php
use Phalcon\Mvc\Model\EagerLoading\Loader,
    Phalcon\Mvc\Model\EagerLoading\QueryBuilder;

$robotsAndParts = Robot::with('Parts');

// Equivalent to:

$robots = Robot::find();
foreach ($robots as $robot) {
    $robot->parts; // $robot->__get('parts')
}

// Or

$robot = Robot::findFirst()->load('Parts');

// Equivalent to:

$robot = Robot::findFirst();
$robots->parts; // $robot->__get('parts')

// Because Robot::find() returns a resultset, so in that case this is solved with:
$robots = Loader::fromResultset(Robot::find(), 'Parts'); # Equivalent to the second example

// Multiple and nested relations can be used too
$robots = Robot::with('Parts', 'Foo.Bar');

// And arguments can be passed to the find method
$robots = Robot::with('Parts', 'Foo.Bar', ['limit' => 5]);

// And constraints
$robots = Robot::with(
    [
        'Parts',
        'Foo.Bar' => function (QueryBuilder $builder) {
            // Limit Bar
            $builder->limit(5);
        }
    ],
    [
        'limit' => 5
    ]
);

https://github.com/stibiumz/phalcon.eager-loading中作为单个软件包分发

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM