Kendo UI Foreign Key - user-interface

I am new to Kendo UI and I got this problem. I don't understand, what are the basic steps in showing child object information in a column with a foreign key property. I mean, what do I have to do to create a foreign key column. It might be a silly question, but I saw a demo of foreign key on demos pages http://demos.telerik.com/kendo-ui/web/grid/foreignkeycolumn.html, still, I didn't quite get how does it work.
Thanks,
Vidmantas

Below is the MVC example for foreign key column demo:
Its as simple as it sounds,
For example, lets say you have CoachId field in a table and its the foreign key of the table
Coach that has CoachId and Name columns. Now you wish to present the Coach Name instead of the CoachId in the Grid.
columns.ForeignKey(student => student.CoachId,
(System.Collections.IEnumerable)ViewBag.Coaches, "CoachId", "Name")
.Title("Coach");
Then from the controller or from Code you have to send all the coach records in Viewbag or Viewdata.
Cheers!!

Related

Backpack Laravel select2_from_ajax field returns results, but I can't select any

I'm implementing a 'select2_from_ajax' field using Backpack for Laravel.
I've implemented this in other places and it works correctly. But for some reason when implementing it this time it will not let me select any of the options and doesn't show the highlight when mousing over the options. It lists out the options correctly, I just can't select any of them.
The only thing I can think of is that the relationship it's trying to reference doesn't have a primary 'id' field in the database, but I'm not sure why that would affect this.
I have implemented both the index and show routes.
The issue was that the relationship field's primary key was not 'id' it was setup with a different column name.
I reworked the data structure so the foreign key referenced was pointing to a column labeled 'id'.

Any way to intercept an error message and make more user friendly?

I have looked for an answer here among other places but havent quite been able to find what I need to know.
I have 3 tables, Order_Details, Products_Ordered and Product_Details. The first two are being used in a master detail form to show the order and the items ordered together. The Products_Ordered table has a composite primary key made from two foreign keys, the first being the primary key from the Order_Details table, and the second being the primary key from the Product_Details table. Together they ensure that a type of product can only be added to an order once. If someone wants to order more than one product then the quantity field in the record can be altered to reflect this. All that seems fine so far.
My issue is that when adding products to the order in the master detail form i have used a drop down list of values to select the product to add to the order. the display value for this is the product name and the return value for it is the primary key for the product from the Product_Details table.
I like this because its easier for the user to simply select the product and add a quantity of it to the table. And it works fine for both insert and update operations apart from one situation.
If the user selects the same product in to rows then submits the table the database then tries to add the product to the order twice, throwing a "ORA-00001: unique constraint violated." error. Obviously this is because of the product ID being used in the primary key of the table.
I don't want to allow the user to add two records to the table like that, rather id like to force them to alter the quantity field accordingly. The error message that comes up isn't very user friendly so my question is how can I detect this error and display a more user friendly one instead telling them to alter the quantity field instead?
*If this isn't possible then is there a way that I can hide any already selected products from the dropdown list of values in the following table rows? I haven't looked into this too much because surely it would get complicated when the user tries to add more rows than products available in the dropdown and there are no more products values to show?
I am quite new to this so please be nice. Any help is greatly appreciated :D
Here is a link where all is nicely described:
https://docs.oracle.com/cd/A97630_01/appdev.920/a96624/07_errs.htm
Section
Predefined PL/SQL Exceptions
in combination with:
Defining Your Own PL/SQL Exceptions
and
Defining Your Own Error Messages: Procedure RAISE_APPLICATION_ERROR
Hope it helps...

Foreign key, or no foreign key? Defining Laravel relationships

What is the difference between defining a foreign key VS just creating an integer column named user_id?
// create_posts migrations
$table->integer('user_id')->unsigned();
// vs
$table->foreign('user_id')->references('id')->on('users');
Can they be used interchangeably? What purpose do each one serve? Which is considered a best practice, first or second definition?
Edit
The command $post->user() will work either ways, so what advantages does usage of a foreign key bring?
$table->integer('user_id')->unsigned();
// Above command is creating a column in database and it is required to have the required table structure
$table->foreign('user_id')->references('id')->on('users');
// Above command is creating foreign key index and making reference to id in users table.
As you can see from command explanations they can't be interchangeable, you need first command to have second command without first second command would complain.
Best practise is to use both of them together.
Few of advantages are listed below:
You can implement cascade update/delete.
Database level validation that only valid values of user_id is recorded ( to avoid some one entering 999999 which might be invalid or non existing user_id).
Above two are main advantages and you can express multiple scenarios how above two can be life saviour.
Let's say in post table by human error or bug in script makers user_id = 9999. What you think $post->user() will do?
Unless you can have a post without any reference to user you can see there could be multiple logical issue you may find if foreign keys are not used.
Think of foreign keys as enforcing relations and taking care of post if user is removed / deleted from db.
No, They can't be used interchangeably and each one has its usage. Use index when you want to define index on column, means database index, see here. But when you define a foreign key it set a index to that column (for searching, ...) and also make a relation between user_id and id column in user table, so if there is a user by id 10, then you can use user_id = 10 on another table. Also it has another benefits such as making sure your data are integrated. For example you can't delete user by id 10 if there is article that belongs to this user. For more information see this.
The first line, will only create a user_id column not something special,
while the other line will create a column as a foreign key which will be tightly coupled with id column of users table, this will create some limitations.
read about foreign key reference
For the best practices I always use:
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
Don't mix the definition of index with foreign key, they don't mean the same.

kendo grid with foreign key without relations in database design

I have a database without the relations between tables in SQL Server, but the data are relationated.
I am trying to use a Kendo Grid with Foreign Key column, but, the example that I found is for database with correctly related tables.
How I can declare a Kendo Grid when for a Foreign Key column I said who is the Action and Controller to fill this data.
IMHO, you may do like this:
Let's say we create a foreign key column about gender
col.ForeignKey(m => m.id_gender, (System.Collections.IEnumerable)ViewData["gender"], "id_gender", "description");
In the controller, we may create a ienumerable collection then pass it back to the view using view data
IEnumerable<GenderItem> gender_list = .... <-- initiallize the ienumerable or get from database
ViewData["gender"] = gender_list;

How can I have a table without primary key in Magento?

In Magento, it is necessary that you create a primary key in order your grid and module to work. But, I don't have primary key in my table and don't want to create it. Is it possible to make my module work properly without primary key?
Magento is giving error Column not found: 1054 Unknown column 'main_table.modulename_id' in 'field list' as it doesn't find primary key id in the table while displaying grid.
Without auto-increment it can be done with:
$this->_isPkAutoIncrement = false; in the model. Is there anything like that for primary key as well?
I searched on net without any luck. Any help will be highly appreciated.
If you will not use model/collection for this table, you can do this. If not -- you cann't (without rewrites). See newsletter queue and queue_link tables -- there are no model for queue_link table (it has primary key, but you can use your table without it if you will wok in such way), all things are done in queue resource.
The Magento ORM is setup in a way which will require you specify a primary field. You should always have primary key anyway, I can't imagine a situation where you would want one as it would no longer be suited to a database...
I would imagine you may want a natural/composite key rather than a surrogate key, but I am not sure if that's what you meant?

Resources