1. Installation¶
We assume you’re familiar with Composer, a dependency manager for PHP. Use the following command to add the bundle to your composer.json and download package.
If you have Composer installed globally.
$ composer require "sylius/inventory-bundle"
Otherwise you have to download .phar file.
$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar require "sylius/inventory-bundle"
1.1. Adding required bundles to the kernel¶
First, you need to enable the bundle inside the kernel. If you’re not using any other Sylius bundles, you will also need to add SyliusResourceBundle and its dependencies to the kernel. Don’t worry, everything was automatically installed via Composer.
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new FOS\RestBundle\FOSRestBundle(),
new Sylius\Bundle\ResourceBundle\SyliusResourceBundle(),
new Sylius\Bundle\InventoryBundle\SyliusInventoryBundle(),
);
}
1.2. Creating your entities¶
Let’s assume we want to implement a book store application and track the books inventory.
You have to create a Book and an InventoryUnit entity, living inside your application code.
We think that keeping the app-specific bundle structure simple is a good practice, so
let’s assume you have your AppBundle registered under App\Bundle\AppBundle namespace.
We will create Book entity.
<?php
// src/App/AppBundle/Entity/Book.php
namespace App\AppBundle\Entity;
use Sylius\Bundle\InventoryBundle\Model\StockableInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="app_book")
*/
class Book implements StockableInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $isbn;
/**
* @ORM\Column(type="string")
*/
protected $title;
/**
* @ORM\Column(type="integer")
*/
protected $onHand;
/**
* @ORM\Column(type="boolean")
*/
protected $availableOnDemand;
public function __construct()
{
$this->onHand = 1;
$this->availableOnDemand = true;
}
public function getId()
{
return $this->id;
}
public function getIsbn()
{
return $this->isbn;
}
public function setIsbn($isbn)
{
$this->isbn = $isbn;
}
public function getSku()
{
return $this->getIsbn();
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getInventoryName()
{
return $this->getTitle();
}
public function isInStock()
{
return 0 < $this->onHand;
}
public function isAvailableOnDemand()
{
return $this->availableOnDemand;
}
public function setAvailableOnDemand($availableOnDemand)
{
$this->availableOnDemand = (Boolean) $availableOnDemand;
}
public function getOnHand()
{
return $this->onHand;
}
public function setOnHand($onHand)
{
$this->onHand = $onHand;
}
}
Примечание
This example shows the full power of StockableInterface. The bundle also provides an Stockable entity which implements StockableInterface for you. By extending the Stockable entity, the example above can be dramatically simplified.
In order to track the books inventory our Book entity must implement StockableInterface.
Note that we added ->getSku() method which is alias to ->getIsbn(), this is the power of the interface,
we now have full control over the entity mapping.
In the same way ->getInventoryName() exposes the book title as the displayed name for our stockable entity.
The next step requires the creating of the InventoryUnit entity, let’s do this now.
<?php
// src/App/AppBundle/Entity/InventoryUnit.php
namespace App\AppBundle\Entity;
use Sylius\Bundle\InventoryBundle\Entity\InventoryUnit as BaseInventoryUnit;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="app_inventory_unit")
*/
class InventoryUnit extends BaseInventoryUnit
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
Note that we are using base entity from Sylius bundle, which means inheriting some functionality inventory bundle provides. InventoryUnit holds the reference to stockable object, which is Book in our case. So, if we use the InventoryOperator to create inventory units, they will reference the given book entity.
1.3. Container configuration¶
Put this configuration inside your app/config/config.yml.
sylius_inventory:
driver: doctrine/orm
backorders: true
classes:
unit:
model: App\AppBundle\Entity\InventoryUnit
stockable:
model: App\AppBundle\Entity\Book
1.4. Routing configuration¶
Import the routing configuration by adding the following to your app/config/routing.yml`.
sylius_inventory:
resource: @SyliusInventoryBundle/Resources/config/routing.yml
1.5. Updating database schema¶
Remember to update your database schema.
For “doctrine/orm” driver run the following command.
$ php app/console doctrine:schema:update --force
Предупреждение
This should be done only in dev environment! We recommend using Doctrine migrations, to safely update your schema.
1.6. Templates¶
The bundle provides some default bootstrap templates.
Примечание
You can check our Sandbox app to see how to integrate it in your application.