Swagger annotations with linked property array - annotations

I'm new to PHP Swagger and using the Laravel package L5-Swagger to create documentation for my API. Now I'm trying to let one model contain an array of the other model, an Order can have several OrderItems.
Unfortuantly I cannot get the linking to work. See attached screen shot.
What am I doing wrong?
This is my Order model:
/**
* #SWG\Definition(
* required={"order_id","order_items"},
* type="object",
* #SWG\Xml(name="Order")
* )
*/
class Order
{
/**
* #SWG\Property(example="O-789456123")
* #var string
*/
public $order_id;
/**
* #SWG\Property(type="array", items="$ref:OrderItem")
* #var array
*/
public $order_items = [];
}
This is my OrderItem model:
/**
* #SWG\Definition(
* required={"sku","quantity", "price_including_tax"},
* type="object",
* #SWG\Xml(name="OrderItem")
* )
*/
class OrderItem
{
/**
* #SWG\Property(example="SKU-123")
* #var string
*/
public $sku;
/**
* #SWG\Property(example=2)
* #var integer
*/
public $quantity;
/**
* #SWG\Property(example=199.75)
* #var float
*/
public $price_including_tax;
}

I think items="$ref:OrderItem" should be #SWG\Items(ref="#/definitions/OrderItem")
Ps. Checking the intermediate format (the swagger.json) can provide insight into what going wrong.

Related

Doctrine Table Inheritance - No identifier/primary key specified for Entity

