【问题标题】:Zend recursive table dependencies data retrivalZend 递归表依赖数据检索
【发布时间】:2012-09-24 04:46:07
【问题描述】:

我有三个表,incidentsincident_propertiesproperty_types

我想做的是:

  1. incidents 表中查询一行
  2. 检索其所有属性(键值行和 type_id)
  3. 对于每个属性,从property_type 表中检索其类型

所以我建立了这个表关系-

class Incidents extends Zend_Db_Table_Abstract
{
    protected $_name = 'incidents';
    protected $_dependentTables = 'Properties';

}

class IncidentProperties extends Zend_Db_Table_Abstract
{
    protected $_name = 'incident_properties';
    protected $_dependentTables = 'PropertyTypes';
    protected $_referenceMap = array(
        'Incidents' => array(
            'refTableClass' => 'Incidents',
            'refColumns' => 'incident_id'
        )
    );
}

class PropertyTypes extends Zend_Db_Table_Abstract
{
    protected $_name = 'incident_property_types';
    protected $_referenceMap = array(
        'Properties' => array(
            'refTableClass' => 'IncidentProperties',
            'refColumns' => 'property_type_id'
        ) 
    );
}

在我的incidents 模型映射器中,我想做类似的事情:

$select = $this->_dbTable->select()->where('id = ?',$incident->get_id());

$incident_properties = $this->_dbTable
                            ->fetchRow($select)
                            ->findDependentRows('IncidentsProperties')
                            ->toArray();
print_r($incident_properties);

并在$incident_properties 中检索其类型行中的属性键、值和类型。

任何想法如何以正确的方式完成此任务?

【问题讨论】:

    标签: php zend-framework zend-db zend-db-table


    【解决方案1】:

    好吧,我们走吧。

    1:您总是必须在 $_dependentTables 中添加完整的类名。因此,您不要添加数据库中的表名,而是添加 Zend_Db_Table_Abstract 实例的类名。

    应该是这样的:

    class Incidents extends Zend_Db_Table_Abstract
    {
        protected $_name = 'incidents';
        protected $_dependentTables = 'IncidentProperties';
    
    }
    

    2:你应该像这样向你的referenceMap添加一个columns属性:

    protected $_referenceMap = array(
        'Incidents' => array(
            'refTableClass' => 'Incidents',
            'refColumns' => 'incident_id',
            'columns' => 'name of the column that references the incident_id'
         )
    );
    

    好的,你想做的是这样的:

    class IncidentMapper
    {
        protected $_dbAdapter; //an instance of your Incident-Class extending Zend_Db_Table_Abstract
    
        public function doSomeStuff($incidentID)
        {
             $incident = $this->_dbAdapter->find($incidentID);
             //fetch dependent rowsets using incident_id
             $incidentProperties = $result->findDependentRowset('IncidentProperties', 'Incidents');
             foreach($incidentProperties as $incidentProperty)
             {
                 //fetch parent row using property_type_id
                 $propertyType = $incidentProperty->findParentRow('PropertyTypes', 'Properties');
             }
        }
    }
    

    然后,如果您想将适配器用作数据数组,则必须调用

    ->toArray()

    关于你的结果。

    因此,首先,您会通过

    收到您的 Incidences-Class 的实例

    ->find($incidentID)

    然后你打电话

    ->findDependentRowset('ReferencedClass', 'Rule')
    

    获取所有带有已获取事件 ID 的 IncidentProperties。 然后你遍历所有找到的属性并调用

    ->findParentRow('ReferencedClass', 'Rule')
    

    获取 PropertyType。

    我希望它有所帮助。如果您需要更多信息,请查看ReferenceGuide - Zend_Db_Table

    起初它有点难以理解和混淆。如果您有任何其他问题,请随时提出。

    编辑:

    目前我不太确定您收到的属性是适配器还是数据数组。 (不幸的是我现在无法测试它) 因此,如果您遇到问题,您可能必须按以下方式获取每个 IncidentProperty:

    $_incidentPropertyAdapter->find($incident_property_id)
    

    在你可以打电话之前

    ->findParentRow(...)
    

    【讨论】:

    • 好的,我知道了,但是我还有另一个问题,这有点离题 - 不使用这些 findDependentRowset() 和 findParentRow() 函数会生成另外两个对数据库的选择查询可能比只使用一个 JOIN 查询效率低?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 2014-10-15
    • 2015-01-08
    • 1970-01-01
    • 2015-03-06
    相关资源
    最近更新 更多