CodeIgniter-MY_Model
CodeIgniter-MY_Model copied to clipboard
Relationships Neasted
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
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.
Same Result:
$aplicaciones2 = $this->aplicaciones ->where('id_empresa',$id_empresa) ->with_tests(array('fields'=>'nombre, descripcion','with' => array('relation' =>'reactivos','fields'=>'pregunta'))) ->get_all();
can I see how you defined the models? the code, preferably... and also how the tables look...
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.
Iim sorry i not included this table
Same issue here. i can not access under pivot table... I attachaed all information for testing.
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();
This query prevents possible sql injection attacks ?