Multiple fields relationated with one table (CakePHP3) - associations

I have a table named "communications" and it has two fields: "ethernet_id" and "gprs_id", these two fields are foreign keys to another table named "communication_hosts".
My problem is that when I make the association with:
$this->belongsTo('CommunicationHosts')
->setForeignKey('gprs_id')
->setProperty('gprs'); //I have the "communications.gprs" in the form
$this->belongsTo('CommunicationHosts')
->setForeignKey('ethernet_id')
->setProperty('ethernet'); //I have the "communications.gprs" in the form
It only creates the Ethernet object and doesn't create the GPRS object, but it creates a gprs simple array.
Is there any way to do this association in CakePHP3?

Related

Realm - Creating and Updating Objects With other Keys apart from primary key

I'm using Mongodb Realm.
I know it is possible to 'createOrUpdate' in realm by using the primary key i.e If the primary key doesn't exists create a new object, If it does update the object.
Something like
realm.write(() => {
// Create a book object
realm.create('Book', {id: 1, date: '12-12-2020', price: 35});
// It will update the price but won't create a new object since the id is the same
realm.create('Book', {id: 1, date: '12-12-2020', price: 55}, 'modified');
});
The realm docs says that
If your model class includes a primary key, you can have Realm
intelligently update or add objects based off of their primary key
values. This is done by passing true as the third argument to the
create method:
this can be found here https://docs.mongodb.com/realm-legacy/docs/javascript/latest/#creating-and-updating-objects-with-primary-keys
NOW, I want to update the Object based on a different field(key) apart from the primary key and On this case it is the date field This is to say that, If the date doesn't exist, create a new object/entry but it it does, just update the price.
How do I do this with realm?

How to query a property bag entity type in EFCore5?

In EFCore5, implicit tables are saved as Dictionary<TKey, object> sets, knows as Property Bag Entity Types. However, I cannot figure out how to create a LINQ query with a Where() clause that compiles for MySQL for such a property bag entity type.
For example, this code successfully retrieves the IQueryable reference to an intermediate table (generated by EFcore5's implicity many-to-many feature) given the ISkipNavigation:
ISkipNavigation nav = // some many-to-many relationship
IQueryable<Dictionary<string, object>> intermediateTable =
context.Set<Dictionary<string, object>>(nav.JoinEntityType.Name);
And this code successfully retrieves all the entries in the intermediate table:
List<Dictionary<string, object>> joins = await intermediateTable.ToListAsync();
In the resulting List, each Dictionary has just one key/value (representing the row).
However, I cannot figure out how to add a .Where() clause to the LINQ query which will compile:
joinTable.Where(d => d.Keys.First() == "foo").ToList();
joinTable.Where(d => d.Keys.Any(k => k == "foo")).ToList();
The error is:
Translation of member 'Keys' on entity type 'MachinePartMachineProfile (Dictionary<string, object>)' failed. This commonly occurs when the specified member is unmapped. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'
I do not wish to do client-side parsing for performance reasons (the join table will be to big).
I note that the type reported by the error is MachinePartMachineProfile (Dictionary<string, object>). Some investigation showed that these types are being generated based upon the static Model.DefaultPropertyBagType (which is a Dictionary<string, object>). But despite staring at the EFCore code base, I cannot discern how to correctly query such a default property bag type.
I am using MySQL as my database, if it is relevant.
You can index the dictionary directly, with knowledge of the column name.
Working example would be:
joinTable.Where(d => d[columnName] == "foo").ToList();
And for the sake of completeness, if you have an ISkipNavigation instance, you can infer these keys as follows:
string foreignKey = nav.ForeignKey.Properties.First().GetColumnBaseName();
string localKey = nav.Inverse.ForeignKey.Properties.First().GetColumnBaseName();

Best way to create a custom lookup

I would like to know what the best way is to create a custom lookup for a field in my table, my situation is as following:
I have Form A which has a datasource to Table A, a field on that datasource has a lookup method:
public void lookup(FormControl _formControl, str _filterStr)
{
changeCompany(companyInfo.DataArea)
{
super(_formControl, _filterStr);
}
}
The field has an EDT, which has an relation to a Table.
The table has multiple fields, 1 of them is field: GroupType (Enum), with 2 options: Suppliers and Customers.
Form A is showing all records, both with Suppliers and Customers, but i would like to filter on the records with only has the value Suppliers in Column C.
Based on the information above, what is the best way to create this custom lookup?
You can create Related field fixed relation between your tables
TableB = TableB.Id
Enum::Suppliers = TableB.GroupType
Or create a custom lookup
and set a range for field GroupType.

Ax - check if field is selected from database

Is there a method to check if a field is retrieved from the database? I created some logic which is being called from different locations. But from some locations, the table buffer was selected using a field list. I would like to only execute the query again if the field isn't retrieved. The queries to fetch the correct record are quite heavy and in most cases the field will be empty.
Common o;
o = args.caller();
if(!isFieldRetrievedFromDatabase(o.(fieldId)))
{
o = refetch(o);
}
//execute logic for o
Use o.isFieldDataRetrieved.
xRecord.isFieldDataRetrieved Method:
Checks whether the data of the given field has been retrieved
true if the data has been retrieved; otherwise, false.

Updating Cassandra Map Value through querybuilder

Cassandra support updating specific value in Collection by syntax
ALTER TABLE users ADD todo map<timestamp, text>
UPDATE users SET todo['2012-10-2 12:00'] = 'throw my precious into mount doom'
WHERE user_id = 'frodo';
http://www.datastax.com/documentation/cql/3.0/cql/cql_using/use_map_t.html
Did not see any example of using QueryBuilder to update specific row in Map. How it can be done?
I think you have several options.
1/ Build your own query based on the CQL one.
Example: Consider that you have a table named Interactions and in your schema a column of type named 'attributes'.
String update ="UPDATE demo.Interactions SET attributes=attributes + {'i':'j'} where id='ff';
SimpleStatement statement = new SimpleStatement(update);
session.execute(statement);
2/ Use Java API.
Java API is not that documented indeed.
Let's take an example
a- Create an update object using queryBuilder
Update update = QueryBuilder.update(keyspace, tableName);
b- Then populate with 'put' or 'putAll' functions. put/putAll will add/update content
update.with(QueryBuilder.putAll(key, map));
To remove a key, set the content of a key to null, like:
for (Object item : map.keySet()) {
update.with(
QueryBuilder.put(columName, item, null)
);
}
Then execute the query.
Following methods are available for different types:
LIST:
QueryBuilder.appendAll(...)
QueryBuilder.discardAll(...)
SET:
QueryBuilder.addAll(...)
QueryBuilder.removeAll(...)
MAP:
QueryBuilder.putAll(...)
QueryBuilder.put(...)
The list is not exhautive.
Have a look in QueryBuilder class if you do not find what you are looking for.
Best regards and best luck with Cassandra.

Resources