【问题标题】:Kohana ORM multiple relationships in models模型中的 Kohana ORM 多重关系
【发布时间】:2012-10-28 14:24:14
【问题描述】:

我是新手,正在使用 Gallery3 中的一个模块,该模块已被废弃,并希望添加一些功能。我找遍了,也没找到解决办法。

我有以下表格。

Products {id, name, description, cost, discount_id, postage_id)
Discounts {id, name, threshold, amount, maxamount)
Postage_bands {id, name, flatrate, peritem)

我添加了折扣以根据消费金额为客户提供折扣。我基于现有的 Postage 表并复制了相关页面,以便它们使用新的 Discount 功能。我希望产品的管理页面列出与每个产品相关的折扣和邮资。

折扣的管理页面运行良好。

我遇到的问题是产品与折扣和邮资之间的关系。我只能让其中一个工作。

有模型,每个模型都在自己的文件中。

class Product_Model extends ORM {
  var $rules = array(
    "name" => "length[1,32]",
    "description" => "length[0,255]");
    protected $belongs_to=array('discount');
    protected $belongs_to=array('postage_band');
}

class Postage_Band_Model extends ORM {
    var $rules = array(
    "name" => "length[1,32]");
    protected $has_many=array('products');
}

class Discount_Model extends ORM {
    var $rules = array(
    "name" => "length[1,32]");
    protected $has_many=array('products');
}

查看页面中显示折扣或邮费相关信息的代码是

<?= html::clean($product->discount->name) ?>
or
<?= html::clean($product->postage_band->name) ?>

我只能通过注释掉 Products_model 中的 Discount 或 Postage 行以及在视图页面中显示它的代码来使其中一个工作。

如何让一个表中的列关系与两个单独表中的列一起使用。在这种情况下

  • Products.discount_id 到 Discounts.id,所以我可以指向 Discounts 表中的名称字段。
  • Products.postage_id 到 Postage_bands.id,所以我可以指向 Postage_bands 表中的名称字段。

或这些表中的任何其他字段,如果我需要的话。

提前致谢

保罗

【问题讨论】:

  • 别忘了接受正确答案

标签: php orm model kohana


【解决方案1】:

您声明了两次 $belongs_to。试试:

class Product_Model extends ORM {

    protected $belongs_to=array('discount', 'postage_band');
}

另外,我认为是 $_belongs_to

【讨论】:

  • 谢谢,问题解决了。至于“$_belongs_to”与“$belongs_to”,我不知道为什么,但画廊 3 没有使用进行中的 _。在寻找答案时,我看到很多帖子都使用了_并尝试过,但根本不起作用。
猜你喜欢
  • 1970-01-01
  • 2013-01-27
  • 2013-08-22
  • 2011-03-24
  • 2011-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多