-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
937 additions
and
706 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 52 additions & 65 deletions
117
docs/03_Development/01_Extending_Guide/01_Extend_CoreShop_Resources.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,113 @@ | ||
# Extend CoreShop Resources | ||
|
||
All models in Coreshop are placed in the `Coreshop\Component\*ComponentName*\Model` namespaces alongside with their | ||
interfaces. | ||
All models in Coreshop are placed in the ```Coreshop\Component\*ComponentName*\Model``` namespaces alongside with their interfaces. | ||
|
||
> Many models in CoreShop are extended in the Core component. If the model you are willing to override exists in the | ||
> Core you should be extending the Core one, not the base model from the component. | ||
> Many models in CoreShop are extended in the Core component. If the model you are willing to override exists in the Core you should be extending the Core one, not the base model from the component. | ||
## How to Customize a Model | ||
|
||
First things first: If you want to extend coreshop models your Bundle must extend the `AbstractResourceBundle`. Next you | ||
have set your supported drivers. Just add the following lines of code to your bundle class: | ||
### Doctrine ORM | ||
|
||
```php | ||
public function getSupportedDrivers(): array | ||
{ | ||
return [ | ||
CoreShopResourceBundle::DRIVER_DOCTRINE_ORM | ||
]; | ||
} | ||
``` | ||
|
||
After that have to tell the bundle where your models are. For that, add the override the following method in your bundle | ||
class and return the model namespace. Here is an example for the `AppBundle`: | ||
Depending on your Doctrine ORM configuration, you need to place your Models in the right Folder. The Default configuration, places them in the `Entity` directory: | ||
|
||
```php | ||
protected function getModelNamespace(): string | ||
{ | ||
return "AppBundle\Model"; | ||
} | ||
```yaml | ||
doctrine: | ||
orm: | ||
mappings: | ||
App: | ||
type: attribute | ||
dir: '%kernel.project_dir%/src/App/Entity' | ||
is_bundle: false | ||
prefix: App\Entity | ||
alias: App | ||
``` | ||
Here a quick overview for you which directories are important for you, when customizing CoreShop models. | ||
### Example Model | ||
| Folder | Description | | ||
|-------------------------------------------|-----------------------------------------------| | ||
| `AcmeBundle/Model` or `AcmeBundle/Entity` | Where your models are living | | ||
| `AcmeBundle/config/doctrine/model` | Put your doctrine `.yml` config files in here | | ||
| `AcmeBundle/config/serializer` | The serializer configs for the models | | ||
|
||
Let’s take the `CoreShop\Component\Currency\Model\Currency` as an example. This one is extended in Core. How can you | ||
check that? | ||
Let’s take the [```CoreShop\Component\Currency\Model\Currency```](https://github.com/coreshop/CoreShop/blob/master/src/CoreShop/Component/Currency/Model/Currency.php) as an example. This one is extended in Core. How can you check that? | ||
|
||
First of all, you need to find the current used class by doing following: | ||
|
||
```bash | ||
$ php bin/console debug:container --parameter=coreshop.model.currency.class | ||
``` | ||
|
||
As a result you will get the `CoreShop\Component\Core\Model\Currency` - this is the class that you need to be extending. | ||
As a result you will get the [```CoreShop\Component\Core\Model\Currency```](https://github.com/coreshop/CoreShop/blob/master/src/CoreShop/Component/Core/Model/Currency.php) - this is the class that you need to be extending. | ||
|
||
Assuming you want to add a field called **flag**: | ||
Assuming you want to add a field called **flag** | ||
|
||
**1.** The first thing to do is to write your own class which will extend the base `Currency` class: | ||
**1.** The first thing to do is to write your own class which will extend the base ```Currency``` class | ||
|
||
```php | ||
<?php | ||
namespace AppBundle\Entity; | ||
namespace App\Entity; | ||
use CoreShop\Component\Core\Model\Currency as BaseCurrency; | ||
use Doctrine\ORM\Mapping as ORM; | ||
/** | ||
* @ORM\Entity | ||
* @ORM\Table(name="coreshop_currency") | ||
*/ | ||
class Currency extends BaseCurrency | ||
{ | ||
/** | ||
* @var bool | ||
* @ORM\Column(type="boolean") | ||
*/ | ||
private $flag; | ||
private bool $flag = false; | ||
/** | ||
* @return bool | ||
*/ | ||
public function getFlag() | ||
public function getFlag(): bool | ||
{ | ||
return $this->flag; | ||
} | ||
/** | ||
* @param bool $flag | ||
*/ | ||
public function setFlag($flag) | ||
public function setFlag(bool $flag): void | ||
{ | ||
$this->flag = $flag; | ||
} | ||
} | ||
``` | ||
|
||
**2.** Next define your entity’s mapping. The file should be placed | ||
in `AppBundle/Resources/config/doctrine/Currency.orm.yml`: | ||
|
||
```yaml | ||
AppBundle\Entity\Currency: | ||
type: mappedSuperclass | ||
table: coreshop_currency | ||
fields: | ||
flag: | ||
type: boolean | ||
nullable: true | ||
``` | ||
**2**. Finally you’ll need to override the model’s class in the ```config/config.yaml```. | ||
|
||
**3.** Finally, you’ll need to override the model’s class in the `app/config/config.yml`. | ||
Under the core_shop_* where * is the name of the bundle of the model you are customizing, in our case it will be the CoreShopCurrencyBundle -> core_shop_currency. | ||
|
||
Under the `core_shop_currency`, modify as follows: | ||
|
||
```yaml | ||
core_shop_currency: | ||
resources: | ||
currency: | ||
classes: | ||
model: AppBundle\Entity\Currency | ||
resources: | ||
currency: | ||
classes: | ||
model: App\Entity\Currency | ||
``` | ||
|
||
**4.** Update the database. There are two ways to do it: | ||
**3**. Update the database. There are two ways to do it. | ||
|
||
via direct database schema update: | ||
|
||
```bash | ||
$ php bin/console doctrine:schema:update --force | ||
``` | ||
|
||
via migrations (recommended): | ||
via migrations: | ||
Which we strongly recommend over updating the schema. | ||
|
||
```bash | ||
$ php bin/console doctrine:migrations:diff | ||
$ php bin/console doctrine:migrations:migrate | ||
``` | ||
|
||
**4**. We also need a serializer group for the new field. This is done in the ```config/jms_serializer/Currency.yaml``` file. | ||
|
||
```yaml | ||
App\Entity\Currency: | ||
exclusion_policy: ALL | ||
xml_root_name: store | ||
properties: | ||
flag: | ||
expose: true | ||
type: bool | ||
groups: [Detailed] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.