2017-12-15 15 views
0

hasManyToMany가있는 모델에서로드하는 데 시간이 오래 걸리는 문제가 있습니다.Phalcon 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'; 
    } 
} 

표 Tvguide 가지고 이상의 레코드 (1kk +),하지만 TvguideChannel 228 개 기록

나는 테이블 TvguideChannel에서 출력 레코드를 원하는가 :

나는 코드가

$data = TvguideChannel::find(); 

을 페이지를 5 초 더로드합니다. 관계 hasManyToMany를 사용하여 모든 레코드를 올바르게 출력하는 방법은 무엇입니까?

+0

에서 하나의 패키지로 분산? – Timothy

+0

예 물론! – Jajaja

+0

관련 모델은 지연로드됩니다. 코드가 요청할 때까지 실제로 쿼리되지 않습니다. 따라서 관계를 설정한다고해서 관련된 모든 기록이 꺼내지는 것은 아닙니다. 그 관계를 제거하면 페이지로드 시간이 줄어들면 놀라실 것입니다. 'microtime()'에'TvguideChannel :: find()'를 싸서 지연을 일으키는 지 확인해 봤습니까? – Pickle

답변

0

대신 열심히로드를 사용하여 문제를 해결할 수 있습니다. 창업 보육 센터에서는 이것을 허용하고 프로젝트에 포함시키고 "찾기"대신 "함께"사용합니다.

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