举个例子:"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/