I'm new to the WIX web development platform.
I need to use the Primary Key of Table 1 to be used as a reference of Table 2, but I cannot use the ID of table 1 for this purpose.
I wonder if the best way is to "copy" this ID in the Title field (Primary Key) of this table 1. How I do that? Is this the best method?
Thank you,
Arturo
Arturo:
Have you tried doing this without using wix code?
Check out this post to see if it is possible.
Now in code the only way to add a reference field from another data collection is to use the ID. But note that ID is the field name used in the data collection for dashboard and Editor view. When accessing the ID value in code you need to use the field key which is _id.
So in your Table2 you need a column (field) that is of type reference and give it a field name like "Table1 Reference". The editor will generate a field key for you that will look like table1Reference.
Now if you have a record from Table1 that you want to link to Table2 you do something like this:
wixData.query('Table1')
.eq('title', 'uniqueTitle')
.find()
.then((results) => {
if (results.totalCount !== 1) {
throw Error('We didn't get a record from Table1');
}
// Success add this in a new record in Table2
let table1Item = results.items[0];
let table2Data = {
table1Reference:table1Item._id,
anotherTable2Field:"Some info for table2"
};
return wixData.save('Table2', table2Data)
.then((savedRecord) => {
// Successful save!
// Do something here....
});
})
.catch((error) => {
console.log(error);
});
Good luck!
Steve
Related
Based on the docs, inserting a new record
const { error } = await supabase
.from('countries')
.insert({ name: 'Denmark' })
returns
{
"status": 201,
"statusText": "Created"
}
For columns that are Is Identity, it automatically assigns a sequential unique number to the column.
How to get the ID of the newly inserted record on Supabase with JavaScript client?
You can add .select() after your insert statement in order to get the newly inserted record.
const { data, error } = await supabase
.from('countries')
.insert({ name: 'Denmark' })
.select()
You will see the inserted record inside data variable. Note that retrieving the inserted record requires you to have not only insert permission, but select permission on RLS policies.
On a side note, you can click on the buttons on the right of the docs to view different use cases. You can see this example on Create a record and return it example.
response = zoho.inventory.getRecords("Item","i inserted ID in here");
var = response.get("Product Name");
for each hey in var
{
insert into SKU #SKU is a form in Zoho Creator
[
Added_User=zoho.loginuser
Item_Name=hey #Item_Name is my field in SKU
]
}
Above code not fetching the Product Name Data from Inventory into Zoho Creator
To serarch in Inventory you must use:
<response> = zoho.inventory.getRecords(<module>, <org_id>, [<criteria_map>], [<connection>]);
The module is Items. In "Settigs" -> "Profile organization" you can obtain your org_id. It returns a map and you have to iterate over response.get("Items")
I you want to get a single item and you have the ID you can use the next function
<response> = zoho.inventory.getRecordsByID(<module>, <org_id>, <record_id>, [<connection>]);
If you want to view the reponse, you can use "info response;" or "alert response;" depend where you are using it.
Are you sure, you are using the correct link name of 'Product Name' field, try 'Product_Name' instead.
Please Used the Correct API Name for fetch out in Map.
getRecords()- used to get all records (200) at time & for Individual records try this:getRecordsByID()
response = zoho.inventory.getRecords("Item","i inserted ID in here");
Org id is missing in this get record code.
var = response.get("Product Name");
make sure you are using a correct api name of the product name
I'm using Laravel Auditing Package to trace changes on my models.
I want to get a specific column of the foreign key in it's primary table before recording the events (create, update, delete) in the audit table.
There is a way the package helps get all the attribute of the foreign key, it's called Audit Transformation but it generates an error for me when displaying the details in the table i want to know if there's any eloquent model method to get the specific column info i need instead of using getattribute() method which gets the entire row of the item_id.
Audit Transformation Method
public function transformAudit(array $data): array
{
if (Arr::has($data, 'new_values.item_id')) {
$data['old_values']['item_name'] = Item::find($this->getOriginal('item_id'));
$data['new_values']['item_name'] = Item::find($this->getAttribute('item_id'));
}
return $data;
}
This is how it's stores in the database ephasis on the item_name.
{"item_id":"1","qty_received":"2","delivered_by":"John",
"received_by":"Abi","supplier":"Stores","date":"2019-11-26","item_name":{"item_id":1,"item_name":"Toner","colour":"Black","status":"Active",
"created_at":"2018-10-25 17:55:26","updated_at":"2018-10-25 17:55:26"}}
And this is the Item table schema
So in my scenario i'd want to store the item_name as Toner not the entire row of the item_id
Any suggestion will be welcomed, thanks in advance.
Add ->item_name->item_name after the function of Find.
if (Arr::has($data, 'new_values.item_id')) {
$data['old_values']['item_name'] = Item::find($this->getOriginal('item_id'))->item_name->item_name;
$data['new_values']['item_name'] = Item::find($this->getAttribute('item_id'))->item_name->item_name;
}
When I want to delete a row from table I want to check the tables which use the id of this row. If used then it doesn't allow to delete.
You can simply check whether specific column(containing other table's key) in the row is empty or null and if it is empty then delete the row otherwise skip it.
You have two ways in which you can do it-
using eloquent get the row by primary key and check the property on collection.
write a stored procedure in the database and call it in your Laravel code.
You can use the deleting model event to check if the relationships exist. If they do, then prevent the delete.
https://laravel.com/docs/5.7/eloquent#events
If you create an observer for your model, you can use something like this:
public function deleting($model)
{
if($model->someRelation->count()) { // replace ->someRelation with whatever you want to check
return false; // prevent delete
}
if($model->anotherRelation->count()) {
return false; // prevent delete
}
}
Please tell me details process and files in which changes need to make for showing the foreign key value in place of id as shown in the snap below:
Data Gridview generated using Ajax Crud Generator
My Scenario is I have two table 1) Screen and 2) SubScreen Table, The
Table Screen has two fileds screenId and screenName
And Table SubScreen has subscreenId subscreenName and Screen_ScreenId (as a foreign key) in the view of SubScreen Table I want to show screenName (from Screen table) in place of Screen_ScreenId (of SubScreen Table View)
Finally I found a solution to show foreign key value in place of foreign key id: I am posting this solution so that others who face the same issue can use this method to implement this in their code..
In the model class a relation function need to be created in my case:
my model class is Subscreen.php this has a relation function
public function getScreenScreen()
{
return $this->hasOne(Screen::className(), ['screenId' => 'Screen_ScreenId']);
}
the above mentioned function was auto generated when generated the model using the gii module.
What extra i did here in the model is added one more function code is as shown below
public function getScreenName()
{
return $this->screenScreen->screenName;
}
-------------------------Model changes ends up here-----------------------------
Now come to the _columns.php file of the view which was auto generated while generating the CRUD code using the Ajax Crud Generator extension..
...
[
'class'=>'\kartik\grid\DataColumn',
'attribute'=>'Screen_ScreenId',
'value'=>function($model){return $model->getScreenName();},
],
...
the last line in above code 'value'=>function($model) {return $model->getScreenName();}, is added by me as a extra line to get the functionality up in the view...this made it possible for me to show the ScreenName in place of their Id in the view.
Hope this will help you to solve the same issue...