php – Zend查找所有依赖行
发布时间:2020-12-13 16:13:45 所属栏目:PHP教程 来源:网络整理
导读:举例:“table- person” – “table- books”(uses- person_id) – “table- notebook”(uses- person_id) 在我的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(); } 注意:未经测试! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |