【问题标题】:Fuel PHP - ORM - Has Many and Belongs ToFuel PHP - ORM - 有很多并且属于
【发布时间】:2013-01-11 13:28:44
【问题描述】:

我遇到了关系问题(大声笑)...这是 ORM 的问题。

我有两个模型“项目”和“报价”。一个项目可以有许多优惠 - 所以优惠属于项目。但是,优惠也有一个不同的项目。

这是我的(简化的)物品模型:

class Model_Item extends \Orm\Model
{

  protected static $_belongs_to = array('user', 'offer');
  protected static $_has_many = array('offers');

  protected static $_properties = array(
    'id',
    'user_id',
  );

}

这是我的(简化的)优惠模型:

class Model_Offer extends \Orm\Model
{
protected static $_belongs_to = array('item');
protected static $_has_one = array('item');

protected static $_properties = array(
  'id',
  'item_id',
  'owneditem_id', // <- THIS IS THE ITEM IT OWNS
  );
}

如您所见,我需要能够在优惠模型中保存“拥有”项目,以及它“拥有”的项目,但我无法重新声明 item_id,因为它已被提供。如何告诉 Fuel 和 ORM ownitem_id 是一个项目对象?

【问题讨论】:

    标签: php orm fuelphp


    【解决方案1】:

    我终于明白了!

    报价关系:

    protected static $_has_one = array(
            //(offered item)
            'offereditem' => array(
                'key_from' => 'offereditem_id',
                'model_to' => 'Model_Item',
          'key_to' => 'id',
          'cascade_save' => true,
          'cascade_delete' => false,
            )
        );
    

    与物品模型关系:

    受保护的静态 $_belongs_to = array('user', 'item'); protected static $_has_many = array('offers');

    有迁移:

    //Add OfferedItem to Offers Migration
    oil generate migration add_offereditem_to_offers offereditem:int
    
    //Rename OfferedItem to OfferedItem_ID in Offers Migration
    oil generate migration rename_field_offereditem_to_offereditem_id_in_offers
    

    【讨论】:

      【解决方案2】:

      在fuel/app/migration 中修改你的create_Offers.php

      public function up()
      {
          \DBUtil::create_table('offers', array(
              'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' 
          ), array('id'),false, 'InnoDB', 'utf8_general_ci',
      array(
          array(
              'constraint' => 'offers_items',
              'key' => 'offer_id',
              'reference' => array(
                  'table' => 'items',
                  'column' => 'id',
              ),
              'on_update' => 'CASCADE',
              'on_delete' => 'RESTRICT'
          ))););
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-28
        • 1970-01-01
        • 2018-01-08
        • 1970-01-01
        • 1970-01-01
        • 2011-09-26
        相关资源
        最近更新 更多