Api platform add or update - api-platform.com

Good afternoon I am adding data in via POST method in PLATFORM API can I make this method work like adding or updating data.
So that when the data is already there for the object, it will simply update the pinOrder field.
My input:
{
"chat": "/api/chats/01FVKRYXMMTHKJ2EZB02F4FZ3Z",
"pinOrder": 3
}

Insert or update (upsert) is not available in Api Platform. However, you can achieve this behavior with a custom (or decorated) Data Persister.
https://api-platform.com/docs/core/data-persisters/
In the persist method of the data persister you could manually check if an item matching your criteria does already exist and, if yes, update this one instead of persisting a new one.

You can use PRE_WRITE event. For exemple, an order item quantity.
public static function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => [
'updateExistingItemQuantity', EventPriorities::PRE_WRITE,
],
];
}
public function updateExistingItemQuantity(
ViewEvent $event
): void {
$item = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$item instanceof MyItemObject || Request::METHOD_POST !== $method) {
return;
}
// find duplicateItem
if ($duplicateItem) {
$duplicateItem->setQuantity("UPDATED QUANTITY");
// save $duplicateItem
$event->setControllerResult($duplicateItem);
}
}

Related

How to always filter a collection on specific field value in api-platform GET operations?

In the GET operations, I'd like to exclude from the returning collections my entities that have an " archive" field that equals " true ".
I'd like that to be the default for my endpoints like /users or /companies and i want to avoid to add an URL filter by hand like /users?filter[archive]=true
what would be the best way to do that ?
Thanks for any help :)
I had to do something like that, and I solved it by applying a DoctrineExtension to a Collection that will add the WHERE clause to the QueryBuilder.
How?
Define your Filter, I see you already have that.
Add the Doctrine Extension to by applied only to Collections, or if you need, also to an Item: https://api-platform.com/docs/core/extensions/#custom-doctrine-orm-extension
services:
App\Doctrine\Extension\ArchivedExtension:
tags:
- { name: api_platform.doctrine.orm.query_extension.collection }
Code your Extension.
You could check if you are receiving a certain "filter" (your "filter[archive]=true" query parameter): if YES, dont apply the condition to the QueryBuilder, your Filter will be applied by the filtering mechanism of ApiPlatform.
Your extension class should looked something like this:
class ArchivedExtension implements QueryCollectionExtensionInterface
{
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = [])
{
$this->addWhere($queryBuilder, $resourceClass, $context);
}
private function addWhere(QueryBuilder $queryBuilder, string $resourceClass, array $context = [])
{
if (MyResource::class !== $resourceClass) {
return;
}
// Search if a "archive" Filter is being requested, if not, apply a restriction to the QueryBuilder
if (array_key_exists('archive', $context['filters'])) {
return;
}
$rootAlias = $queryBuilder->getRootAliases()[0];
$queryBuilder->andWhere(sprintf('%s.archive = :archive', $rootAlias));
$queryBuilder->setParameter('archive', true);
}
}

Laravel 5.7 many to many sync() not working

I have a intermediary table in which I want to save sbj_type_id and difficulty_level_id so I have setup this:
$difficulty_level = DifficultyLevel::find(5);
if ($difficulty_level->sbj_types()->sync($request->hard, false)) {
dd('ok');
}
else {
dd('not ok');
}
Here is my DifficultyLevel.php:
public function sbj_types() {
return $this->belongsToMany('App\SbjType');
}
and here is my SbjType.php:
public function difficulty_levels() {
return $this->hasMany('App\DifficultyLevel');
}
In the above code I have dd('ok') it's returning ok but the database table is empty.
Try to change
return $this->hasMany('App\DifficultyLevel');
to
return $this->belongsToMany('App\DifficultyLevel');
The sync() method takes an array with the id's of the records you want to sync as argument to which you can optionally add intermediate table values. While sync($request->hard, false) doesn't seem to throw an exception in your case, I don't see how this would work.
Try for example:
$difficulty_level->sbj_types()->sync([1,2,3]);
where 1,2,3 are the id's of the sbj_types.
You can read more about syncing here.

