⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

No save when n-n relationship



reynierpm
  • profile picture
  • Member

Posted 02 February 2012 - 19:10 PM

Hi every:
I'm starting to use grocery CRUD, amazing by the way, and have this code:

function trabajadores() {
try {
$crud = new grocery_CRUD ();

$crud->set_theme ( 'datatables' );
$crud->set_table ( 'ntrabajadores' );
$crud->set_subject ( 'Trabajadores' );

$crud->display_as ( 'idprofesion_FK', 'Profesión' );
$crud->display_as ( 'idcargo_FK', 'Cargos' );
$crud->display_as ( 'idcategoria_FK', 'Categorías' );
$crud->display_as ( 'idempresa_FK', 'Empresa' );
$crud->display_as ( 'idnivfuncional_FK', 'Niveles Funcionales' );

$crud->display_as ( 'sueldobasico ', 'Sueldo Básico' );
$crud->display_as ( 'estadocivil ', 'Estado Civil ' );

$crud->set_relation ( 'idprofesion_FK', 'nprofesiones', 'nombre' );
$crud->set_relation ( 'idcargo_FK', 'ncargos', 'nombre' );
$crud->set_relation ( 'idnivfuncional_FK', 'nnivfuncional', 'nombre' );
$crud->set_relation ( 'idcategoria_FK', 'ncategorias', 'nombre' );
$crud->set_relation ( 'idempresa_FK', 'empresa', 'nombre' );

$crud->set_relation_n_n ( 'asignaciones', 'asignaciones_has_ntrabajadores', 'asignaciones', '', 'idasignacion', 'nombre', '' );
$crud->set_relation_n_n ( 'deducciones', 'deducciones_has_ntrabajadores', 'deducciones', '', 'iddeduccion', 'nombre', '' );

$output = $crud->render ();
$this->_example_output ( $output );
} catch ( Exception $e ) {
show_error ( $e->getMessage () . ' --- ' . $e->getTraceAsString () );
}
}

As you notice I have some 1-n and n-m relationship here but when I try to add a new "trabajadores" nothing happen and the table still empty. Why? I'm doing something wrong? Where?

Cheers and thanks in advance

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 02 February 2012 - 19:39 PM

Yes you have to add all the 6 required fields. The 7th is only if you have a priority field. If you have not you just don't add it.So in your case it will be:

$crud->set_relation_n_n ( 'asignaciones', 'asignaciones_has_ntrabajadores', 'asignaciones', 'SOMETHING_HERE', 'idasignacion', 'nombre' );
$crud->set_relation_n_n ( 'deducciones', 'deducciones_has_ntrabajadores', 'deducciones', 'SOMETHING_HERE', 'iddeduccion', 'nombre' );


You can see with the beautiful orange numbers that I designed :) , where is each field to your relation tables at: http://www.grocerycr..._a_relation_1_n

Just to mention that all the primaries keys are auto recognized.

reynierpm
  • profile picture
  • Member

Posted 02 February 2012 - 20:07 PM

Ok, my table "asignaciones_has_ntrabajadores" has only two fields: idasignacion, idtrabajador. I try this two variants:

$crud->set_relation_n_n ( 'asignaciones', 'asignaciones_has_ntrabajadores', 'asignaciones', 'idtrabajador', 'idasignacion', 'nombre' );
$crud->set_relation_n_n ( 'deducciones', 'deducciones_has_ntrabajadores', 'deducciones', 'idtrabajador', 'iddeduccion', 'nombre' );

and this other

$crud->set_relation_n_n ( 'asignaciones', 'asignaciones_has_ntrabajadores', 'asignaciones', 'idasignacion', 'idtrabajador', 'nombre' );
$crud->set_relation_n_n ( 'deducciones', 'deducciones_has_ntrabajadores', 'deducciones', 'iddeduccion', 'idtrabajador', 'nombre' );


And still not working, I'm still wrong?

reynierpm
  • profile picture
  • Member

Posted 02 February 2012 - 20:22 PM

I have another question, how I can get the n-n sample fields for n-n relationship? By default I have a select

reynierpm
  • profile picture
  • Member

