Google datastore modelling - google-cloud-datastore

I have two entities. Categories and Products. Each product can have many categories and each category can belong to many categories. I would like to be able to return all products and for each one to have all the categories it belongs to and their parent categories. Is datastore a good option for something like this?
example Product response:
{
id: 1,
name: "shoes",
categories: [{
id:1,
ordinal:1
},{
id:2,
ordinal:1
}]
}

I assume you meant 'Datastore' where you wrote 'dataflow'
It depends if you want to query later based on categories or not.
In case you won't need to query based on categories
I'd suggest defining your categories in a model (with the appropriate parent/child relationships) and then adding each category to a product in a LocalStructuredProperty. Datastore will save as a blob but will reconstruct the category entity model when you retrieve the product. You could also add a JsonProperty with a serialized string containing the category structure for each product. For example:
[
0: {
category: 'Electronics',
subcategories: ['Smartphones', 'Telephones', 'Gadgets']
},
1: {
category: 'Apple',
subcategories: ['iPhone']
}
]
Read more about the LocalStructuredProperty and the JsonProperty here (for Python client library).
If you need to query based on categories
Then you should use a StructuredProperty. Create a model to define your categories and their ancestor paths. Then you add one or more categories (along with their parents) to the Product entity when you instantiate it.
The Entity Property Reference in Datastore documentation has a good example of how to implement it (in Python, but also available for other languages). And here's how you filter for StructuredProperty values.

Related

How to retrieve only the relation ID, not the whole entity in MikroORM?

I have the following basic test entities:
#Entity()
class Author {
#PrimaryKey()
public id!: number;
#Property()
public name!: string;
}
#Entity()
class Book {
#PrimaryKey()
public id!: number;
#Property()
public title!: string;
#ManyToOne({joinColumn: 'authorID'})
public author!: Author;
}
What i'm trying, is to select only a single Book record, with its 'author', but I care only about its ID, I don't want to actually load the entity.
If I simply call this, it won't work (no author data loaded at all):
em.getRepository(Book).findOne({id: 1}, {fields: ['id', 'title', 'author.id']});
'author.id' doesn't do the trick, the SQL doesn't even contain the 'authorID' field.
If I add 'author' to the fields list as well, it works, author is loaded (only with the ID), but as a separate entity, with a separate, additional SQL statement! That's what I'm trying to avoid.
em.getRepository(Book).findOne({id: 1}, {fields: ['id', 'title', 'author', 'author.id']})
#1. SQL
select `b0`.`id`, `b0`.`title`, `b0`.`authorID` from `book` as `b0` where `b0`.`id` = 1 limit 1
#2. SQL (this wouldn't be neccessary as I want only the ID)
select `a0`.`id` from `author` as `a0` where `a0`.`id` in (2)
--> Result:
Book: { id: 1, title: 'a book', author: { id: 2 } }
The only way I found is to add the specific 'authorID' field too to the Book entity:
#Property()
public authorID!: number;
But, I'd like to avoid introducing these foreign key columns, it would be better to handle through the already existing and used 'author' relation (only by the 'id' property).
Does any solution exists where I could retrieve a relation's ID without generating a 2nd SELECT statement (for the relation), and even avoid introducing the foreign key (next to the already existing relation property)? Would be great to receive through the relation without any extra sql statement.
Thanks in advance.
It is correct behaviour you see the second query, that is how population works, and the fact that you want just a single property from the entity does not change anything, you still populate the relation, and each relation will use its own query to load it. You can use LoadStrategy.JOINED if you want to use a single query. But that would still do a join for that relation, which is not needed for your use case.
Given you only want the FK to be present, you dont need to care about the target entity at all. This should do the trick too:
em.getRepository(Book).findOne(1, {
fields: ['id', 'title', 'author'],
populate: [],
});
This way you say you want those 3 properties to be part of what's selected from the Book entity. You already have the author property, which represents the FK. You will end up with what you want once you serialize such entity. During runtime, you will see entity reference there - an entity with just the PK. It is represented as Ref<Author> when you console.log such entity.
Note that you need that populate: [] there, as otherwise it would be inferred from your fields which contains author property, and that would trigger the full load of it.

