I am using SlickGrid/DataView for CRUD purposes. It is working fine. However, we have some of the tables which do not have primary key.
Q1: How can I use dataview for such tables? If not then am I left with using Slickgrid without dataview only OR I have another choice? Any example would be appreciated.
Q2: Does Dataview support composite primary keys? If yes, can anybody give me an example of using it?
thanks
SlickGrid requires at least the ID field, you can take whichever that could be named differently in your table and then alias it to "ID" but it has to be a UNIQUE id field because SlickGrid is using it for row naming and so on.
If you do not have a UNIQUE field and you really want to use composite primary keys, then I suggest you create a fake ID field which you simply increment by 1 each loop. A simple way is to do this while fetching the data, something similar to the following:
$sqlTable = "SELECT * FROM myTable";
$result = mysql_query($sqlTable,$connMySQL);
$i = 1;
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$tabData[] = array(
"id" => $i++,
"column1" => $row['column1'],
"column2" => $row['column2']
);
}
Now if you do use a fake ID like I just said, this will give you some problems doing the CRUD (update, delete) since your ID is not real and point to nowhere... but there is a way to fix that too. By the way, you should split your question into 2 separate question since they are different subjects. I suggest you ask your CRUD subject in another question and that you provide some code so we can develop from there.
Related
I need to create a new table dynamically from within the code. The current proof of concept is currently working, but what I was given as POC has several issues and I have some questions about whether this is the best way. I cannot change the architecture, I can only implement as best I can.
Assume I have a controller called WorldController with accompanying Model WorldTable and accompanying views to list all Worlds and also add new Worlds. Also assume every user has their own universe, called $universe. Lastly, note that we are using Postgres and subsequently, schema.
Right now,I can add a new World to the Database in the World table and I can successfully create the required tables that the World requires, using $universe.'world_geography' syntax. So if $universe = 'milkyway', then that tables are milkyway.world_climate and milkyway.world_geography etc.
The additional tables are created in the WorldTable model by using SQL statements:
$sql = "
CREATE TABLE $universe.world_geography (
id serial4 NOT NULL,
landmasses_id int4 NOT NULL,
layers_id int4 NOT NULL,
materials_id int4 NOT NULL
);
";
$connection = ConnectionManager::get('default');
$submit = $connection->execute($sql);
This is obviously problematic due to SQL code injection. What would
be a better way to do this?
Second question: Seeing that this code is creating Tables that
aren't part of this model, should this code then rather be run from
the Controller?
And lastly, does CakePHP have any better way of doing this that I
might have missed?
Comment: I have read many, many other questions here on SO and around the net and none of them ask this question or have sufficient answers to my question.
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'.
I'm fetching a specific record with a DB table using
$myTableObj = MyTable::where(['type' => $sometype])->first();
Getting it successfully, updating some fields and saving with
$myTableObj->save();
Surprisingly, this record is updated along with another record that also has 'type' = $sometype. What can be done to prevent this?
NOTE: originally the table did not have the auto increment id field, but I have read in forums that it may make problems in Laravel so I did add it, which did not solve the problem.
Method save() working with 'id' filed only.
You can try this
$myTableObj = MyTable::where(['type' => $sometype])->update(['something' => 'value']);
Source
I understand update() is to update, but, my answer works fine and fits good for update too. Its useful where you dont want columns to be defined once again for update, (sp when they are not fillable, its tested with primary key as condition)
$myTableObj->save(); basically its for saving new record, if you want to update that row you can update like below code:
$myTableObj=new MyTable;
$myTableObj->exists=true;
$myTableObj->type=$sometype;//this is your condition, identify
$myTableObj->update();
I think what's happening here is Laravel is saving as well as updating row.
I have an application that I want to port over to Laravel.
It already has data so I would need to port and set the database data using Migration and Seeding.
In my app, I have Table foo and Table bar. A field in Table bar is a foreign key to a field in Table foo.
When I seed, the id's inserted would most likely be different from the old application.
The problem then is that the integrity of the foreign key fields would be compromised since the ids' would be different.
Is there any strategy to solve this issue? I don't mind changing the values of the foreign key fields as long as it points to the correct row.
Does not depend of the language or framework, Laravel, Rails... the logic stays same.
You have to do some smarts select on bar table to be able to link foo to bar.
So in Laravel context you open FooTableSeeder.php and write something like this:
$bar1 = Bar::where( ---your condition---)->first();
$bar2 = Bar::where( ---your condition---)->first();
DB::table('foo')->truncate();
$foogees = array(
array( 'bar_id' => $bar1->id ...),
array( 'bar_id' => $bar1->id ...),
array( 'bar_id' => $bar2->id ...),
...
);
DB::table('foo')->insert($foogees);
I had a similar situation. My solution was to manually input the foo id's. It worked well for me.
Normally, I wouldn't recommend setting the primary keys for a table, but since you're porting it, I think this is one of those situations where it would be acceptable.
I am working with a few legacy tables that have relationships, but those relationships haven't been explicitly set as primary/foreign keys. I created a .dbml file using "Linq To Sql Classes" and established the proper Case.CaseID = CaseInfo.CaseID association. My resulting class is CasesDataContext.
My Tables (One to many):
Case
------------------
CaseID (int not null)
MetaColumn1 (varchar)
MetaColumn2 (varchar)
MetaColumn3 (varchar)
...
CaseInfo
------------------
CaseInfoID (int)
CaseID (int nulls allowed)
CaseInfoMeta (varchar)
...
I'm new to LinqToSQL and am having trouble doing..
CasesDataContext db = new CasesDataContext();
var Cases = from c in db.Cases
where c.CaseInfo.CaseInfoMeta == "some value"
select c;
(Edit) My problem being that CaseInfo or CaseInfos
is not available as a member of Cases.
I heard from a colleague that I might try ADO.Net Entity Data Model to create my Data Context class, but haven't tried that yet and wanted to see if I'd be wasting my time or should I go another route. Any tips, links, help would be most appreciated.
Go back to the designer and check the relation is set up correctly. Here is one real life example, with BillStateMasters have "CustomerMasters1" property (customers for the state):
Ps. naming is being cleaned up ...
Update 1: You also need to make sure both tables have a primary defined. If the primary key isn't defined on the database (and can't be defined for whatever reason), make sure to define them in the designer. Open the column's properties, and set it as primary key. That said, entity tracking also won't work if you haven't a primary key for the entity, which for deletes means it silently doesn't updates the entity. So, make sure to review all entities and to have them all with a primary key (as I said, if it can't be on the db, then on the designer).
CasesDataContext db = new CasesDataContext();
var Cases = from c in db.Cases
join ci in db.CaseInfo on
ci.ID equals c.InfoID
where ci.CaseInfoMeta == "some value"
select new {CASE=c, INFO=ci};
my "join" linq is a bit rusty, but the above should get close to what you're after.
Is the association set to One to One or One to Many? If you have the association set to One to Many, then what you have is an EntitySet, not an EntityRef and you'll need to use a where clause on the dependent set to get the correct value. I suspect that you want a One to One relationship, which is not the default. Try changing it to One to One and see if you can construct the query.
Note: I'm just guessing because you haven't actually told us what the "trouble" actually is.
Your query looks correct and should return a query result set of Case objects.
So... what's the problem?
(Edit) My problem being that CaseInfo
is not available under Cases... i.e.
c.CaseInfo doesn't exist where I'm
assuming it would be if there were
explicit primary/foreign key
relationships.
What do you mean by "not available"? If you created the association in the designer as you say you did, then the query should generate SQL something along the lines of
SELECT [columns]
FROM Case INNER JOIN CaseInfo
ON Case.CaseID = CaseInfo.CaseID
WHERE CaseInfo.CaseInfoMeta = 'some value'
Have you debugged your linq query to get the SQL generated yet? What does it return?
Couple of things you might want to try:
Check the properties of the association. Make sure that the Parent property was created as Public. It does this by default, but something may have changed.
Since you're not getting CaseInfo on C, try typing it the other direction to see if you get ci.Case with intellisense.
Delete and recreate the association all together.
There's something very basic going wrong if the child members are not showing up. It might be best to delete the dbml and recreate the whole thing.
If all else fails, switch to NHibernate. :)
After a few tests, I'm pretty sure the FK relationships are required in the DB regardless of whatever associations are created in Linq-to-SQL. i.e. if you don't have them explicitly set in the DB, then you will have to do a join manually.
Is this c#? I think you need == instead of = on this line:
where c.CaseInfo.CaseInfoMeta = "some value"
should read
where c.CaseInfo.CaseInfoMeta == "some value"