2009-02-03 2 views
1

어떻게 관련 객체를 설정하기 위해 Zend_Db 관계를 사용하는 방법입니까? 나는 다음과 같은 코드를 찾고 있어요 :Zend_Db_Table_Row에서 설정을위한 관계를 사용하여

$contentModel = new Content();   
$categoryModel = new Category(); 

$category = $categoryModel->createRow(); 
$category->setName('Name Category 4'); 

$content = $contentModel->createRow(); 
$content->setTitle('Title 4'); 

$content->setCategory($category); 
$content->save(); 

이 작은 도서관 제공 : http://code.google.com/p/zend-framework-orm/

누군가가 그 경험이 있습니까 를? ZF에서 비슷한 계획이 없습니까? 아니면 사용하기에 더 좋은 점이 있습니까?

감사

답변

1

난 항상 Zend_Db_Table 및 Zend_Db_Table_Row을 무시하고 내 자신의 서브 클래스를 사용합니다 (I 교리의 ORM 또는 외부 뭔가를 사용하는 wnat하지 않습니다). 내 Db_Table 클래스에서 내가 가진 :

public function __get($key) 
{ 
    $inflector = new Zend_Filter_Word_UnderscoreToCamelCase(); 

    $method = 'get' . $inflector->filter($key); 

    if(method_exists($this, $method)) { 
     return $this->{$method}(); 
    } 

    return parent::__get($key); 
} 

public function __set($key, $value) 
{ 
    $inflector = new Zend_Filter_Word_UnderscoreToCamelCase(); 

    $method = 'set' . $inflector->filter($key); 

    if(method_exists($this, $method)) 
     return $this->{$method}($value); 

    return parent::__set($key, $value); 
} 

Bascially 단지 getFoo라는 방법을 찾기 위해 클래스를 알려줍니다 (: 나는 __get() 그리고 __set() 함수 다음 한 내 Db_Table_Row에서

protected $_rowClass = 'Db_Table_Row'; 

을) 및 setFoo() 또는 무엇이든간에. 당신은 자신의 논리를 뒤에 쓰는 한 자신의 분야를 구성 할 수 있습니다. 아마도 당신의 경우 :

public function setCategory($value) 
{ 
    $this->category_id = $value->category_id; 
} 
+0

안녕하세요, 답변 해 주셔서 감사합니다.하지만 값만 설정하거나 자체 기능을 정의 할 필요가 없습니다. $ content-> setCategory ($ category); $ content-> save(); 관련 테이블에 새로운 행을 생성하고 관련 외래 키에 바인딩합니다. –

+0

주제를 조금 벗어나지 만 'Zend_Db'에 대해 조사하고 있습니다. 기성품 인'Zend_db_Table_Row'를 대체하는 기사 나 프로젝트에 대한 링크가 있을까요? – troelskn

3

젠드 프레임 워크에서 테이블 관계형 코드를 디자인하고 구현했습니다.

외래 키 (예 : $content->category)에는 참조하는 상위 행의 기본 키 값이 들어 있습니다. 귀하의 예에서 $category은 저장하지 않았기 때문에 기본 키 값을 포함하지 않습니다 (자동 증분 가짜를 사용한다고 가정). 당신이 외국 키를 채울 때까지 당신은 $content 행을 저장할 수 있으므로 참조 무결성은 만족 : 그것은 기본 키 인구가없는 경우 아무 소용이 없을 것이다

$contentModel = new Content();     
$categoryModel = new Category(); 

$category = $categoryModel->createRow(); 
$category->setName('Name Category 4'); 

$content = $contentModel->createRow(); 
$content->setTitle('Title 4'); 

// saving populates the primary key field in the Row object 
$category->save(); 

$content->setCategory($category->category_id); 
$content->save(); 

setCategory()에 행 개체를 전달할 수 . $content->save()은 참조 할 유효한 기본 키 값이없는 경우 실패합니다.

어쨌든 기본 키 필드가 채워 져야하므로 setCategory()으로 전화를 걸 때 필드에 액세스하는 것이 그리 어렵지 않습니다.

관련 문제