هل أفعل فئات فرعية من العقيدة بشكل صحيح؟لماذا الخطأ؟

StackOverflow https://stackoverflow.com/questions/1228308

  •  22-07-2019
  •  | 
  •  

سؤال

لقد بدأت في بناء نظام نقاط البيع في 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
هل كانت مفيدة؟

المحلول

أضف $this->hasColumn('type','int');فوق مكالمة الفئة الفرعية الخاصة بك.يجب عليك الإعلان عن العمود أولاً قبل استخدامه للفئة الفرعية.

أيضًا، في استدعاءات setTableDefinition للفئة الفرعية الخاصة بك، قم بإضافة والد::setTableDefinition();بيان في الأعلى.افعل نفس الشيء أيضًا مع أساليب setUp().قد يحل مشكلتك وقد لا يحلها، لكن ذلك قد يسبب مشاكل في المستقبل.بالنسبة لما تشير إليه، عندما يقوم Doctrine بترطيب مجموعات العلاقات، في المرة الأخيرة التي استخدمت فيها وراثة تجميع الأعمدة، فعل نفس الشيء بالنسبة لي... قد لا يكون مدعومًا ببساطة إلا إذا كنت تستعلم مباشرة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top