I've implemented for the first time Doctrine-ORM on my SOAP server application; can you tell me if I could optimize some of the written ORM-related code?
More specifically, is it a good practice to package entities in sub-namespaces, or is it better to simply put them in an Entity namespace ?
I cannot post the XML used for parsing as it is too long, but it isn't the concern.
Soap serve method
/**
* @param string $apiKey
* @param $id
* @return mixed
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Doctrine\ORM\TransactionRequiredException
*/
public function getProductConfig($apiKey, $id)
{
/** @var Response $response */
// check credentials
$response = $this->checkCredentials($apiKey);
if ($response->getSuccess()) {
// call api
$response = $this->api->getProductDetails($id);
if ($response->getSuccess()) {
$responseCode = self::RESPONSE_OK;
// populate database
if ($response->product) {
if (!$product = $this->em->find('Application\Entity\Prduct', $response->product->id)) {
$product = new Entity\Product();
$product->setId($response->product->id);
}
$product->setType($response->product->type)
->setName($response->product->name)
->setStock($response->product->stock)
->setDescription($response->product->description);
$this->em->persist($product);
$this->em->flush();
if ($product->getId() && $response->config) {
foreach ($response->config as $data) {
if (!$config = $this->em->find('Application\Entity\Product\Configuration', $data->id)) {
$config = new Entity\Product\Configuration();
$config->setId($data->id);
}
$config->setName($data->name)
->setDescription(trim($data->description) ?: null)
->setCategory($data->category)
->setDontChargeDefault($data->config->dontchargedefault)
->setInitialValue($data->config->initialval)
->setLowValue($data->config->minvalue)
->setHighValue($data->config->maxvalue)
->setProduct($product);
$this->em->persist($config);
}
$this->em->flush();
}
} else {
$responseCode = self::RESPONSE_ERROR;
}
// create new response from subcall
$response = new Response(array(
'call' => __FUNCTION__,
'code' => $responseCode
));
}
}
return $this->handleResponse($response);
}
Product model (have many configurations)
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* Product entity
* @ORM\Table(name="Products")
* @ORM\Entity
* @package Application\Entity
*/
class Product
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
*/
protected $id;
/** @ORM\Column(type="integer") */
protected $type;
/** @ORM\Column(type="string") */
protected $name;
/** @ORM\Column(type="integer") */
protected $stock;
/** @ORM\Column(type="text") */
protected $description;
/**
* @var Collection
* @ORM\OneToMany(targetEntity="Application\Entity\Product\Configuration", mappedBy="product", fetch="LAZY")
*/
protected $configurations;
/**
* Constructor
*/
public function __construct()
{
$this->configurations = new ArrayCollection();
}
/**
* @return Collection
*/
public function getConfigurations()
{
return $this->configurations;
}
/**
* Get id
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set product Id
* @param int $id
* @return Product
*/
public function setId($id)
{
$this->id = (int)$id;
return $this;
}
/**
* Get product description
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set product description
* @param string $description
* @return Product
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get product name
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set product name
* @param string $name
* @return Product
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get product stock availability
* @return int
*/
public function getStock()
{
return $this->stock;
}
/**
* Set product stock availability
* @param int $stock
* @return Product
*/
public function setStock($stock)
{
$this->stock = $stock;
return $this;
}
/**
* Get product type
* @return int
*/
public function getType()
{
return $this->type;
}
/**
* Set product type id
* @param int $type
* @return Product
*/
public function setType($type)
{
$this->type = (int)$type;
return $this;
}
}
product configuration (belongs to one product)
<?php namespace Application\Entity\Product;
use Application\Entity\Product;
use Doctrine\ORM\Mapping as ORM;
use InvalidArgumentException;
/**
* Product config entity
* @ORM\Table(name="ProductsConfigurations")
* @ORM\Entity
* @package Application\Entity\Product
*/
class Configuration
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
*/
protected $id;
/** @ORM\Column(type="string") */
protected $name;
/** @ORM\Column(type="string", nullable=true) */
protected $description;
/** @ORM\Column(type="string") */
protected $category;
/** @ORM\Column(type="integer") */
protected $lowValue;
/** @ORM\Column(type="integer") */
protected $highValue;
/** @ORM\Column(type="integer") */
protected $initialValue;
/** @ORM\Column(type="boolean") */
protected $dontChargeDefault;
/**
* @var Product the product this configuration belongs to
* @ORM\ManyToOne(targetEntity="Application\Entity\Product", inversedBy="configurations")
* @ORM\JoinColumn(name="productId", referencedColumnName="id")
*/
protected $product;
/**
* @return Product
*/
public function getProduct()
{
return $this->product;
}
/**
* Sets a new user group and cleans the previous one if set
* @param Product $product
* @return Configuration
* @throws InvalidArgumentException
*/
public function setProduct($product)
{
if ($product === null) {
if ($this->product !== null) {
$this->product->getConfigurations()->removeElement($this);
}
$this->product = null;
} else {
if (!$product instanceof Product) {
throw new InvalidArgumentException(sprintf(
'$product must be null or instance of Product, %s given',
get_class($product)
));
}
if ($this->product !== null) {
$this->product->getConfigurations()->removeElement($this);
}
$this->product = $product;
$product->getConfigurations()->add($this);
}
return $this;
}
/**
* Get id
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set config Id
* @param int $id
* @return Configuration
*/
public function setId($id)
{
$this->id = (int)$id;
return $this;
}
/**
* Get config category
* @return string
*/
public function getCategory()
{
return $this->category;
}
/**
* Set config category
* @param string $category
* @return Configuration
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
* Get description
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set description
* @param string $description
* @return Configuration
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get don't charge default flag
* @return bool
*/
public function getDontChargeDefault()
{
return $this->dontChargeDefault;
}
/**
* Set don't charge default flag
* @param bool $dontChargeDefault
* @return Configuration
*/
public function setDontChargeDefault($dontChargeDefault)
{
$this->dontChargeDefault = (bool)$dontChargeDefault;
return $this;
}
/**
* Get highest config value
* @return int
*/
public function getHighValue()
{
return $this->highValue;
}
/**
* Set highest config value
* @param int $highValue
* @return Configuration
*/
public function setHighValue($highValue)
{
$this->highValue = (int)$highValue;
return $this;
}
/**
* Get initial config value
* @return int
*/
public function getInitialValue()
{
return $this->initialValue;
}
/**
* Set initial config value
* @param int $initialValue
* @return Configuration
*/
public function setInitialValue($initialValue)
{
$this->initialValue = (int)$initialValue;
return $this;
}
/**
* Get config lowest value
* @return int
*/
public function getLowValue()
{
return $this->lowValue;
}
/**
* Set config lowest value
* @param int $lowValue
* @return Configuration
*/
public function setLowValue($lowValue)
{
$this->lowValue = (int)$lowValue;
return $this;
}
/**
* Get config name
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set config name
* @param string $name
* @return Configuration
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
}