php - Zend 查找所有相关行

标签 php zend-framework dependencies zend-db

举个例子:"table->person"- "table->books"(uses->person_id) - "table->notebook"(uses->person_id)

在我的 Zend 类(class)中,我定义了从人到书籍和笔记本的所有关系以及相反的关系。现在很明显,如果我想删除那个人,我的应用程序应该确保这个人不再拥有任何东西(至少这是我想要实现的)。

显然,通过一个小示例,我可以很容易地检查 if $person->hasBooks() || $person->hasNotebooks() 但随着数据库的增长,这里有鞋子、裤子、眼镜和许多小东西。

是否有任何想要以类似的方式自动化

foreach ( connectedGoods in person as theGood) 
{
  if ( person->hasGood( theGood ) ) {
    //log person still uses theGood
  } 
} 

还是我必须始终手动检查每个“connectedGood”?

澄清一下:我确实知道如何findDepentendRowset('singleTable') - 我只是想知道是否有类似findDepentendRowset('allDependentTables')

提前致谢

//编辑 这是我当前的表结构,可以提供更多信息:

tbl_buildings:
b_id
b_*

tbl_asset_x
a_id
b_id (tbl_buildings)

tbl_asset_y
y_id
b_id (tbl_buildings)

最佳答案

如果我对您的理解正确,这应该可以实现您的目标。我在表行中添加了一个方法来检查它的每个依赖项。

abstract class MyBaseTable extends Zend_Db_Table_Abstract {
    protected $_rowClass = 'MyBaseTableRow';
    public function getReferences() {
        return $this->_referenceMap;
    }
}

abstract class MyBaseTableRow extends Zend_Db_Table_Abstract {
    public function hasDependents() {
        foreach ($this->_getTable()->getReferences() as $entity => $info) {
            if (count($this->findDependentRowset($entity) > 0) {
                return true;
            }
        }
        return false;
    }
}

class Persons extends MyBaseTable {
    protected $_referenceMap    = array(
        'Book' => array(
            'columns'           => 'reported_by',
            'refTableClass'     => 'Books',
            'refColumns'        => 'account_name'
        ),
        'Notebook' => array(
            'columns'           => 'assigned_to',
            'refTableClass'     => 'Notebooks',
            'refColumns'        => 'account_name'
        )
    );
}

$persons = new Persons();
$person = $persons->find(1234);

if ($person->hasDependents()) {
    echo 'freaking remove assets first';    
} else {
    $person->delete();
}

注意:未经测试!

关于php - Zend 查找所有相关行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7833252/

相关文章:

cmake 不会在自定义命令中复制文件

php - (403) 您的项目无权访问此功能

php - Redis读写基本概念

php - 上传图片到服务器时打开流失败

zend-framework - 如何对对象使用 Zend Framework 的部分循环

c++ - 如何在非标准 makefile 名称中使用 makedepend

php - PHP 中的辅助类

php - 如何从 PHP 5.4 连接到 MS SQL 2000?

mysql - Magento - 在类别的自定义设计中添加一个选项

spring - Spring Data 是否强制依赖于 SLF4j?