我正确地做了Doctrine子类吗?为什么错误?

时间:2009-08-04 15:49:55

标签: php mysql doctrine subclass

我开始在Doctrine中构建一个POS系统。我收到了订单,但我不知道我是否以正确的方式为Doctrine设置了子类。

以下是我为订单中的订单项提出的模型

lineItem: line_total, order_id, type
rentLineItem: returned_date_time, item_id, sold
buyLineItem: item_id

数据库看起来像那样。类型是1(租金)或2(买入)

这是lineItem类

class lineItem extends Doctrine_Record
{
  public function setTableDefinition()
  {
    $this->hasColumn('line_total','int');
    $this->hasColumn('order_id','int');

    $this->setSubclasses(array(
        'rentLineItem' => array('type' => 1),
        'buyLineItem' => array('type' => 2),
      )
    );
  }

  public function setUp()
  {
    $this->hasOne('order', array('local' => 'order_id', 'foreign' => 'id'));
  }
}

这是rentLineItem类(buyLineItem看起来类似)

class rentLineItem extends lineItem
{
  public function setTableDefinition()
  {
    $this->hasColumn('returned_date_time','datetime');
    $this->hasColumn('sold','tinyint', 2); // just in case it is sold at the end of the rental
    $this->hasColumn('item_id','int');
  }

  public function setUp()
  {
    $this->hasOne('item', array('local' => 'item_id', 'foreign' => 'id'));
}
}

这是我调用对象的代码

$q = Doctrine_Query::create()
->select('*')
->from('order')
->where('DATE(creation_date_time) = \'' . $theDate . '\'');

$orders = $q->execute();

$totalRents = 0;
$totalSales = 0;

foreach ($orders as $order) {
  foreach ($order->line_items as $lineItem) {
    if ($lineItem->type == 1) {
      $totalRents++;
    } else if ($lineItem->type == 2) {
      $totalSales++;
    }
  }
}

这是我得到的错误

Fatal error: Uncaught exception 'Doctrine_Record_UnknownPropertyException' with message 'Unknown record property / related component "type" on "lineItem"' in 
/Developer/Projects/VEL/lib/vendor/doctrine/Doctrine/Record/Filter/Standard.php:55 Stack trace: #0 
/Developer/Projects/VEL/lib/vendor/doctrine/Doctrine/Record.php(1296): Doctrine_Record_Filter_Standard->filterGet(Object(lLineItem), 'type') #1 
/Developer/Projects/VEL/lib/vendor/doctrine/Doctrine/Record.php(1255): Doctrine_Record->_get('type', true) #2 
/Developer/Projects/VEL/lib/vendor/doctrine/Doctrine/Access.php(72): Doctrine_Record->get('type') #3 
/Developer/Projects/VEL/manage/manage/dailyincomeexpensereport.php(29): Doctrine_Access->__get('type') #4 {main} thrown in 
/Developer/Projects/VEL/lib/vendor/doctrine/Doctrine/Record/Filter/Standard.php on line 55

1 个答案:

答案 0 :(得分:1)

添加$ this-> hasColumn('type','int');在您的子类调用之上。在将列用于子类化之前,必须先声明该列。

另外,在你的子类setTableDefinition调用中,添加一个parent :: setTableDefinition();在顶部的声明。也可以使用setUp()方法执行相同的操作。它可能会也可能不会解决您的问题,但这可能会在将来引发问题。至于你所指的是什么,当Doctrine给关系集合加水时,我最后一次使用列聚合继承时它对我做了同样的事情......除非你直接查询,否则它可能根本不受支持。