0

我开始在 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
4

1 回答 1

1

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

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

于 2009-08-04T16:21:04.877 回答