In Doctrine 2/Symfony 3 with Postgres as the database I am trying to create a table with following fields:
**actions-reviews**
id
action_id
action_task_id
document_id
review_to
review_date
review_description
review_by
is_effective
The table is either linked to a doc_id which is foreign key for an entity called Document or action_task_id for entity called ActionTask. In order to achieve this I am using inheritance mapping via the use of discriminator. In skipper the entity relationship look like this:
As you can see both the ActionTask and ActionReview are sub-set of Action entity.
My issue is when create the entities and run php bin/console doctrine:schema:update --force I get the following error:
[Doctrine\ORM\Mapping\MappingException]
No identifier/primary key specified for Entity "AppBundle\Entity\ActionTask
Review". Every Entity must have an identifier/primary key.
Reading the forums I add #ORM/Id to ActionTaskReview entity and then it updates the DB without any errors. However when I look at the postgres table only the document_id field is there not the action_task_id field. What I am doing wrong as the document_id does not throw the same error when I exclude #ORM/Id?
My Doctrine entities look like:
ActionReview.php
/**
* #ORM\Entity
* #ORM\Table(name="actions_reviews")
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="owner", type="string")
* #ORM\DiscriminatorMap({"document":"AppBundle\Entity\DocumentActionReview","action_task":"AppBundle\Entity\ActionTaskReview"})
* #Discriminator(field = "owner", map = {"document": "AppBundle\Entity\DocumentActionReview", "action_task": "AppBundle\Entity\ActionTaskReview"})
*/
abstract class ActionReview
{
/**
* #ORM\Id
* #ORM\Column(type="guid")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="datetime", nullable=true)
*/
private $review_date;
/**
* #ORM\Column(type="text", nullable=true)
*/
private $review_description;
/**
* #ORM\Column(type="boolean", nullable=true)
*/
private $is_effective;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Action", inversedBy="action_review")
* #ORM\JoinColumn(name="action_id", referencedColumnName="id")
*/
private $action;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Employee")
* #ORM\JoinColumn(name="review_to", referencedColumnName="id")
*/
private $review_to;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Employee")
* #ORM\JoinColumn(name="review_by", referencedColumnName="id")
*/
private $review_by;
/**
* Get id
*
* #return guid
*/
public function getId()
{
return $this->id;
}
/**
* Set reviewDate
*
* #param \DateTime $reviewDate
*
* #return ActionReview
*/
public function setReviewDate($reviewDate)
{
$this->review_date = $reviewDate;
return $this;
}
/**
* Get reviewDate
*
* #return \DateTime
*/
public function getReviewDate()
{
return $this->review_date;
}
/**
* Set reviewDescription
*
* #param string $reviewDescription
*
* #return ActionReview
*/
public function setReviewDescription($reviewDescription)
{
$this->review_description = $reviewDescription;
return $this;
}
/**
* Get reviewDescription
*
* #return string
*/
public function getReviewDescription()
{
return $this->review_description;
}
/**
* Set isEffective
*
* #param boolean $isEffective
*
* #return ActionReview
*/
public function setIsEffective($isEffective)
{
$this->is_effective = $isEffective;
return $this;
}
/**
* Get isEffective
*
* #return boolean
*/
public function getIsEffective()
{
return $this->is_effective;
}
/**
* Set action
*
* #param \AppBundle\Entity\Action $action
*
* #return ActionReview
*/
public function setAction(\AppBundle\Entity\Action $action = null)
{
$this->action = $action;
return $this;
}
/**
* Get action
*
* #return \AppBundle\Entity\Action
*/
public function getAction()
{
return $this->action;
}
/**
* Set reviewTo
*
* #param \AppBundle\Entity\Employee $reviewTo
*
* #return ActionReview
*/
public function setReviewTo(\AppBundle\Entity\Employee $reviewTo = null)
{
$this->review_to = $reviewTo;
return $this;
}
/**
* Get reviewTo
*
* #return \AppBundle\Entity\Employee
*/
public function getReviewTo()
{
return $this->review_to;
}
/**
* Set reviewBy
*
* #param \AppBundle\Entity\Employee $reviewBy
*
* #return ActionReview
*/
public function setReviewBy(\AppBundle\Entity\Employee $reviewBy = null)
{
$this->review_by = $reviewBy;
return $this;
}
/**
* Get reviewBy
*
* #return \AppBundle\Entity\Employee
*/
public function getReviewBy()
{
return $this->review_by;
}
}
ActionTaskReview.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Exclude;
/**
* ActionTaskReview
* #ORM\Entity
*/
class ActionTaskReview
{
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\ActionTask", inversedBy="action_review")
* #ORM\JoinColumn(name="action_task_id", referencedColumnName="id") \AppBundle\Entity\ActionTask
*/
private $action_task;
/**
* Set actionTask
*
* #param \AppBundle\Entity\ActionTask $actionTask
*
* #return ActionTaskReview
*/
public function setActionTask(\AppBundle\Entity\ActionTask $actionTask = null)
{
$this->action_task = $actionTask;
return $this;
}
/**
* Get actionTask
*
* #return \AppBundle\Entity\ActionTask
*/
public function getActionTask()
{
return $this->action_task;
}
}
DocumentActionReview.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="documents_actions_reviews")
*/
class DocumentActionReview extends \AppBundle\Entity\ActionReview
{
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Document", inversedBy="action_review")
* #ORM\JoinColumn(name="document_id", referencedColumnName="id")
*/
private $document;
/**
* Set document
*
* #param \AppBundle\Entity\Document $document
*
* #return DocumentActionReview
*/
public function setDocument(\AppBundle\Entity\Document $document = null)
{
$this->document = $document;
return $this;
}
/**
* Get document
*
* #return \AppBundle\Entity\Document
*/
public function getDocument()
{
return $this->document;
}
}
First of all, your ActionTaskReview should extend ActionReview:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Exclude;
/**
* ActionTaskReview
* #ORM\Entity
*/
class ActionTaskReview extends ActionReview
{
...
}
Than I am not sure what you want to achieve, as you are using Single Table Inheritance but are setting the #ORM\Table(name="documents_actions_reviews") annotation on the DocumentActionReview class. Either switch to Class Table Inheritance if you need own tables for your inherited entities or remove the annotation.
Every entity class must have an identifier/primary key. You can select the field that serves as the identifier with the #Id annotation.
`/**
* #ORM\Column(type="integer")
* #ORM\Id
* #GeneratedValue
*/
private $id;`

How to implement multiple file upload in TYPO3 Front End Extension

