entity_name.repositoryFor products this then would be
product.repository. Additional to that, you're going to need the
taxrepository later for this guide, so let's add this as well already.
WritingDataand applied the repositories as a constructor parameter. If you want to fetch data for another entity, just switch the
services.xmlto whatever repository you need, e.g.
writeData, which will take of creating a new product. Its first parameter is the
Context, which you need for the upcoming methods. This is usually passed through the stack, starting at a controller or event.
createmethod on the product repository with two parameters. The first one is an array of arrays, since you can write more than just one product with a single call, thus the first array. The second and inner array is representing the entities' data to be written.
price. So the first three fields are just plain values, easy as that.
taxIdthough represents the ID of the associated
tax. Since we want to assign an existing tax here, we've created a new method called
getTaxIdto actually read the ID that we need. For this purpose, you need to understand how to read data from Shopware, so have a look at our guide about reading data. We're calling
taxRepositoryto only get IDs, since we don't need the full tax data here. Since we only need the first ID with the given tax rate here, we're just grabbing the first ID by using the
firstIdmethod on the collection. And there we go, we got a tax ID to fill into the mandatory field
price. The price is saved to the product entity via a
JsonField, so it's saved in the JSON format in the database. A product can have multiple prices, thus we're providing an array of arrays again here. For this example we'll still just write a single price. The structure for the JSON can be found in the getConstraints method of the PriceFieldSerializer. Basically you need to provide a currency ID, for which we'll just use the shop's default currency, a gross and a net price and a boolean value of whether or not the gross and the net price are linked. If
linkedis set to
true, changes to the gross price will also affect the net price, using the product's tax.
Uuidclass can be found here:
Shopware\Core\Framework\Uuid\UuidMake sure to import this class first.
idfield we've provided now - even though you're just creating your new entity, you can already define which ID it's going to have, so you can keep working with the said ID right afterwards without having to fetch the recently written data again.
updatemethod on the repository. Let's just update our previously created product and change its name.
upsertmethod. Make sure to provide an ID in the data, because otherwise the data will always be created and never updated.
createmethod. For updating data, we've used the
updatemethod. You might have guesses it already, for this example you'll need the
taxIdin this example, with the respective ID value of the entity to be associated.
categories. Since this is a
ManyToManyassociation, you could technically assign more than just one category, hence the array of arrays again. In the second inner array, you just need to fill the
ManyToManyassociation comes with a mapping entity. It's important to know that you cannot update a mapping entity itself.
categoriesfield here. Just remember to use an array of arrays for