Update existing entity - shopware

I am retrieving an Category entity through the DAL and change some values.
$randomId = 'an-existing-category-id';
$result = $this->categoryRepository->search((new Criteria())->addFilter(new EqualsAnyFilter('id', $ids)), Context::createDefaultContext());
$entity = $result->get($randomId);
$entity->setParentId('new-parent-id');
Instead of converting the entity to array and passing it to the DAL to upsert it i wonder if it is possible to "persist" the complete entity like in doctrine?

It is not possible, you can update a record using only update or upsert method of the entity repository.
But you don't need to convert the whole entity to an array, you just need to provide id and fields that should be updated (not all fields).

Related

Azure Table Storage Update making columns as Null

I am using Azure Table Storage Rest Api to Update the entity.
If the entity has 5 columns(col1,col2,col3,col4,col5) and I am making a PUT request body something like this.
{"col1":"value"}
The rest of the columns value is set to NULL.
Is there a way to avoid it?
The reason you're seeing this behavior is because you're performing Update Entity operation which will replaces an entire entity.
If you're only interested in changing just one attribute (property), you need to use Merge Entity operation which updates an existing entity by updating the entity's properties. It does not replace the existing entity.
Simply changing your HTTP Request method to MERGE from PUT should do the trick.

How can I retrieve the id of a document I added to a Cosmosdb collection?

I have a single collection into which I am inserting documents of different types. I use the type parameter to distinguish between different datatypes in the collection. When I am inserting a document, I have created an Id field for every document, but Cosmosdb has a built-in id field.
How can I insert a new document and retrieve the id of the created Document all in one query?
The CreateDocumentAsync method returns the created document so you should be able to get the document id.
Document created = await client.CreateDocumentAsync(collectionLink, order);
I think you just need to .getResource() method to get the create document obj.
Please refer to the java code:
DocumentClient documentClient = new DocumentClient(END_POINT,
MASTER_KEY, ConnectionPolicy.GetDefault(),
ConsistencyLevel.Session);
Document document = new Document();
document.set("name","aaa");
document = documentClient.createDocument("dbs/db/colls/coll",document,null,false).getResource();
System.out.println(document.toString());
//then do your business logic with the document.....
C# code:
Parent p = new Parent
{
FamilyName = "Andersen.1",
FirstName = "Andersen",
};
Document doc = client.CreateDocumentAsync("dbs/db/colls/coll",p,null).Result.Resource;
Console.WriteLine(doc);
Hope it helps you.
Sure, you could always fetch the id from creation method response in your favorite API as already shown in other answers. You may have reasons why you want to delegate key-assigning to DocumentDB, but to be frank, I don't see any good ones.
If inserted document would have no id set DocumentDB would generate a GUID for you. There wouldn't be any notable difference compared to simply generating a new GUID yourself and assign it into id-field before save. Self-assigning the identity would let you simplify your code a bit and also let you use the identity not only after persisting but also BEFORE. Which could simplify a lot of scenarios you may have or run into in future.
Also, note that you don't have to use GUIDs as as id and could use any unique value you already have. Since you mentioned you have and Id field (which by name, I assume to be a primary key) then you should consider reusing this instead introducing another set of keys.
Self-assigned non-Guid key is usually a better choice since it can be designed to match your data and application needs better than a GUID. For example, in addition to being just unique, it may also be a natural key, narrower, human-readable, ordered, etc.

How to select only needed fields of objects?

I am using the Pimcore API to fetch objects.
$myObjects = new Object\MyObject\Listing();
$myObjects->load();
$myObjects->getObjects();
Works as expected. Now I want to select only a specific field of my objects, e.g. the name field.
How can I tell Pimcore to select only fields that I want? Is it even possibile through the API or do I need to use custom SQL? If so, how can I do that?
Best regards
The pimcore listing is always returning the complete set of objects matching your listing condition...
If you want a fast and easy way to only select one field of your object, I recommend to use the pimcore db class:
$db = \Pimcore\Db::get();
$fieldsArray = $db->fetchCol("SELECT `YOUR_FIELD` FROM `object_query_CLASS-ID`");
This will return you an array width all 'YOUR_FIELD' values from the object query table of your class.
To get the class ID for your query dynamically your should use:
$classId = \Pimcore\Model\Object\MyObject::classId();
Edit:
To get more than one field column, you need to use 'fetchAll' instead of 'fetchCol':
$fieldsArray = $db->fetchAll("SELECT `YOUR_FIELD`, `YOUR_FIELD_2` FROM `object_query_CLASS-ID`");

Pre-allocation of attributes by creating new entity

While creating new entity in CRM, some fields will be pre-allocated. Exmaple: for a new contact - transactioncurrency and ownerid will be filled with values. Haw can I programmatically find out, which rules will be used for such pre-allocation. I can't call "initializefromrequest" to find it out, because I want to build offline DB and there apply the rules
thanks
Get the EntityMetadata for your entity then examine AttributeMetadata for each Attribute to determine if the IsCustomAttribute Property is true
Retrieve Entity metadata using EntityFilters = EntityFilters.Attributes
Iterate the Attribute collection of the EntityMetadata in the response
Check the IsCustomAttribute property on each AttributeMetadata

Core Data automatic migration doesn't create the new entity in my new model

I made a new version of my core data model which includes one new entity "Test" with one attribute "type".
The lightweight migration worked with no errors but I noticed that the new entity was not created in the database. When fetching this new entity "Test" , the result was nil. I had the attribute "type" set as non-optional with a default value of 1 because I expected that the migration will create the new entity with this default value . But it didn't. So my question is : is this expected behavior? In the case of core data migration, the new entities are not created but set as nil? Or if I am missing some steps , what happens if the new model has relationships between the new entity "Test" and the existing entities? Thanks.
You need to create an object of this entity, using for example -[NSEntityDescription insertNewObjectForEntityName:...]. Simply having the entity in the model will not create any objects.
You can do this at the end of migration, or lazily at startup (if you do a fetch for an object of this entity and can't find it).

Resources