relation n to n

edited October 2015 in Modules
Hello,
I am not able to set relation n to n for my models. I know it has "has_many"' for relation one to many, but how can I manage relation n to n like grocery crud in codeigniter( example in grocery crud:$crud->set_relation_n_n('projects', 'fuel_site_projects_customers','fuel_site_customers', 'id', 'id', 'title','priority');).

Any suggestion would be appreciated.

Comments

  • edited October 2015
    You can assign a has many relationship to your models with a $has_many variable. For example, your model would look something like this:

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once(FUEL_PATH.'models/base_module_model.php'); class Projects_model extends Base_module_model { // keys are model, which can be a key value pair with the key being the module and the value being the model, module (if not specified in model parameter), relationships_model, foreign_key, candidate_key public $has_many = array( 'customers' => 'customers_model' ); function __construct() { parent::__construct('projects'); // table name } }

    Fuel automatically stores relationships in a fuel_relationships tables. Also have a look here: http://docs.getfuelcms.com/general/models#relationships
  • edited 8:45PM
    Thank you so much.
    your comment's last line answered me that FUEL will automatically save the relationship in the fuel_relationships table without needing to setup a separate lookup table.
  • edited 8:45PM
    np, good luck!
  • edited 8:45PM
    Just one question more.
    Do I have to set belongs to in Customers model or it is exactly the same as Projects model and both (Projects and Customers have has_many properties, or one of them has has_many and the other belong_to?
    Thank you so much for your help
  • edited 8:45PM
    You can use a belongs_to. Fuel itself uses this in describing the relationships between Fuel_users_model (has_many fuel_permissions_model) and Fuel_permissions_model (belongs_to fuel_users_model).
  • edited 8:45PM
    Thank you so much.
  • edited 8:45PM
    and one more and the last question:
    how to join this two table with relation n to n ?
  • edited October 2015
    Your Projects model would look something like the above, and your Customers model would look something like this:

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once(FUEL_PATH.'models/base_module_model.php'); class Customers_model extends Base_module_model { // keys are model, which can be a key value pair with the key being the module and the value being the model, module (if not specified in model parameter), relationships_model, foreign_key, candidate_key public $belongs_to = array( 'locations' => 'locations' ); function __construct() { parent::__construct('customers'); // table name } }
  • edited 8:45PM
    First of all thank you very much for all your answers and kindness.Yes Aaron, I understand that completely , but I want to have a select with join query with this two table and I do not know how to do that?I know how to write join query with two table with relation 1 to n but I do not know how to have join query with two table that has relation n to n in FUEL CMS.Could you help me please?
  • edited 8:45PM
    One thing to remember is that FUEL is based on CodeIgniter and leverages the same query building methods as CodeIgniter so those are always at your disposal like a normal CI model (e.g. $this->db->join(...)).
    https://codeigniter.com/user_guide/database/query_builder.html

    For many to many relationships, FUEL uses the table fuel_relationships as a lookup table and can be joined using CodeIgniter's query building syntax. The blog_posts_model may be an example you can lookout (in particular the _common_query() method):
    https://github.com/daylightstudio/FUEL-CMS-Blog-Module/blob/master/models/blog_posts_model.php

    Additionally, depending on what you are wanting your returned data to including, you may not need to do this because the FUEL will automatically create magic methods and properties on a particular record that has a many to many relationship. More on that here:
    http://docs.getfuelcms.com/general/models#relationships
  • edited 8:45PM
    Thanks for all you useful information.
    I would like to contribute to Persian language of fuel CMS.
    I have already done the translation, where shall I post it?
  • edited 8:45PM
    Thanks for the help! There is a separate repo for the language files that can be found at the following URL which you can make pull requests:
    https://github.com/daylightstudio/FUEL-CMS-Languages
  • edited 8:45PM
    Thank you but I do not know what to do after I press pull new request . Could you please help me?
  • edited 8:45PM
    The modules/fuel folder contains the language files for the core FUEL distribution. Those files match the files in the fuel/modules/fuel/language/ folder. I actually just pushed so please pull again.
  • edited 8:45PM
    I put my files in Github but I do not know if I put them correctly.
    Working with Github is difficult for me.
    So please check them and inform me if they are true.
    I was not able to make a folder (Persian folder), I just created the specefic file there.
Sign In or Register to comment.