1

関連オブジェクトを設定するために Zend_Db リレーションを使用する方法はありますか? 次のコードのようなものを探しています:

$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 で同様の計画はありませんか? それとももっと使いやすいものはありますか?(ドクトリンORMや外部のものを使いたくありません)

ありがとう

4

2 に答える 2

3

Zend Framework でテーブル リレーションシップ コードを設計および実装しました。

外部キー($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()主キーが入力されていない場合 、Row オブジェクトを渡しても意味がありません。$content->save()参照する有効な主キー値がない場合、失敗します。

どのような場合でも主キー フィールドにデータを入力する必要があるため、 を呼び出すときにフィールドにアクセスすることはそれほど難しくありませんsetCategory()

于 2009-12-27T00:29:40.397 に答える
1

私は常に Zend_Db_Table と Zend_Db_Table_Row をオーバーライドし、独自のサブクラスを使用しています。私の Db_Table クラスには次のものがあります。

protected $_rowClass = 'Db_Table_Row';

私の Db_Table_Row には、次の __get() および __set() 関数があります。

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

基本的には、getFoo() や setFoo() などと呼ばれるメソッドを探すようにクラスに指示するだけです。独自のロジックを背後に書く限り、独自のフィールドをほとんど作成できます。あなたの場合、多分:

public function setCategory($value)
{
     $this->category_id = $value->category_id;
}
于 2009-02-03T15:40:25.377 に答える