My requirement is to implement a multiple fileupload field in TYPO3 Front-end Extension. Here is what I've used for a single file upload.
My Fields in Model
/**
* angebotuploaden
*
* #var \TYPO3\CMS\Extbase\Domain\Model\FileReference
*/
protected $angebotuploaden = NULL;
/**
* Returns the angebotuploaden
*
* #return \TYPO3\CMS\Extbase\Domain\Model\FileReference $angebotuploaden
*/
public function getAngebotuploaden() {
return $this->angebotuploaden;
}
/**
* Sets the angebotuploaden
*
* #param \TYPO3\CMS\Extbase\Domain\Model\FileReference $angebotuploaden
* #return void
*/
public function setAngebotuploaden(\TYPO3\CMS\Extbase\Domain\Model\FileReference $angebotuploaden) {
$this->angebotuploaden = $angebotuploaden;
}
Now I face issues in implementing multiple file-uploads for this field. Please help me to sort it out.
Use ObjectStorage to get an 1:n-Relation to the FileReference model. In your model that could look like this:
/**
* uploadFiles
*
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
* #cascade remove
*/
protected $uploadFiles = NULL;
/**
* __construct
*/
public function __construct() {
//Do not remove the next line: It would break the functionality
$this->initStorageObjects();
}
/**
* Initializes all ObjectStorage properties
* Do not modify this method!
* It will be rewritten on each save in the extension builder
* You may modify the constructor of this class instead
*
* #return void
*/
protected function initStorageObjects() {
$this->uploadFiles = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
}
/**
* Adds a UploadFile
*
* #param \TYPO3\CMS\Extbase\Domain\Model\FileReference $uploadFile
* #return void
*/
public function addUploadFile(\TYPO3\CMS\Extbase\Domain\Model\FileReference $uploadFile) {
$this->uploadFiles->attach($uploadFile);
}
/**
* Removes a UploadFile
*
* #param \TYPO3\CMS\Extbase\Domain\Model\FileReference $uploadFileToRemove The UploadFile to be removed
* #return void
*/
public function removeUploadFile(\TYPO3\CMS\Extbase\Domain\Model\FileReference $uploadFileToRemove) {
$this->uploadFiles->detach($uploadFileToRemove);
}
/**
* Returns the uploadFiles
*
* #return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $uploadFiles
*/
public function getUploadFiles() {
return $this->uploadFiles;
}
/**
* Sets the uploadFiles
*
* #param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $uploadFiles
* #return void
*/
public function setUploadFiles(\TYPO3\CMS\Extbase\Persistence\ObjectStorage $uploadFiles) {
$this->uploadFiles = $uploadFiles;
}
There're still more things to do, especially in TCA, but I don't know them in detail because I didn't use that yet. See Hemult Hummels Upload Example an this question for more detailed information.

Editing Slug using Gedmo TreeSlugHandler

