Laravel's Eloquent' with'数组正在破碎

时间:2015-12-07 19:31:08

标签: php laravel eloquent polymorphism

我正在尝试使用单个基类来抽象我的模型。我有三个继承自同一基础的模型:

  1. 修复
  2. 检验
  3. 购买
  4. 我能够成功地将模型创建并保存到数据库,但是在获取时我得到一个空白屏幕,不会抛出任何错误。当我删除$with属性时,一切似乎都有效。

    下面是代码:

    abstract class ItemType extends Model 
    {
        public    $timestamps = false;
        protected $with       = ['details'];
    
        public function details()
        {
             return $this->morphOne(Item::class, 'type', 'item_details_type', 'item_details_id', 'id');
        }
    }
    
    class Repair extends ItemType
    {
        protected $table      = 'repairs';
        protected $guarded    = ['id', 'created_at', 'updated_at'];
        protected $morphClass = self::class;
    }
    class Inspection extends ItemType {}
    class Purchase   extends ItemType {}
    

1 个答案:

答案 0 :(得分:1)

最后,我决定使用if segue.identifier == "newRecord" { let controller = (segue.destinationViewController as! NewRecordVC) let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate // instantiate the delegate methods in AppDelegate controller.managedContext = appDelegate!.managedObjectContext // create context from delegate methods let recordEntity = NSEntityDescription.entityForName("RecordData", inManagedObjectContext: controller.managedContext) let locationEntity = NSEntityDescription.entityForName("Location", inManagedObjectContext: controller.managedContext) controller.location = Location(entity: locationEntity!, insertIntoManagedObjectContext: controller.managedContext) controller.record = Record(entity: recordEntity!, insertIntoManagedObjectContext: controller.managedContext) print("segueing") } 关系而不使用抽象类来解决问题。