Jump to content

ObjectModel with ForeignKey


carlosgoce

Recommended Posts

Hello,

 

My prestashop version is 1.5

 

I am trying to create a CRUD with a ObjectModel which contains a ForeignKey but I am not able to do it. 

 

This is my table (without other non related fields:

 

MyTable

id_my_table

name

id_employee (FK not null)

id_another_table (FK but nullable)

 

 

So, I need to set id_employee.

 

 

This is my ObjectModel:

 

class MyTableModel extends ObjectModel
{
    public $id_my_table;
    public $id_employee;
    public $id_another_table;
    public $name;


    public static $definition = array(
        'table'     => 'my_table',
        'primary'   => 'id_my_table',
        'multilang' => false,
        'fields'    => array(
            'name'  => array('type' => self::TYPE_STRING, 'required' => true, 'validate' => 'isString'),
            'id_another_table' => array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),
            'id_employee' => array('type' => self::TYPE_INT, required => 'true', 'validate' => 'isUnsignedId', 'copy_post' => false),  
        ),
    );


}

And finally, the controller:

 

<?php


include_once _PS_MODULE_DIR_ . 'my_module/models/MyTableModel.php';




class ModuleMyTableController extends ModuleAdminController
{
    function __construct()
    {
        $this->table = 'my_table';
        $this->className = 'MyTableModel';


        $this->fields_list = array(
            'name' => array('title' => $this->l('name')),
        );


        $this->context = Context::getContext();
        $this->context->controller = $this;
        $this->fields_form = array(
            'legend' => array(
                'name' => $this->l('my legend'),
                'image' => '../img/admin/group.gif',
            ),
            'input' => array(
                array('type' => 'text', 'label' => $this->l('Name'), 'name' => 'name', 'required' => true),
            ),
            'submit' => array('title' => $this->l('Save'))
        );


        parent::__construct();


        $this->addRowAction('view');
        $this->addRowAction('edit');
        $this->addRowAction('delete');
    }


   /**
    * Set employee and promoter group ids
    */
    protected function beforeAdd($object)
    {
        $object->id_employee = $this->context->employee->id;
        $object->id_another_table = $this->context->employee->id_another_table;
    }


}

The validation fails because id_employee is not set! I thought that adding the ids on the beforeAdd method should be enough. If I remove validation I got the database error that the id_employee fails because it was null. 

 

$object->id_employee has the ID 22 which exist.

 

I tried several things but could manage to get it working. What is the best way to add a foreign key?

 

 

UPDATE

I think that the problem is another. I am not setting any id_another_table because is nullable, but I think that prestashop is trying to save a 0 instead of null, so the entity does not exist and the insert fails. It is adding a 0 even when I put the datatype as TYPE_NOTHING on the fields array to NOTHING. If I remove the field from the model it works.

Edited by carlosgoce (see edit history)
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...