微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Zend查找所有依赖行

举例:“table-> person” – “table-> books”(uses-> person_id) – “table-> notebook”(uses-> person_id)

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

显然有一个小例子我可以轻松检查$person-> hasBooks()|| $person-> hasNotebooks()但随着数据库的增长,鞋子,裤子和眼镜以及许多小东西都在增长.

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

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

或者我在任何时候都必须手动检查每个“connectedGood”?

澄清:我知道我怎么能找到DepentendRowset(‘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();
}

注意:未经测试!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