user1780848
user1780848

Reputation: 11

Kohana ORM multiple relationships in models

I'm new to this and working with a module in Gallery3 that has been abandoned and looking to add some functionality. I've searched all over and haven't been able to find a solution.

I have the following tables.

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

I added the discounts to offer customers a discount based on amount spent. I based it on the existing Postage table and copied the associated pages so they work with the new Discount functionality. I want the admin page for the products to list which Discount and postage is associated with each product.

The admin pages for the Discounts work just fine.

What I am having problems with is the relationship between Products to Discounts and Postage. I can only get one or the other working.

There are the models, each in their own file.

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');
}

The code in the views page that displays information relating to the Discount or Postage is

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

I can only get one or the other working by commenting out wither the Discount or Postage lines from the Products_model and the code to display it in the views page.

How can I get relationships of columns in one table to work with columns in two separate tables. In this case

or any other fields in those table if I need them.

Thanks in advance

Paul

Upvotes: 1

Views: 309

Answers (1)

Niall
Niall

Reputation: 435

You are declaring $belongs_to twice. Try:

class Product_Model extends ORM {

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

Also, I think it's $_belongs_to

Upvotes: 1

Related Questions