简体   繁体   English

Phalcon hasManyToMany数据加载非常慢

[英]Phalcon hasManyToMany data load very slow

I have a problem with load slow dates from model with hasManyToMany. 我的hasManyToMany模型加载慢日期有问题。

I have code: 我有代码:

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';
  }
}

Table Tvguide have more records (1kk+), but TvguideChannel have 228 records 表电视指南具有更多记录(1kk +),但电视指南频道具有228条记录

When I Want output records from table TvguideChannel with: 当我想要从表TvguideChannel输出记录时:

$data = TvguideChannel::find();

I get load page more 5 seconds. 我得到加载页面再5秒钟。 How I can output all records correctly with relation hasManyToMany? 我怎样才能正确地与hasManyToMany关系输出所有记录?

You can fix this by using eager loading instead. 您可以通过使用预先加载来解决此问题。 The incubator allow this, just include it into your project and use the "with" instead of "find". 孵化器允许这样做,只需将其包含在您的项目中,然后使用“ with”而不是“ find”即可。

composer require phalcon/incubator

https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Mvc/Model 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
    ]
);

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

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

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