Posted 02 February 2012 - 21:51 PM

Well after doing test once and once again and check my Firebug I notice this:
[quote]
"NetworkError: 500 Internal Server Error - 192.168.3.135/nominas/index.php/sistemanominas/trabajadores/insert"
JSON.parse: unexpected character
192.168.3.135...ry-1.7.1.min.js
Line 2
[/quote]
Could this be the error because doesn't save nothing?

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 02 February 2012 - 22:47 PM

I don't know perhaps it's a javascript conflict. Did you use another jquey to your project?

reynierpm
  • profile picture
  • Member

Posted 03 February 2012 - 13:08 PM

No, just the Grocery JS files :(

reynierpm
  • profile picture
  • Member

Posted 03 February 2012 - 13:45 PM

Well I check the JSON output because the error and watch this:
[quote]Error Number: 1452</p><p>Cannot add or update a child row: a foreign key constraint fails (`nominas`.`asignaciones_has_ntrabajadores`, CONSTRAINT `Ref_07` FOREIGN KEY (`idasignacion`) REFERENCES `asignaciones` (`idasignacion`) ON DELETE NO ACTION ON UPDATE NO ACTION)</p><p>INSERT INTO `asignaciones_has_ntrabajadores` (`idasignacion`, `idtrabajador`) VALUES (12, '6')[/quote]
Why the 6 is inserted as string and not as a integer or a number? This could be the problem but I not know where to look in order to fix the error. Any help?

reynierpm
  • profile picture
  • Member

Posted 03 February 2012 - 14:41 PM

Well I have another error, see the result when I execute the method "trabajadores":
[quote]
Error Number: 1052
Column 'idasignacion' in where clause is ambiguous
SELECT * FROM (`asignaciones_has_ntrabajadores`) JOIN `asignaciones` ON `asignaciones_has_ntrabajadores`.`idtrabajador` = `asignaciones`.`idasignacion` WHERE `idasignacion` = '12' ORDER BY `asignaciones`.`nombre`
Filename: /var/www/html/nominas/models/grocery_model.php
Line Number: 175
[/quote]
I think this isn't correct because I not need asignaciones I need trabajadores but getting data from asignaciones, I'm doing something wrong?

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 03 February 2012 - 19:06 PM

OK, actually I don't understand the relations. Can you please post the tables and the relations of each tables? Just to understand. All the fields and tables are in different language than English and I really can't understand anything.

reynierpm
  • profile picture
  • Member

Posted 03 February 2012 - 20:29 PM

Ok I've attached a JPG file with tables and relations between them

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 03 February 2012 - 22:31 PM

Ok this is absolutely right:


$crud->set_relation_n_n ( 'asignaciones', 'asignaciones_has_ntrabajadores', 'asignaciones', 'idtrabajador', 'idasignacion', 'nombre' );
$crud->set_relation_n_n ( 'deducciones', 'deducciones_has_ntrabajadores', 'deducciones', 'idtrabajador', 'iddeduccion', 'nombre' );


So when you add those code you have the multiple relation input right? So you have probably have wrong to the relations. Perhaps a key is been added two times. I don't know what to say. Perhaps another person has an idea of what this would be. I don't know what it's wrong with this. Sorry...

franxisco
  • profile picture
  • Member

Posted 28 March 2012 - 19:39 PM

[b]set_relation_n_n[/b] don`t work on the server.

I am using the example of films, if it works on another server or local server.

What PHP settings may be giving this conflict?

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 29 March 2012 - 06:31 AM

If it's the set_relation_n_n it is probably the suhosin that causes the problem. See /topic/206-why-would-i-get-an-error-has-been-occured-on-saving/page__view__findpost__p__780

If it's not can you send us the firebug error to see what's happens?

Adrian
  • profile picture
  • Member

Posted 19 February 2018 - 15:27 PM

Maybe its caused by different id types on database like integer related to mediuminteger ?

If it's the set_relation_n_n it is probably the suhosin that causes the problem. See /topic/206-why-would-i-get-an-error-has-been-occured-on-saving/page__view__findpost__p__780

If it's not can you send us the firebug error to see what's happens?