How to insert multiple records with submitting only one time? - flask-admin

The structure of table: id, article, tag and, I defined a form with wtforms:
def scaffold_form(self):
class MyForm(Forms):
article = StringField('name')
tags = Select2TagsField('tags') # tag1, tag2
return MyForm
Here, I want insert multiple records (like [name, tag1], [name, tag2]) with submitting only one time, how can I implement it?

Related

How Do I Generate RowId For Intermediate Group Rows?

I am working on implementing grouping w/ the Server Side Row Model. I need to generate an appropriate ID for the intermediate group rows. For example, if I group by Status then I would have intermediate rows representing each Status (NEW, IN PROGRESS, COMPLETE, etc). I need to come up with a unique ID for these rows (but preferable something deterministic if they need to be accessed/updated later).
The getRowId function is passed an object that contains things like the row's data, the previous parent group values, a reference to the api, etc.
What I would ideally like to know is the current list of group fields... I have all of the values readily accessible, but I don't know what field the current row is being grouped by - else I could just go grab that field from the row's data to use as part of the row id...
Is there any good way to acquire this information?
The columnApi exposes the 'getRowGroupColumns' function from which the field property can be deduced:
getRowId: ({ columnApi, data, level, parentKeys = [] }) => {
const groupColumns = columnApi.getRowGroupColumns();
if (groupColumns.length > level) {
const field = groupColumns[level].getColDef().field;
return [...parentKeys, data[field]].join('-');
}
return [...parentKeys, data.athlete, data.year];
},

Nested Hasura GraphQL Upsert mutation is there a way to stop nesting on conflict?

I use Hasura and I have a social-network like situation.
In which I have a "User" object and a "Feed" object.
Every user has a feed.
I have a relationship from user.id to feed.id.
The relevant mutation is UpsertUserDetails as follows:
mutation UserDetailsUpsert(
$email: String!
$picture: String
) {
insert_users_one(
object: {
email: $email
feed: { data: {} }
picture: $picture
}
on_conflict: { constraint: users_tid_email_key, update_columns: [picture] }
) {
id
}
}
So when I create a new user it also creates a feed for it.
But when I only update user details I don't want it to create a new feed.
I would like to stop the upsert from going through to relationships instead of the above default behavior.
and according to this manual I don't see if its even possible: https://hasura.io/docs/latest/graphql/core/databases/postgres/mutations/upsert.html#upsert-in-nested-mutations
To allow upserting in nested cases, set update_columns: []. By doing this, in case of a conflict, the conflicted column/s will be updated with the new value (which is the same values as they had before and hence will effectively leave them unchanged) and will allow the upsert to go through.
Thanks!
I'd recommend that you design your schema such that bad data cannot be entered in the first place. You can put partial unique indices on the feed table in order to prevent duplicate feeds from ever being created. Since you have both users and groups you can implement it with 2 partial indices.
CREATE UNIQUE INDEX unique_feed_per_user ON feed (user_id)
WHERE user_id IS NOT NULL;
CREATE UNIQUE INDEX unique_feed_per_group ON feed (group_id)
WHERE group_id IS NOT NULL;

Trigger on Notes object is not firing

trigger LMDofNotes on Note (after insert, after update) {
Id accountId;
Date LMDofNote;
for(Note att: Trigger.new){
accountId = att.ParentId;
LMDofNote= (Date)att.LastModifiedDate;
}
Account acc = [Select Id,LMD_of_Notes__c from Account where Id=:accountId LIMIT 1];
acc.LMD_of_Notes__c = LMDofNote;
update acc;
system.debug('updated date'+LMDofNote);
}
Orgs with Enhanced Notes active do not create Note records, which represents classic Notes.
Instead, they create ContentNote records, which are part of a much more complex ERD. ContentNote doesn't have a ParentId field (its relationship to records is many-to-many), so your trigger would have to be on the ContentDocumentLink object.
ContentNote is basically a facade on the underlying ContentDocument and ContentVersion objects, but you can identify the note records by the FileType field:
All notes have a file type of SNOTE.
Be aware that ContentDocumentLink is used for linking all Content records (not just notes) to sObjects, and that there are unique restrictions on querying it that are described in the documentation linked above.