How to implement required File upload attribute, on Update action, in Yii 2?

I need file upload field to be required for both Create and Update actions, and the required validation and validation of types to be performed in both cases.
This is how my form looks like (Note: It's a form, not an Active Record model):
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\base\Object;
use yii\helpers\FileHelper;
class MyCustomForm extends Model
{
public $file_image;
public function rules()
{
return [
[
[['file_image'], 'file', 'skipOnEmpty' => false, 'extensions' => 'jpg, jpeg, png, bmp, jpe']
]
];
}
public function scenarios()
{
$scenarios = parent::scenarios();
//Scenarios Attributes that will be validated
$scenarios['action_add'] = ['file_image'];
$scenarios['action_edit'] = ['file_image'];
return $scenarios;
}
}
And this is how my controller actions looks like.
The Create action works as expected (on POST request I'm taking the Uploaded File with UploadedFile::getInstance command)
public function actionCreate()
{
$model_form = new MyCustomForm(['scenario' => 'action_add']);
if (Yii::$app->request->isPost && $model_form->load(Yii::$app->request->post())) {
$model_form->file_image = \yii\web\UploadedFile::getInstance($model_form, "file_image");
if($model_form->validate()) {
if(isset($model_form->file_image)){
//I'm uploading my image to some cloud server here
//creating corresponding $model_entity for database, fill with the data from the form and save it
$model_entity->save();
}
}
}
}
But I'm facing with an issue when doing the same on Update action. In database I have the URL of image that is on third party cloud server and I can access it and display the image in my form (so GET request on Update works, I'm getting the corresponding entity from database and fill the form with data). But for POST, file validation is failing, if I don't have file assigned in the model and the POST request for Update is not working.
public function actionUpdate($id)
{
$model_entity = $this->findModel($id);
$image_URL = //I'm having URL to corresponding image here;
$model_form = new MyCustomForm(['scenario' => 'action_edit']);
$model_form_data = [$model_form->formName() => $model_entity->attributes];
$model_form->load($model_form_data);
if (Yii::$app->request->isPost && $model_form->load(Yii::$app->request->post())) {
//if we upload image again this will work
//NOTE: I have another text fields in the form and If I only change them
//and if I don't change the file image, the following result will return NULL and validation will fail
$file_uploaded_image = \yii\web\UploadedFile::getInstance($model_form, "file_image");
if(isset($file_uploaded_image)){
//new image is uploaded on update, this scenario will be OK
} else {
//no image is uploaded on update, this scenario will fail
}
if($model_form->validate()) {
//this will fail if I don't upload image on Update
}
}
}
I have tried many things in Update action, before validation, in order to find a workaround to get the image and validation to not fail. For example:
$dataImg = file_get_contents($image_URL);
$model_form->file_image = $dataImg;
or trying to do get with temporary file:
$dataImg = file_get_contents($dataImg);
$filePath = \yii\helpers\FileHelper::createDirectory("/images/tmp/test.png", 777);
file_put_contents($filePath, $dataImg);
$model_form->file_image = $filePath;
But none of them is working. Is there any solution to this scenario?
Note that I will have to use a Froms (as above) and not the ActiveRecord, since my real project is more complex that example listed.
Write this code in your Model(MyCustomForm) :
class MyCustomForm extends Model
{
public $file_image;
public function rules()
{
return [
[['file_image',],'required','on'=>['create','update']],
[['file_image'], 'file','extensions' => 'jpg, jpeg, png, bmp, jpe'],
];
}
}
Write this code in your actionCreate() :
$model_form = new MyCustomForm();
$model_form->scenario = "create";
Write this code in your actionUpdate() :
$model_form = new MyCustomForm();
$model_form->scenario = "update";
Or you can add scenario by this :
$model_form = new MyCustomForm(['scenario' => 'create']);
I have tried this and it is working.

Why session.getSaveBatch() is undefined when child record was added - Ext 5.1.1

Well the title says it all, details following.
I have two related models, User & Role.
User has roles defined as:
Ext.define('App.model.security.User', {
extend: 'App.model.Base',
entityName: 'User',
fields: [
{ name: 'id' },
{ name: 'email'},
{ name: 'name'},
{ name: 'enabled', type: 'bool'}
],
manyToMany: 'Role'
});
Then I have a grid of users and a form to edit user's data including his roles.
The thing is, when I try to add or delete a role from the user a later call to session.getSaveBatch() returns undefined and then I cannot start the batch to send the modifications to the server.
How can I solve this?
Well after reading a lot I found that Ext won't save the changed relationships between two models at least on 5.1.1.
I've had to workaround this by placing an aditional field on the left model (I named it isDirty) with a default value of false and set it true to force the session to send the update to the server with getSaveBatch.
Later I'll dig into the code to write an override to BatchVisitor or a custom BatchVisitor class that allow to save just associations automatically.
Note that this only occurs when you want to save just the association between the two models and if you also modify one of the involved entities then the association will be sent on the save batch.
Well this was interesting, I've learned a lot about Ext by solving this simple problem.
The solution I came across is to override the BatchVisitor class to make use of an event handler for the event onCleanRecord raised from the private method visitData of the Session class.
So for each record I look for left side entities in the matrix and if there is a change then I call the handler for onDirtyRecord which is defined on the BatchVisitor original class.
The code:
Ext.define('Ext.overrides.data.session.BatchVisitor', {
override: 'Ext.data.session.BatchVisitor',
onCleanRecord: function (record) {
var matrices = record.session.matrices
bucket = null,
ops = [],
recordId = record.id,
className = record.$className;
// Before anything I check that the record does not exists in the bucket
// If it exists then any change on matrices will be considered (so leave)
try {
bucket = this.map[record.$className];
ops.concat(bucket.create || [], bucket.destroy || [], bucket.update || []);
var found = ops.findIndex(function (element, index, array) {
if (element.id === recordId) {
return true;
}
});
if (found != -1) {
return;
}
}
catch (e) {
// Do nothing
}
// Now I look for changes on matrices
for (name in matrices) {
matrix = matrices[name].left;
if (className === matrix.role.cls.$className) {
slices = matrix.slices;
for (id in slices) {
slice = slices[id];
members = slice.members;
for (id2 in members) {
id1 = members[id2][0]; // This is left side id, right side is index 1
state = members[id2][2];
if (id1 !== recordId) { // Not left side => leave
break;
}
if (state) { // Association changed
this.onDirtyRecord(record);
// Same case as above now it exists in the bucket (so leave)
return;
}
}
}
}
}
}
});
It works very well for my needs, probably it wont be the best solution for others but can be a starting point anyways.
Finally, if it's not clear yet, what this does is give the method getSaveBatch the ability to detect changes on relationships.

Magento product save, how to detect whether product data has been changed

While editting a product in the backend I need to know whether any of it's data has been changed or not?
$product->hasDataChanges() always return true even I didn't modify any fields.
Why does $product->hasDataChanges() always return true even I didn't modify any fields.?
Looking into the Varien_Object function setData function it appears that hasDataChanges is always set to true even if technically the data has not changes.
public function setData($key, $value=null)
{
$this->_hasDataChanges = true;
if(is_array($key)) {
$this->_data = $key;
$this->_addFullNames();
} else {
$this->_data[$key] = $value;
if (isset($this->_syncFieldsMap[$key])) {
$fullFieldName = $this->_syncFieldsMap[$key];
$this->_data[$fullFieldName] = $value;
}
}
return $this;
}
Solution:
When you have a model which is an type of Mage_Core_Model_Abstract, then you can easily get the previous data (original data) on save using public function getOrigData($key=null) method.
getOrigData() returns the data in the object at the time it was initialized/populated.
After the model is initialised you can update that data and getData() will return what you currently have in that object.
Have a look at Varien_Object (getOrigData,setOrigData) so you can have a look at how and why it is used.

Resources