Jump to content

Table Associations


Recommended Posts

Hi,

 

I 'm creating a module which has 2 tables A and B linked together with one2many relationship. I'm using Prestashop 1.6

With the first and main table, I'm able to add, delete and list the list of elements in backoffice, great.

 

Now I would like to add the association on a field. So from what I read in documentation, I add the association array in the  Model object:

    public static $definition = array(
        'table' => 'lino_checklist_el',
        'multishop' => false,
	'multilang' => true,
        'primary' => 'id_element',
        'fields' => array(
            'genre' =>           array('type' => self::TYPE_STRING,'lang' => false, 'size' => 1),
            'essential' =>       array('type' => self::TYPE_INT,'lang' => false),
            'order' =>           array('type' => self::TYPE_INT,'lang' => false),
            'active' =>          array('type' => self::TYPE_INT,'lang' => false),
            // Lang fields
            'name' =>            array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
            'link' =>     array('type' => self::TYPE_STRING, 'lang' => true,  'size' => 200),
          ),
          'associations' => array(
              'season' => array('type' => self::HAS_MANY, 'field' => 'id_season', 'object' => 'LinoSeasonChecklist', 'association' => 'lino_checklist_el_seas'),
          ),
        ),
    );

Then I add the field season in renderForm method of my Admincontroller :

              array(
                'type' => 'select',
                'label' => $this->l('Saison:'),
                'name' => 'season[]',
                'required' => true,
                'multiple' => true,
                'options' => array(
                  'query' => $seasonList,
                  'id' => 'id_option',
                  'name' => 'name'
                ),
              ),

When adding an element, I can see in the generated form the list of possible season, great.

 

But I save, I've a green message saying it saved, but nothing is saved in the association table lino_checklist_el_seas.

 

I didn't see in detail how to implement associations in Prrestashop documentation.

 

Thank you

Edited by pascal-ld (see edit history)
Link to comment
Share on other sites

  • 2 years later...

Hi,

yes, I just override the add method in the model object to insert manually. Then it worked. In the form I let as explained. Hope it helps you.

 

<?php
class LinochecklistElement extends ObjectModel
{
    /** @var string Name */
    public $id_element;
    public $genre;
    public $essential;
    public $order;
    public $link;
    public $active;
    public $name;
    public $seasons;
    public $ages;
    public $withWho;
    public $what;
    public $where;
    public $location;

    /**
     * @see ObjectModel::$definition
     */
    public static $definition = array(
        'table' => 'lino_checklist_el',
        'multishop' => false,
		    'multilang' => TRUE,
        'primary' => 'id_element',
        'fields' => array(
            'genre' =>           array('type' => self::TYPE_STRING,'lang' => false, 'size' => 1),
            'essential' =>       array('type' => self::TYPE_INT,'lang' => false),
            'order' =>           array('type' => self::TYPE_INT,'lang' => false),
            'active' =>          array('type' => self::TYPE_INT,'lang' => false),
            // Lang fields
            'name' =>            array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
            'link' =>     array('type' => self::TYPE_STRING, 'lang' => true,  'size' => 200),
        ),
    );

    public function add($autodate = true, $null_values = false)
    {
      // Main insert
      $res = parent::add($autodate, $null_values);

      // Age
      if (sizeof($this->ages) > 0) {
        $query = 'INSERT INTO `' . _DB_PREFIX_ . 'lino_checklist_el_age` (`id_element`, `id_age`) VALUES ';
        $sep = "";
        foreach ($this->ages as $age) {
          $query .= $sep . '(' . $this->id . ', ' . $age . ')';
          $sep = ", ";
        }
        $res &= Db::getInstance()->execute($query);
      }

      // Location
      if (sizeof($this->location) > 0) {
        $query = 'INSERT INTO `' . _DB_PREFIX_ . 'lino_checklist_el_loc` (`id_element`, `id_location`) VALUES ';
        $sep = "";
        foreach ($this->location as $el) {
          $query .= $sep . '(' . $this->id . ', ' . $el . ')';
          $sep = ", ";
        }
        $res &= Db::getInstance()->execute($query);
      }
      
      // season
      if (sizeof($this->season) > 0) {
        $query = 'INSERT INTO `' . _DB_PREFIX_ . 'lino_checklist_el_seas` (`id_element`, `id_season`) VALUES ';
        $sep = "";
        foreach ($this->seasons as $el) {
          $query .= $sep . '(' . $this->id . ', ' . $el . ')';
          $sep = ", ";
        }
        $res &= Db::getInstance()->execute($query);
      }      
[...]

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...