2012-09-23 90 views
1

我有三个表,incidentsincident_propertiesproperty_typesZend递归表依赖关系数据检索

我想要做的是:

  1. 查询的incidents表一行
  2. Retreive其所有属性(这是键值行和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检索其类型行内的属性键,值和类型。

任何想法如何实现这个正确的方式?

回答

0

好吧,放手吧。

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

所以它应该是:

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

} 

2:你应该添加一个栏属性您referenceMap这样的:

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()

上你的结果。

所以刚开始,您通过

->find($incidentID)

收到您的发生率,类的实例然后调用

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

让所有IncidentProperties与获取事件的ID。 通过所有发现的属性之后,你循环,并呼吁

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

得到属性类型。

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

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

编辑:

目前我不太清楚,如果你收到性能适配器或数据阵列。如下(不幸的是我不能够马上进行测试) 所以,如果你遇到问题,你可能要取每一个IncidentProperty:

$_incidentPropertyAdapter->find($incident_property_id) 

后,才能调用

->findParentRow(...) 
+0

好吧,我已经明白了, 但我还有一个问题是有点偏离主题 - 未使用这些findDependentRowset()和findParentRow()函数 生成两个更多的选择查询到数据库,这可能比只使用一个JOIN查询? – Xeus