2017-12-05 8 views
2

findFirst() 호출을 통해 데이터를 검색하려고했지만 작동하지 않습니다. 나는 많은 노력을하고 도처에서 찾았지만 해결책을 찾지 못했습니다. phalconphp hasMany() 및 belongsTo() join

내가이 테이블이 : 내 과일 모델 @Initialize()에서

- fruit 
    fruit_id 
    fruit_name 
- fruit_color 
    fruit_id 
    fruit_color_name 

를, 내가 선언 : 내 fruit_color 모델에서

$this->hasOne('fruit_id', 'fruitColor', 'fruit_id'); 

, @initalize는(), 내가 가진 :

$this->belongsTo('fruit_id', 'fruit', 'fruit_id'); 

find() 또는 findFirst() 메서드를 호출하면 아무 것도 검색 할 수 없습니다. 디버그 및 인쇄 할 때 아무 조인도 호출되지 않은 것을 볼 수 있으며 간단한 SELECT (*) 만 얻을 수 있습니다.

실마리가 있습니까?

답변

2

나는 모델 관계에 대한 아이디어를 이해하지 못했다고 생각합니다. 나는 예제로 설명하려고 노력할 것이다.

모델 관계가 조인을 전혀 만들지 않습니다. 관련 모델을 요청할 때 추가 쿼리를 생성합니다. 위에서 정의 된 관계와 모델의 관계

public function initialize() 
{ 
    $this->hasMany('id', 'Models\ServicesVideos', 'service_id', [ 
     'alias' => 'videos', 
     'params' => [ 
      'order' => 'position ASC', 
      'conditions' => 'type = :type:', 
      'bind' => [ 
       'type' => get_class($this) 
      ] 
     ] 
    ]); 
} 

당신과 같이 사용할 수 있습니다 :

$user = Users::findFirst(12); 
$user->videos; // This will return all videos for this user 
모델 관계에 대한

더 많은 정보 : https://docs.phalconphp.com/en/3.2/db-models-relationships


그러나 경우 너는 2 또는 m에 가입해야한다. 당신이 쿼리 빌더을 사용하고자하는 광석 테이블. 쿼리 작성기에 대한

$lang = $this->getDI()->getSession()->language; 
$cacheFile = 'news-'. $lang; 
$items = $this->modelsManager->createBuilder() 
    ->columns([ 
     'main.id', 
     'main.title', 
     'main.slug', 
     'main.summary', 

     'upload.id AS imageId', 
     'upload.filename AS imageFilename', 
     'upload.ver AS uploadVersion', 
    ]) 
    ->from(['main' => 'Models\News']) 
    ->leftJoin('Models\Uploads', 'upload.foreign_key = main.id AND upload.section = "newsImage" AND upload.is_active = "1" AND upload.is_default = "1"', 'upload') 
    ->where('main.is_active = 1') 
    ->andWhere('main.lang = :lang:', ['lang' => $lang]) 
    ->orderBy('main.ord ASC') 
    ->getQuery()->cache(['key' => $cacheFile])->execute(); 

더 많은 정보는 : https://docs.phalconphp.com/en/3.2/db-models 안녕

+1

... 난 그냥 뭔가가되었으며, hasOne의/hasMany의 기능에 PARAM 배열 : 감사합니다! – Finkle