Querying Laravel Relationship

I am trying to get one query work since morning and not able to get it working I have two tables photographers and reviews please have a look at structure and then I will ask the question at the bottom :
Reviews table :
id int(10) unsigned -> primary key
review text
user_id int(10) unsigned foreign key to users table
user_name varchar(64)
photographer_id int(10) unsigned foreign key to photographers table
Photographers table :
id int(10) unsigned -> primary key
name text
brand text
description text
photo text
logo text
featured varchar(255)
Photographers model :
class Photographer extends Model
{
public function reviews()
{
return $this->hasMany('\App\Review');
}
}
Reviews Model :
class Review extends Model
{
public function photographers()
{
return $this->belongsTo('\App\Photographer');
}
}
My logic to query the records
$response = Photographer::with(['reviews' => function($q)
{
$q->selectRaw('max(id) as id, review, user_id, user_name, photographer_id');
}])
->where('featured', '=', 'Yes')
->get();
The question is : I want to fetch all the photographers who have at least one review in the review table, also I want to fetch only one review which is the most latest, I may have more than one review for a photographer but I want only one.
I would add another relationship method to your Photogrpaher class:
public function latestReview()
{
return $this->hasOne('App\Review')->latest();
}
Then you can call:
Photographer::has('latestReview')->with('latestReview')->get();
Notes:
The latest() method on the query builder is a shortcut for orderBy('created_at', 'desc'). You can override the column it uses by passing an argument - ->latest('updated_at')
The with method loads in the latest review.
The has method only queries photographers that have at least one item of the specified relationship
Have a look at Has Queries in Eloquent. If you want to customise the has query further, the whereHas method would be very useful
If you're interested
You can add query methods to the result of a relationship method. The relationship objects have a query builder object that they pass any methods that do not exist on themselves to, so you can use the relationships as a query builder for that relationship.
The advantage of adding query scopes / parameters within a relationship method on an Eloquent ORM model is that they are :
cacheable (see dynamic properties)
eager/lazy-loadable
has-queryable
What you need is best accomplished by a scoped query on your reviews relation.
Add this to your Review model:
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\Model;
class Review extends Model {
public function scopeLatest(Builder $query) {
// note: you can use the timestamp date for the last edited review,
// or use "id" instead. Both should work, but have different uses.
return $query->orderBy("updated_at", "desc")->first();
}
}
Then just query as such:
$photographers = Photographer::has("reviews");
foreach ($photographers as $photographer) {
var_dump($photographer->reviews()->latest());
}

Order Zend_Db_Table rowset by reference column

i know i can define relationships through _referenceMap, i know that i con join selects trough
$db->select()
But what i need is to fetch rowset in model extending Zend_Db_Table_Abstract and then order it by value of referenced column from another table.
Is there some workaround to do that?
edit:
heres is the example:
first table:
table bugs columns id, bugname, authorid
second table:
table authors columns id, authorname
I have a model Model_Bugs extends Zend_Db_Table_Abstract
I want to make something like this:
$model->fetchAll($model->select()->order('authorname ASC'))
This means, that i need to join tables and sort by a column, which is not in the model table.
thanks for help
Jan
I would add a method in Model_Bugs like so:
public function fetchBugsByAuthorname() {
$bugTable = $this;
$bugTableName = $this->info('name');
$authorsTable = new Model_Authors();
$authorsTableName = $authorsTable->info('name');
$select = $bugTable->select()
->setIntegrityCheck(false)
->from($bugTable, array('id', 'bugname', 'authorid'))
->join($authorsTableName,
"$bugTableName.authorid = $authorsTableName.id",
array("authorname"))
->order("$authorsTableName.authorname asc");
$result = $bugTable->fetchAll($select);
return $result;
}
But to do this you have to turn off ZF's table integrity checking (setIntegrityCheck(false) above), which means you won't be able to directly call save() on the resulting rows. But if it's for a read-only purpose, it will work.
If you needed to save rowsets back to the database, you may have to first select the author ID's from Model_Authors in the order you want them, and then re-order your Model_Bugs query accordingly. It's messier but it can work.