WP-Cli wc product create Custom attributes

Good day,
I'm using the wp-cli for adding products to Wordpress, for example i use this one:
wp --allow-root wc product create --user=1 --name="Restricted" --regular_price=1
I do have some attributes called test_1 (checkbox for yes) and test_2 is a multiselect. But is there a way to fill that attributes?
I did try this:
wp wc product create --user=1 --name="Restricted" --regular_price=1 --test_1=yes --test_2=testvalue,testvalue2
But that did result in an error:
Error: Parameter errors:
unknown --test_1 parameter
unknown --test_2 parameter
And did this one, but the values were still empty:
wp wc product create --user=1 --name="Restricted" --regular_price=1 --attributes='[{"test_1": "yes", "test_2": ["testvalue","testvalue2"]}]'
And this one:
wp wc product create --user=1 --name="Restricted" --regular_price=1 --attributes='[{"test_1": 1, "test_2": ["testvalue","testvalue2"]]'
You need to specify attributes as JSON. Since you have 2 attributes, the proper command along with JSON Structure is.
wp wc product create --name='Product Name' --user=1
--attributes='[
{ "name": "test_1", "visible": true, "options" : ["yes", "no"] },
{ "name": "test_2", "visible": true, "options" : ["small", "medium"] }
]'
Check the 2nd FAQ here
It says that certain properties needs to be passed as JSON.
Some 'lists' are objects, for example, if you want to set categories for a product the REST API expects an array of objects: https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties
this reference to create woocommerce product using WP-CLI
https://github.com/woocommerce/woocommerce/wiki/WC-CLI-Overview#frequently-asked-questions
https://nicola.blog/2016/03/10/managing-products-woocommerce-cli/
https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties
if you add product Custom attributes or category through CLI than use JSON format like this
--attributes= [{ "name": "color", "visible": true, "options":["black","blue"]}]
--categories= [ { "id" : category_id } ]
Example demo:-
wp wc product create --name="mobile11" --description="this is mobile 11" --type=simple --regular_price=500 --sale_price=400 --user=dharmesh --categories='[ { "id" : 35 } ]' --attributes='[{ "name": "color", "visible": true, "options":["black","blue","red"]}]' --allow-root
Most of the time terminal isn't formatted properly sometimes it skips , sometime not formatting bash variable causes empty values. It depends how you're using declaring bash variable and using within woocommerce cli.
I was looking way correct format for adding / updating product attributes. This is how I was able to add product attributes for my product.
wp wc product update 2898 --user=1 --attributes='[{ "name":"Background Style","options":"White"},{ "name":"Demographic Level","options":"college-university"}]'
Where,
product_id is 2898
attributes are "Background Style" and "Demographic Level" and options are its corresponding terms.

Select posts that have a specific term using withSelect in Gutenberg block

I'm trying to select posts -- or more specifically, a custom post type -- that belong to a specific term, and display them in my Gutenberg block's edit screen. I'm wanting to recreate what would be tax_query with WP_Query, but in Javascript.
I'm able to select posts, but I'm unsure what parameters to use with getEntityRecords to select by term (or if it is even possible). The documentation is still a little vague at this point.
Here's where I'm at. This successfully selects all posts of the 'rmenu' type:
const items = select("core").getEntityRecords(
"postType",
"rmenu"
);
Does anyone know if getEntityRecords is the right way to handle this?
Thanks.
The third parameter to getEntityRecords is a query object. You can pass any query argument accepted by Wordpress API such as categories or tags. Selecting by category term would look like this:
const items = select("core").getEntityRecords(
"postType",
"rmenu",
{ categories: [ 13 ] }
}
Although Capi's answer is correct, it wasn't clear for me that this would also work for custom taxonomies. It turns out wp.data will add your custom taxonomies automatically as properties to the post object. For example, a post could look like this:
{
title: "hello world",
content: "this is a post",
id: 123,
type: 'my-custom-posttype'
my-custom-tax: [5, 8, 24],
...
}
So, in order to get all the posts of the type my-custom-posttype that have a term with ID 4 or 8 of the taxonomy my-custom-tax, you would run this query:
wp.data.select( 'core' ).getEntityRecords( 'postType', 'my-custom-posttype', { 'my-custom-tax':[4,8] })
Important! If you test this in your browser, you need to run it twice. The first time it will return an empty array, because it only invokes the promise.

