Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 3/雄辩的ORM:扩展其他模型的模型_Php_Orm_Laravel_Eloquent_Laravel 3 - Fatal编程技术网

Php Laravel 3/雄辩的ORM:扩展其他模型的模型

Php Laravel 3/雄辩的ORM:扩展其他模型的模型,php,orm,laravel,eloquent,laravel-3,Php,Orm,Laravel,Eloquent,Laravel 3,我正在用Laravel3编写一个简单的应用程序,我有两个模型:Item和SpecialItem SpecialItem应该通过简单地添加额外的字段(如“颜色”、“价格”等)来“扩展”项目 我的想法是,我可以将“核心”类项保留为普通内容(例如,“优先级”或“标题”),并将其扩展为不同类型的项,每个项都有自己独特的字段集 class Item extends Eloquent { // Just a simple model with a couple relatio

我正在用Laravel3编写一个简单的应用程序,我有两个模型:Item和SpecialItem

SpecialItem应该通过简单地添加额外的字段(如“颜色”、“价格”等)来“扩展”项目

我的想法是,我可以将“核心”类项保留为普通内容(例如,“优先级”或“标题”),并将其扩展为不同类型的项,每个项都有自己独特的字段集

    class Item extends Eloquent
    {
        // Just a simple model with a couple relationship with other models, such as Page, Collection 

        public static $table = 'items';

        public function page()
        {
        return $this->belongs_to('Page');
        }

        public function collection()
        {
        return $this->belongs_to('Collection');
        }
    }

// ...

class PeculiarItem extends Item 
{
    public static $table = 'peculiar_items';
    // Ideally this PeculiarItem needn't redeclare the page_id and collection_id foreign key fields
    // because it extends Item. 

}
问题来自ORM在对我的SpecialItem对象调用save()方法时的硬连接方式

// ...

class Item_Controller extends Base_Controller
{

    /**
     * @param   $type   the class name of the object we are creating
     * @param   $data   the data for the new object
     * @return  mixed
     */
    public function action_create($type = null, $data = null)
    {
        // ... filter, validate data, etc.
        $entry = new $type($data);
        $entry->save();
    }
}

// ...
POST请求示例:item/create/specialItem

数据:页面id=1,集合id=1,标题='Foo'

此操作失败,因为SpecialItem没有字段page_id或collection_id

我怎样才能摆脱这种局面?
原则上这是一个坏主意吗?

您不能也不应该这样做,因为
特殊项是它自己的表

话虽如此,在您雄辩的模型中,您设置了与
->有一个
->有许多
->属于
方法的关系。从那里,您就可以使用
Item::find()->special\u Item()->first()->someSpecialProperty
,等等。。。(未经测试)


由于我一直在使用L4,我很难记住L3的设置-它们使用snake case。看看这个:

想象一下,如果我有15个不同的类来扩展这个项目。然后我应该创建15种不同的方法来处理Item_控制器内的对象创建吗?这没关系。能言善辩是聪明的。您可以
Item::find(123)->special\u items()->get()
——这将返回与父项相关的所有特殊项。这种关系将在您的
项目
模型、方法
特殊项目(){return$This->has_many('special_items','parent_key_name');}
中建立。在阅读了我的问题后,我可能意识到情况还不够清楚,所以这里是另一种尝试。如果您想创建一个API,允许您添加几种类型的项,但保持模型/表干燥,该怎么办?假设所有物品都有某些共同特征(优先级、标题等),但每种物品都有自己的特定特征(颜色/重量、任何可能的组合)。我的目标是有一个像“create/item”这样的API调用,它一次获取所有数据并实例化正确类型的item类。如果我们错了,我们真的必须看一个真实世界的案例。谢谢,虽然你的评论并没有提出一个解决方案,但他们实际上帮助我看到了我的方法中的错误:我将为每种“项”创建一个模型,让它们扩展一个通用的抽象类项,但数据库结构仍然是不干燥的(这意味着我必须重复那里的公共字段)。我知道这听起来有点难听,但在我的用例中,我认为这是最有意义的。