在 Zend_Db_Table_Row 中使用关系进行设置

发布于 2024-07-13 12:11:25 字数 617 浏览 11 评论 0原文

有没有办法使用 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/< /a>

有人有这方面的经验吗? ZF没有类似的计划吗? 或者有什么更好用的吗? (我不想使用 ORM 或外部的东西)

谢谢

is there a way how to use Zend_Db relations for setting related objects?
I am looking for something like following code:

$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();

this provides small library:
http://code.google.com/p/zend-framework-orm/

does somebody have experience with that? Isn't there a plan for something similar in ZF ? Or is there something better for use? (I don't wnat to use doctrine ORM or something external)

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初心未许 2024-07-20 12:11:25

我在 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();

将 Row 对象传递给 setCategory() 没有任何好处如果没有填充主键。 如果没有有效的主键值可供引用,$content->save() 将失败。

由于在任何情况下都需要填充该主键字段,因此在调用 setCategory() 时访问该字段并不困难。

I designed and implemented the table-relationships code in Zend Framework.

A foreign key ($content->category in your example) contains the value of the the primary key in the parent row it references. In your example, the $category doesn't contain a primary key value yet because you haven't saved it (assuming it uses an auto-incrementing pseudokey). You can't save the $content row until you populate its foreign key, so referential integrity is satisfied:

$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();

It would do no good to pass the Row object to setCategory() if it doesn't have the primary key populated. $content->save() will fail if it doesn't have a valid primary key value to reference.

Since you need that primary key field to be populated in any case, it's not so difficult to access the field when you call setCategory().

一袭白衣梦中忆 2024-07-20 12:11:25

我总是重写 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;
}

I always override Zend_Db_Table and Zend_Db_Table_Row and use my own subclasses. In my Db_Table class I have:

protected $_rowClass = 'Db_Table_Row';

In my Db_Table_Row I have the following __get() and __set() functions:

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 that just tells the class to look for methods called getFoo() and setFoo() or whatever. You could then pretty much make up your own fields as long as your write your own logic behind. In you case maybe:

public function setCategory($value)
{
     $this->category_id = $value->category_id;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文