2011-02-23 5 views
1

멀티 사이트 어플리케이션을위한 제품 모델이 있습니다.런타임시 CakePHP가 가상 필드를 변경합니다.

도메인 (사이트)에 따라 다른 데이터를로드하고 싶습니다.

예를 들어 내 데이터베이스에 namedescription 필드가있는 대신 posh_name, cheap_name, posh_description 및 cheap_description이 있습니다.

나는 이런 식으로 뭔가를 설정 한 경우 :

class Product extends AppModel 
{ 
    var $virtualFields = array(
     'name' => 'posh_name', 
     'description' => 'posh_description' 
    ); 
} 

을 그리고 항상 모델 또는 협회를 통해 직접 액세스 여부, 작동합니다.

하지만 도메인에 따라 가상 필드가 달라야합니다. 그래서 처음에 2 세트를 만들었습니다.

그렇다면이 두 세트는 도메인에 기반한 올바른 세트를 어떻게 할당합니까? 저 밑쪽 도메인에 있는지 알 수있는 isCheap()이라는 전역 함수가 있습니다. 이것은 나에게 오류를 제공

var $virtualFields = isCheap() ? $this->cheapVirtualFields : $this->poshVirtualFields; 

:

그래서이 시도. 분명히 이와 같은 클래스 정의에서 변수를 할당 할 수 없습니다.

그래서 내가 대신 내 제품 모델에 넣고 :

function beforeFind($queryData) 
{ 
    $this->virtualFields = isCheap() ? $this->cheapVirtualFields : $this->poshVirtualFields; 

    return $queryData; 
} 

이 데이터 모델에서 직접 액세스 경우에만 작동, 데이터 모델 협회를 통해 액세스 할 때 작동하지 않습니다.

제대로 작동하려면이 방법이 있어야합니다. 방법?

답변

1

을 잘 나는 beforeFind 콜백 대신 생성자에 넣어 경우가 작동하는 것 같다 a CakePHP아니요 다시 물어 볼 수 있습니까?

+0

추가 매개 변수 없이는 함수 __construct() {} 일뿐입니다. – Wayne

+0

@Wayne, 실제로 API는 다음과 같이 말합니다 :'Model :: __ construct()를 오버라이드 할 때 parent :: __ construct ($ id, $ table, $ ds); http://api13.cakephp.org/class/model#method-Model__construct –

+0

덕분에, 나는 그것을 몰랐다. 이전에 __construct() {}를 사용하여 오류를 발견하지 않았습니다. – Wayne

0

은 모델 연관성이 비행 중에 만들어진 모델 일 수있는 것처럼 보입니다. 예 : AppModel

pr (get_class ($ this-> Relation)); 코드에서 출력물이 무엇인지 확인하십시오. AppModel이 아닌 모델 이름이어야합니다. 이 경우 나는 확실하지 않다,

class Product extends AppModel 
{ 
    var $poshVirtualFields = array(
     'name' => 'posh_name', 
     'description' => 'posh_description' 
    ); 

    var $cheapVirtualFields = array(
     'name' => 'cheap_name', 
     'description' => 'cheap_description' 
    ); 

    function __construct($id = false, $table = null, $ds = null) { 
     parent::__construct($id, $table, $ds); 
     $this->virtualFields = isCheap() ? $this->cheapVirtualFields : $this->poshVirtualFields; 
    } 
} 

을하지만 : 또한

시도하고 사용

var $poshVirtualFields = array(
    'name' => 'Model.posh_name', 
    'description' => 'Model.posh_description' 
); 

var $cheapVirtualFields = array(
    'name' => 'Model.cheap_name', 
    'description' => 'Model.cheap_description' 
); 
관련 문제