I have an entity that is hierarchical using the Gedmo Tree Doctrine extension in Symfony 2. The code for the Category entity is:
<?php
namespace MD\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use MD\Entity\Extension\Treeable;
/**
* Category
*
* #ORM\Entity(repositoryClass="MD\Entity\Repository\CategoryRepository")
*
* #Gedmo\Tree(type="nested")
*/
class Category
{
/**
* Entity Extensions
*/
use Treeable;
/**
* The ID of the category
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* The title of the category
*
* #var string
*
* #ORM\Column(type="string", length=255)
*
* #Assert\NotBlank(message="category.title.not_blank")
* #Assert\Length(
* max=255,
* maxMessage="category.title.length.max"
* )
*/
protected $title;
/**
* The description of the category
*
* #var string
*
* #ORM\Column(type="text")
*
* #Assert\NotBlank(message="category.description.not_blank")
*/
protected $description;
/**
* The parent of the category
*
* #var Category
*
* #ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* #ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*
* #Gedmo\TreeParent
*/
protected $parent;
/**
* The children of the category
*
* #var ArrayCollection
*
* #ORM\OneToMany(targetEntity="Category", mappedBy="parent", cascade={"persist"})
* #ORM\OrderBy({"left" = "ASC"})
*/
protected $children;
/**
* The slug of the category
*
* #var string
*
* #ORM\Column(type="string", length=255, unique=true)
*
* #Gedmo\Slug(handlers={
* #Gedmo\SlugHandler(class="Gedmo\Sluggable\Handler\TreeSlugHandler", options={
* #Gedmo\SlugHandlerOption(name="parentRelationField", value="parent"),
* #Gedmo\SlugHandlerOption(name="separator", value="/")
* })
* }, fields={"title"})
*/
protected $slug;
/**
* Constructor
*/
public function __construct()
{
$this->children = new ArrayCollection();
}
/**
* Get the ID of the category
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set the title of the category
*
* #param string $title
*
* #return $this
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get the title of the category
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set the description of the category
*
* #param string $description
*
* #return $this
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get the description of the category
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set the parent of the category
*
* #param Category $parent
*
* #return $this
*/
public function setParent(Category $parent = null)
{
$this->parent = $parent;
if (null !== $parent) {
$parent->addChild($this);
}
return $this;
}
/**
* Get the parent of the category
*
* #return Category
*/
public function getParent()
{
return $this->parent;
}
/**
* Add a child to the category
*
* #param Category $child
*
* #return $this
*/
public function addChild(Category $child = null)
{
if (!$this->children->contains($child)) {
$this->children->add($child);
$child->setParent($this);
}
return $this;
}
/**
* Get the children of the category
*
* #return ArrayCollection
*/
public function getChildren()
{
return $this->children;
}
/**
* Set the slug of the category
*
* #param string $slug
*
* #return $this
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get the slug of the category
*
* #return string
*/
public function getSlug()
{
return $this->slug;
}
/** Magic Methods */
/**
* Return a string representation of the category
*
* #return string
*/
public function __toString()
{
return $this->getTitle();
}
}
Given a category with a title of Bands and a sub-category with a title of Rock, the latter category, on creation, has a slug of bands/rock. This works as expected.
When I add the slug field to a form, however, when I edit the entity, I initially get bands/rock added to the form field. If I change this to bands/rock-and-roll and submit the form, the slug gets updated to bands/bands-rock-and-roll and not bands/rock-and-roll as I'd expect.
If I edit the category and set the slug field to rock-and-roll, then submit the form, the slug gets updated to bands/rock-and-roll. I'd expect the slug to be rock-and-roll after update.
What do I need to do to fix this behaviour? I essentially want the slug to be auto-generated with the handler if it's not provided, but to be set to exactly what I provide if I do provide it.
Thanks
looking at the docs of the Gedmo Tree Doctrine extension and it's relative code, it's not a wrong behaviour because the slug is composed accordingly with the "TreeSlugHandler" method = parentFieldName/field-You-Have-Inserted (that happens every time you edit the slug).
If you need at the same moment of a slug for a specific category and of another that follows the category tree you can add another property (ex: cat_slug) with the simple annotation: #Gedmo\Slug(fields={"fieldYouWantToSlug"}).
Remember that every time (using the "TreeSlugHandler" method) you edit the slug (changing the precedent), every subcategory will be updated with the new slug.
I hope I was helpful

Doctrine 2 Modular App With Entity Replacement

