I have entity who have attributes column which is #ORM\Column(type="json", nullable=true)
And each entity have multiple keys/values in this attributes column
"attributes": {
"name": "Item 1",
"slug": "item-1",
"term": "item-1"
}
Is it possible to create a filter to search slug or some other key in attributes column?
I know the api platform have #ApiFilter(SearchFilter::class, properties={"attributes": "ipartial"}) and this kind of filter in url looks like /api/items.json?attributes[]=item-1.
If posible to set key via url like: ?attributes[slug]=item-1
Or there is another way to do it?
Related
I want my AssociationField to display a blank value by defaut, in ordre to force the users to select an item.
As I understand, AssociationField is based on EntityType FormType.
My Problem is, if the field of my entity is required, the create form renders the AssociationField without empty value, and thus selects the first value.
I haven't seen any option to add a placeholder to a select for required properties.
Here are some samples:
My Doctrine entity :
//Player.php
#[ORM\ManyToOne(inversedBy: 'players')]
#[ORM\JoinColumn(name:'team_id', referencedColumnName: 'id', nullable: false)]
private Team $team;
... and my CrudController
//PlayerCrudController.php
public function configureFields(string $pageName): iterable
{
return [
//...
AssociationField::new('team'),
//...
]
}
And the create form displays a select with already a value selected.
I've looked into the docs but couldn't find how to achieve this.
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.
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.
What I'm asking here is something weird. I'm using Symfony2 with Doctrine2. To bypass a issue between FOSRestBundle and JMSBundle (who don't like composite primary key) I need to know if it's possible - and how - to call a setter from the name of the field.
Here an example: imagine my Product entity have a primary key composed by 3 fields.
The trick is to send an array this kind of JSON into my API REST:
{
"id": {
"field1": "xxx",
"field2": "...",
"field3": "..."
},
// other fields
}
Then I use json_decode() to extract the ID field. I create a table of $key => $value with the 3 primary keys. And there is my problem, with this table I want, in a foreach loop, to do something like $myEntity->set$KEY($VALUE).
I'm doing this because I want to reuse this code with all my entities.
I know that this is something really weird, to sum up with only the name of the object field/property I want to call is appropriate setter().
Thanks ;-)
PS: sorry if my English isn't perfect, this isn't my birth langage.
You can use dynamic method name in PHP :
$myEntity->{'set'.$KEY}($VALUE);
or more readable version :
$method = 'set'.$KEY;
$myEntity->$method($VALUE);
Don't forget to check if method exists anyway :
$method = 'set'.$KEY;
if (!method_exists($myEntity, $method))
throw new \Exception('Something bad');
$myEntity->$method($VALUE);
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.