Using WillCascadeOnDelete on ASP.MVC 5

From the code snippet below :
modelBuilder.Entity<Product>()
.HasMany(e => e.ProductPictures)
.WithRequired(e => e.Product)
.WillCascadeOnDelete(false);
Which is WillCascadeOnDelete referring to?
Product has
-ProductID
-ProductNumber
-ProductDescription
-Cost
-UnitPrice
-OnHandQty
-StartDate
-CreateDate
ProductPicture has
-ProductPictureId
-ProductId
-IsThumb
-SortOrder
-ProductPictureNote
which means that ProductPicture is the dependent one. Is WillCascadeOnDelete referring to Product? meaning that if Product is deleted then the ProductId property of the ProductPicture will be set to null.
"Cascading delete" is a configuration of a relationship, not of an entity/table. Hence WillCascadeOnDelete is a method of ascadableNavigationPropertyConfiguration.
It means that if a Product deleted from the database, it's ProductPictures should not be deleted together with the Product. That's a property of this specific relationship, not of the Product table.

Add a child to a #OneToMany bidirectionnal with FOSRestBundle

I'm building a REST API with FOSRestBundle on my Symfony2 application.
What I want to do is to allow my POST and PUT actions, let say on a Product entity who got a #OneToMany relationship with another entity called Risk, to add/delete the children I past in JSON.
Let me give to you an example of JSON:
{
"cproduct": "ASSOC000000000999",
"risks": [
{
//first risk fields
},
{
//second risk fields
}
]
}
This is a simple JSON (the real one got more fields but these aren't needed here).
So here I've my Product ID (ASSOC000000000999) and I want to update this product by adding to him 2 new risks.
I know that normally I would have to create a Risk with the product ID separately, but for the needs of my application, I need to make only one request to the database. I want my users to be able to create a product, then add one or more risks and only then persist it into the database.
In a second time I would like them to be able to delete a child (risk) if he doesn't appear in the JSON sent with PUT action.
Here an example, let say that product “ASSOC000000000999” got a risk “RISK1”.
If I send this JSON:
{
"cproduct": "ASSOC000000000999",
"risks": [
{
“id”: “RISK2”,
//other fields
},
{ “id”: “RISK3”,
//other fields
}
]
}
On persist I want RISK1 to be deleted.
How can I do that? I found nothing on the web about that, please help me. :-)
PS: Sorry for my English, this isn't my birth langage.
EDIT:
I target what my problem really is.
When I send that JSON file with HTTP PUT verb:
{
"cfam": "AUTE",
"lpronom": "My Contract",
"riss": [{
"cris": "AS",
"lris": "Organization Law of 1901",
"lrisfic": "RCAD_FICHERCA9"
}]
}
Doctrine does a SELECT on RIS (my Risk table is called RIS, so the collection is $riss in my PROduct entity) where CRIS = "AS", and that's my problem. Here I want doctrine to create a RIS if the composite PK {cpro, nprover, cris} doesn't exist, and an update if it exist.
How can I do that?
(Without using Symfony form if possible).
Here my API call :
http://localhost/web/web/app_dev.php/fos/api/pros/ASSOC000000000009_1
My putProAction():
public function putProAction($id, Request $request)
{
$detachedEntity = $this->reqDeserialize($request, 'Namespace\Bundle\ProductBundle\Entity\Pro');
// Here I need to explode my serialized PUT parameter ID
list($cpro, $nprover) = explode('_', $id);
$detachedEntity->setCPRO($cpro);
$detachedEntity->setNPROVER($nprover);
$em = $this->getDoctrine()->getManager();
// I use merge to attache the entity to perform the persist
$entity = $em->merge($detachedEntity);
$em->persist($entity);
$em->flush();
return $entity;
}
You should use Symfony's Form Collections.
The example listed on that page is very similar to your requirements.

Resources