CodeIgniter-MY_Model icon indicating copy to clipboard operation
CodeIgniter-MY_Model copied to clipboard

Relationships Neasted

Open rodyoukai opened this issue 8 years ago • 8 comments

Hi, sorry to bother you so much.

I try to do the following:

$aplicaciones2 = $ this->aplicaciones->where ('id_empresa' $ id_empresa)->with_tests (array ('with' => array( 'relation' => 'reactivos')))->get_all ();

Relationships work perfectly independently, but if I try to nest the second (reactive) don't work.

Relationships are as follows:

aplicaciones - has_many_pivot - tests

tests - has_many - reactivos

rodyoukai avatar Jun 27 '16 21:06 rodyoukai

https://github.com/avenirer/CodeIgniter-MY_Model#retrieve-data-from-nested-relationships-or-should-we-say-retrieve-nested-relationships-data

As mentioned in the tutorial, you do have to add the fields you want to retrieve from the relationship.

avenirer avatar Jun 28 '16 06:06 avenirer

Same Result:

$aplicaciones2 = $this->aplicaciones ->where('id_empresa',$id_empresa) ->with_tests(array('fields'=>'nombre, descripcion','with' => array('relation' =>'reactivos','fields'=>'pregunta'))) ->get_all();

rodyoukai avatar Jun 28 '16 16:06 rodyoukai

can I see how you defined the models? the code, preferably... and also how the tables look...

avenirer avatar Jun 28 '16 18:06 avenirer

I send you...

models.zip tablas

rodyoukai avatar Jun 28 '16 18:06 rodyoukai

Hi, Your relations in the tests and aplicaciones models seam to be wrong. Base on the tables above these relation should be has_many not has_many_pivot.

salain avatar Jul 01 '16 05:07 salain

Iim sorry i not included this table relations

rodyoukai avatar Jul 10 '16 18:07 rodyoukai

Same issue here. i can not access under pivot table... I attachaed all information for testing.

adsiz

Database

`-- -----------------------------------------------------
-- Table `groups`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `groups` (
  `id` INT NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `groups_users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `groups_users` (
  `group_id` INT NOT NULL,
  `user_id` INT NOT NULL,
  PRIMARY KEY (`group_id`, `user_id`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `users` (
  `id` INT NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `messages`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `messages` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `sender_id` INT NULL,
  `receiver_id` INT NULL,
  PRIMARY KEY (`id`, `sender_id`, `receiver_id`))
ENGINE = InnoDB;`

Users_model.php


$this->has_many['messagesInbox'] = [
            'foreign_model' => 'Messages_model',
            'foreign_table' => 'messages',
            'foreign_key'   => 'sender_id',
            'local_key'     => 'id'
        ];

$this->has_many_pivot['groups'] = [
            'foreign_model'     =>'Groups_model',
            'pivot_table'       =>'groups_users',
            'local_key'         =>'id',
            'pivot_local_key'   =>'user_id',
            'pivot_foreign_key' =>'group_id',
            'foreign_key'       =>'id',
        ];

Messages_model.php

$this->has_one['user'] = [
            'foreign_model' => 'Users_model',
            'foreign_table' => 'users',
            'foreign_key'   => 'id',
            'local_key'     => 'receiver_id'
        ];

CONTROLLER

$groups = $this->Groups->with_users([
            'fields' => 'name',
            'where' => [
                'users.deleted' => null,
                'users.id !=' => $this->user->id
            ],
            'with' => [
                'relation' => 'messagesInbox',
                'fields' => 'body',
                'where' => [
                    'messages.receiver_id' => $myId,
                    'messages.deleted' => null,
                ]
            ]
        ])->get_all();

emrahoruc avatar Aug 17 '16 22:08 emrahoruc

This query prevents possible sql injection attacks ?

felix1 avatar Nov 29 '20 09:11 felix1