I'm building an application using ZF2 and Doctrine2.
The ideia is to have a base app entity (lets call it UserEntity).
But in one Module A, I will have another UserEntity-like entity that will "upgrade" the base one, with new fields. And another Module B that will add more fields.
Ex:
BaseUserEntity {
protected $id;
// ...
}
ModuleAUserEntity extends BaseUserEntity {
protected moduleAId;
}
ModuleBUserEntity extends BaseUserEntity {
protected moduleBUserName;
}
Is it possible, somehow, to get a method so when I call UserEntity, it will return the full, upgraded-by-module, entity? Ex:
UserEntity {
protected $id;
// ...
protected moduleAId;
protected moduleBUserName;
}
Is there another way to achieve something like this? The possibility to "extension" of an entity?
I have 2 different approaches:
1.First one:
you should take a look at:
http://docs.doctrine-project.org/en/latest/reference/inheritance-mapping.html
thats the legitimate and recommend by doctrine way of doing this.
2. Second one
If you dont want doctrine to mess with the database, you can use a different aproach:
Create an interface that defines the common behavior of all classes
Write your base class, implementing that behavior.
write your child classes, implementing the interface, containing the new methods, and wrapping an instance of the base class
You implement the methods that are defined in the interface, but since they are already implemented in the parent class, you just bypass the call to the wrapped object.
So, you are using composition over inheritance to avoid doctrine (and probably you) to get crazy
In order to have a really clean behavior with doctrine, the database that i imagine is:
a table with the parent entity
a table with the child entity, containing
a foreign key with the id of the related parent entity (this is, the row in the parent table that contains the values asociated to this, since the child has to have the parent and the child fields)
all the extra columns
For instance:
Interface:
namespace DBAL\Entity;
interface IProfesional
{
public function setName($name);
public function getName();
public function getId();
}
Parent class:
namespace DBAL\Entity;
use Doctrine\ORM\Mapping as ORM;
use DBAL\Entity\User\IUserAware;
/**
* Profesional
*
* #ORM\Table(name="profesional")
* #ORM\Entity
*/
class Profesional implements IProfesional
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=45, nullable=true)
*/
private $name;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* Set nombre
*
* #param string $nombre
* #return Profesional
*/
public function setName($nombre)
{
$this->name = $nombre;
return $this;
}
/**
* Get nombre
*
* #return string
*/
public function getNombre()
{
return $this->name;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
child class:
namespace DBAL\Entity;
use DBAL\Entity\User\IUserAware;
use Doctrine\ORM\Mapping as ORM;
/**
* Jugador
*
* #ORM\Table(name="jugador")
* #ORM\Entity(repositoryClass="DBAL\Repository\JugadorRepository")
*/
class Player implements IProfesional
{
/**
* #var integer
*
* #ORM\Column(name="profesional_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Profesional
*
* #ORM\OneToOne(targetEntity="Profesional")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="profesional_id", referencedColumnName="id", unique=true)
* })
*/
private $profesional;
/**
* Constructor: you create an empty Profesional,
so you are sure you will never use a reference to an in-existent object.
But anyways, if this is an entity loaded from the database by doctrine,
doctrine will fill that field whith an actual professional from the parent table,
based in the foreign key id
*/
public function __construct()
{
if(!isset($id)){
$this->profesional=new Profesional();
}
}
/**
* Set profesional
*
* #param \Profesional $profesional
* #return Jugador
*/
public function setProfesional( Profesional $profesional = null)
{
$this->profesional = $profesional;
return $this;
}
/**
* Get profesional
*
* #return \Profesional
*/
public function getProfesional()
{
return $this->profesional;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->profesional->getId();
}
///New fields, for instance:
/**
* #var integer
*
* #ORM\Column(name="height", type="integer", nullable=true)
*/
private $height;
public function getHeight()
{
return $this->height;
}
public function setHeight($h)
{
$this->height=$h;
}
}

EntityNotFoundException in doctrine 2 proxy class

What are the possible causes of EntityNotFoundException while accessing the proxy class properties in doctrine 2? Anyway, the following is my entities' structure:
/**
* #ORM\Table(name="comments")
*
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="comment_type", type="smallint")
* #ORM\DiscriminatorMap({
* 1 = "VisitorComment",
* 2 = "MemberComment"
* })
*/
class Comment
{
//with common properties of its subclasses
}
subclasses are as follows:
/**
* #ORM\Table(name="member_comments")
*/
class MemberComment extends Comment
{
/**
* owning side
*
* #var Member $author
*
* #ORM\ManyToOne(targetEntity="Member")
* #ORM\JoinColumn(name="author_id", referencedColumnName="id", nullable=false)
*/
private $author;
/**
* Set author
*
* #param Member $author
*/
public function setAuthor($author)
{
$this->author = $author;
}
/**
* Get author
*
* #return Member
*/
public function getAuthor()
{
return $this->author;
}
}
/**
* #ORM\Table(name="visitor_comments")
*/
class VisitorComment extends Comment
{
/**
* owning side
*
* #var Visitor $author
*
* #ORM\ManyToOne(targetEntity="Visitor")
* #ORM\JoinColumn(name="author_id", referencedColumnName="id", nullable=false)
*/
private $author;
/**
* Set author
*
* #param string $author
*/
public function setAuthor($author)
{
$this->author = $author;
}
/**
* Get author
*
* #return Visitor
*/
public function getAuthor()
{
return $this->author;
}
}
The exception occurs when I call $comment->getAuthor()->getFirstName() <assuming that author which is either a Member or Visitor entity has firstName property>. The getAuthor() in this case returns a proxy class of either VisitorProxy or MemberProxy.
Kindly help me. I'm still new to doctrine.
As Floricel found out, this can be caused by an invalid foreign key in a column that's points to the table that the Proxy class references.
#Dave Lancea is right I changed a FK to not Null and then started getting this error, manually updated a broken record, making it point to an existing entity and problem gone.