Forum
13:32
18/04/2011
Hello,
While evaluating the possibilities of jqGrid, I have some difficulties for inserting a new row in a table which one column was obtained by a JOIN.
Here is my sample source code:
ini_set("display_errors","1");
require_once 'common.php';
require_once 'jq-config.php';
// include the jqGrid Class
require_once "php/jqGrid.php";
// include the PDO driver class
require_once "php/jqGridPdo.php";
// Connection to the server
$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
// Tell the db that we use utf-8
$conn->query("SET NAMES utf8");
// Create the jqGrid instance
$grid = new jqGridRender($conn);
$grid->debug = true;
// Show the server errors at client
$grid->showError = true;
// Write the SQL Query
$grid->SelectCommand = 'SELECT component.id, component.type, user.name FROM component,user WHERE component.provider_id = user.id';
// set the ouput format to json
$grid->dataType = 'json';
// Set the table to where you add the data
$grid->table = 'component';
// Set the url from where we obtain the data
$grid->setUrl('question.php');
$grid->cacheCount = true;
// Set grid caption using the option caption
$grid->setGridOptions(array(
"caption"=>"join",
"rowNum"=>10,
"sortname"=>"id",
"hoverrows"=>true,
"rowList"=>array(10,20,50),
'shrinkToFit' => true,
'width' => 700,
));
// Change the col model
$model = array(array(NAME => 'id',
LABEL => 'ID',
WIDTH => 60,
EDITABLE => false),
array(NAME => 'type',
LABEL => 'Article'),
array(NAME => 'name',
LABEL => 'Fournisseur',
WIDTH => 160));
$grid->setColModel($model);
// The primary key should be entered
$grid->setColProperty('id', array("editrules" => array("required" => true)));
// Enable navigator
$grid->navigator = true;
// Use a dropdown for picking a component type
$grid->setSelect('type',
'SELECT id, type FROM component GROUP BY type',
false); // do not connect the key value with 'type'
// Use a dropdown for picking a user name
$grid->setSelect('name',
'SELECT id, name FROM user GROUP BY name',
false); // do not connect the key value with 'name'
// Enable only adding
$grid->setNavOptions('navigator',
array("excel" => false,
"add" => true,
"edit" => true,
"del" => true,
"view" => false,
"search" => false));
// Close the dialog after the record is added
$grid->setNavOptions('add',
array("closeAfterAdd" => true,
"reloadAfterSubmit" => true)
);
// Enjoy
$grid->renderGrid('#question','#question_pager',true, null, null, true,true);
$conn = null;
?>
So, to recap, I have a grid that shows a component.id, a component.type and a user.name, and I want the user to input a component type and a user name, resulting in an INSERT query of a component.type and component.provider_id into the 'component' table.
I don't know how to have jqGrid make a link between the user.name and the component.provider_id.
I found no hint from the examples.
The error I see on the insert dialog box is the following:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mydb`.`component`, CONSTRAINT `component_ibfk_1` FOREIGN KEY (`provider_id`) REFERENCES `user` (`id`))
and the jqGrid.log contains:
(
[0] => Array
(
[time] => 2011-04-18 12:23:27
[query] => SELECT * FROM component WHERE 1=2
[data] =>
[types] =>
[fields] =>
[primary] =>
[input] =>
)
[1] => Array
(
[time] => 2011-04-18 12:23:27
[query] => INSERT INTO component (type) VALUES( ?)
[data] => Array
(
[0] => 2
)
[types] => Array
(
[0] => string
)
[fields] => Array
(
[id] => Array
(
[type] => int
)
[provider_id] => Array
(
[type] => int
)
[type] => Array
(
[type] => string
)
[size_gender_id] => Array
(
[type] => int
)
[color_id] => Array
(
[type] => int
)
[quantity] => Array
(
[type] => int
)
)
[primary] => id
[input] => Array
(
[type] => 2
[name] => 11
[oper] => add
)
)
)
I hope this is clear
Can someone help me please?
Thanks in advance,
Most Users Ever Online: 715
Currently Online:
40 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
OlegK: 1255
markw65: 179
kobruleht: 144
phicarre: 132
YamilBracho: 124
Renso: 118
Member Stats:
Guest Posters: 447
Members: 11373
Moderators: 2
Admins: 1
Forum Stats:
Groups: 1
Forums: 8
Topics: 10592
Posts: 31289
Newest Members:
, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66