Formats for .edi, .seq and .xls - symfony

I'm using the Form Builder from Symfony and the class I created for the Form is like the next:
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
class Upload
{
/**
* #Assert\File(
* maxSize = "4000k",
* mimeTypes = {"text/plain", "text/xml", "application/xml", "text/csv",
* "application/EDI-consent", "application/EDIFACT", "application/EDI-X12"
* "application/vnd.mseq", "application/json-seq",
* "application/vnd.sealed-xls",
* "application/pdf", "application/x-pdf",
* "application/zip"},
* mimeTypesMessage = "Please upload a valid format file"
* )
* #var file
*/
protected $file;
...
I need to accept these next extensions:
'txt,dat,xml,csv,edi,seq,xls,pdf,zip'
Is enough like this? Is anything left to add? I'm wrong with something?
Symfony Docs recommend to get the results from here:
https://www.iana.org/assignments/media-types/media-types.xhtml

In my opinion it should work properly. Maybe you should add application/octet-stream and zz-application/zz-winassoc-dat to match also dat file extension. And... test your code with different files in order to see if it works.

Related

Swagger annotation for query parameters for file upload

I am trying to make an api documentation for my symfony based api and I would like to add info about the upload endpoint for my api. I am using nelmioapidocbundle. Can anyone please provide a link to the documentation where I can find it? or better if there is an example. Thank you.
/**
* #Route("/products/{id}/images", methods={"POST"})
*
* #SWG\Parameter(
* name="file",
* in="body",
* type="file",
* ---- Im not sure what to put here to make this work
* )
*
* #SWG\Response(
* response=201,
* description="File is uploaded."
* )
* )
*/
public function uploadImages(Request $request, FileUploadService $fileUploadService) {}
For anyone who might encounter this. this formData in value fixed the problem:
* #SWG\Parameter(
* name="file",
* in="formData", <----
* required=true,
* type="file",
* description="product image"

Doctrine arrayCollections and relationship

I'm quite new with Doctrine, so I hope someone can help me or redirect me to the good documentation page.
I'm building an app with two entity (I reduce for explanations) :
- Tender
- File
For each tender, we can have one or more files. So I made the following objects.
Tender:
<?php
namespace TenderBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="tender")
*/
class Tender
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $tender_id;
/**
* #ORM\Column(type="array")
* #ORM\ManyToOne(targetEntity="File", inversedBy="tenders")
* #ORM\JoinColumn(name="tender_files", referencedColumnName="file_id")
*/
private $tender_files;
}
File:
<?php
namespace TenderBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* #ORM\Entity
* #ORM\Table(name="file")
*/
class File
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $file_id;
/**
* #ORM\OneToMany(targetEntity="Tender", mappedBy="tender_files", cascade={"persist", "remove"})
*/
private $file_tender;
}
First question: is it the right way to do this?
(of course, i've created the methods to get and set attributes, but they're basic).
When I persist each of my File object i'm trying to add then to my Tender instance. But to do this, I need to make $tender_files public and do this:
$tender->tender_files[]
This is not a viable solution for me because I need all my fields are private and I want to recover my object when I try to call this:
$this->getDoctrine()->getManager()->getRepository('TenderBundle:Tender')->find($id)->getTenderFiles()->getFileName();
So, I'm explaining and asking to find the right way to do what I want. I hope what i need is clear and i'm here to answers questions or show more code if needed.
Thanks!
Like Richard has mentioned, you're missing getters and setters which are declared to be public. They'll have access to your private variables. The quick way to do this with symfony:
php app/console doctrine:generate:entities
It'll generate something like this:
public function addTenderFile(\TenderBundle\Entity\File $file)
{
$this->tender_files[] = $file;
return $this;
}
/**
* Remove
*/
public function removeTenderFile(\TenderBundle\Entity\File $file)
{
$this->tender_files->removeElement($file);
}
/**
* Get
*/
public function getTenderFiles()
{
return $this->tender_files;
}
It's good practice if you're a beginner to see how your code lines up with the auto generator. Once you understand what's going on, just let the generator do the grunt work.
You should have a setter and getter in your File entity similar to this:
public function setTender(\Your\Namespace\Tender $tender)
{
$this->tender = $tender;
return $this;
}
public function setTender()
{
return $this->tender;
}
So when you instance (or create) File, you can go like so:
$file = new File(); // or file fetched from DB, etc.
// here set $file properties or whatever
$tender->setFile($file);
$entityManager->persist($tender);
$entityManager->flush();
Then your tender will be properly associated with your file.
Similarly from the File end, you should be able to do:
$file->addTender($tender);
$entityManager->persist($file);
$entityManager->flush();
And your tender will be added to your File->tenders collection.
For more information the documentation is very useful and has more or less everything you need to get started.
Also, save yourself manually creating getters and setters by using generate:doctrine:entity
This is incorrect:
/**
* #ORM\Column(type="array")
* #ORM\ManyToOne(targetEntity="File", inversedBy="tenders")
* #ORM\JoinColumn(name="tender_files", referencedColumnName="file_id")
*/
private $tender_files;
You can't persist an array to your database. A database row is one entity and it's corresponding attributes. If a tender can have many files, then this relationship should be:
* #ORM\OneToMany
Likewise for the File entity. If many files can have one Tender then it's relationship should be:
* #ORM\ManyToOne
For relationship mapping using Doctrine, it's helpful to read left-to-right with the entity YOU'RE CURRENTLY IN being on the left, and the entity you're setting as a variable being on the right.
If you're in Tender reading left-to-right Tender may have "OneToMany" files. And File(s) may have ManyToOne Tender. Doctrine Association Mapping

how to set the routing translation based on annotation?

All the routing of my website is realized based on the annotations. Now, I want to translate my routing. To realize that, I tried to use the bundle JMSI18nRoutingBundle.
Nevetheless, the documentation does not give any example how to specify the route for each locale.
This is an action with its routing, how to translate it?
/**
* #Route("/welcome", name="welcome")
* #Template()
*/
public function welcomeAction() {
return array();
}
Thanks,
Question after being edited
/**
* #Route("/welcome", name="welcome", defaults={"_locale" = "en"})
* #Route("/bienvenue", name="welcome", defaults={"_locale" = "fr"})
* #Route("/willkommen", name="welcome", defaults={"_locale" = "de"})
* #Template()
*/
public function welcomeAction() {
return array();
}
Now, what is happening with this new annotations:
the selected route is always the last one which is /willkommen (if you change the order the routes, the selected route is still the last one)
the _locale is set the the locale of the last route which 'de' according to the annotation above.
So, any proposal?
Thanks...
I found the solution. You just have to set run the following command
php app/console translation:extract fr --bundle=MinnTestBundle
--enable-extractor=jms_i18n_routing --output-format=yml
Then, minn/TestBundle/Ressources/translations/routes.fr.yml file will be generated. Customize you route translations & that is it!
Hope it will help others...
You can add multiple route annotations.
/**
* #Route("/welcome", name="welcome", defaults={"_locale" = "en"})
* #Route("/bienvenue", name="welcome", defaults={"_locale" = "fr"})
* #Template()
*/

Symfony 2 - FOSCommentBundle : add attachment feature

I need to add functionality to attach a file to a comment. The upload of the image would automatically when the user drops the file into the form (in the same way that you attach a file with gmail). My question is how do I do to find the file that was previously sent to the server when the comment is submitted and to delete the comment if the document is never submitted.
Do any of you have already done something similar?
Here's the association I have between my classes Comment and Document.
class Comment extends BaseComment
{
/** ... */
/**
* #ORM\ManyToMany(targetEntity="Document", cascade={"persist","remove"})
* #ORM\JoinTable(name="fls_comment_and_documents",
* joinColumns={#ORM\JoinColumn(name="comment_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="document_id", referencedColumnName="id", unique=true)}
* )
*
* #var ArrayCollection $documents
*/
protected $documents;
/** ... */
}
Thanks in advance !

Doxygen and #ORM annotated entities in Symfony2

I have implemented entities within the Symfony2 framework that are annotated to be used by Doctrine. For example:
/*
* class description
*
* #ORM\Entity(repositoryClass="Website\ContentBundle\Repository\ContentRepository")
* #ORM\HasLifecycleCallbacks()
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="type", type="string")
* #ORM\DiscriminatorMap({"article" = "Article"})
* #ORM\Table(name="content",indexes={#ORM\index(name="id_UNIQUE",columns={"id"})})
*/
class Content {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
...
}
When I run Doxygen on my source code, the documentation is not very readable. I have tried to define aliases for each #ORM* symbol; for example "ORM=ORM", "Entity=Entity", and so on. But this does not work. For the above mentioned class Doxygen returns
...
ORMEntity(repositoryClass="Website\ContentBundle\Repository\ContentRepository") ORM() ORM("SINGLE_TABLE") ORM(name="type", type="string") ORM({"article" = "Article", "picture_series" = "PictureSeries", "event" = "Event", "invitation" = "Invitation"}) ORMTable(name="content",indexes={ORM(name="id_UNIQUE",columns={"id"})})
With respect to method
/**
* sets the given id
*
* #param number $id
* #return \Website\ContentBundle\Entity\Content
*/
public function setId($id) {
$this->id = $id;
return $this; // fluent interface
}
Doxygen creates
setId ($ id)
sets the given id
Parameters:
number $id
Returns:
Why does it not show the \Website\ContentBundle\Entity\Content after "Returns:"?
Maybe someone can give me a hint or a link on how to configure Doxygen such that it can handle the #ORM annotations appropriately.
THX in advance!
With respect to the question
Why does it not show the \Website\ContentBundle\Entity\Content after Returns:?
This is probably because doxygen commands start with a \, do doxygen thinks you are calling some commands which it does not recognise, and so presumably strips from the documentation and does nothing with.
You were on the right line with attempting to use the ALIASES configuration file option. However, instead of defining ORM=ORM try using ORM=\#ORM. I got your example source code to be documented by doxygen without any warnings by defining the following aliases:
ALIASES = "ORM=\#ORM"
ALIASES += "Entity=\\Entity"
ALIASES += "InheritanceType=\\InheritanceType"
ALIASES += "DiscriminatorColumn=\\DiscriminatorColumn"
ALIASES += "DiscriminatorMap=\\DiscriminatorMap"
ALIASES += "Table=\\Table"
ALIASES += "Id=\\Id"
ALIASES += "Column=\\Column"
ALIASES += "GeneratedValue=\\GeneratedValue"
ALIASES += "index=\\index"
ALIASES += "HasLifecycleCallbacks=\\HasLifecycleCallbacks"
ALIASES += "Repository=\\Repository"
ALIASES += "ContentRepository=\\ContentRepository"
ALIASES += "ContentBundle=\\ContentBundle"
ALIASES += "Website=\\Website"
ALIASES += "Content=\\Content"
Here \\, \# are actually doxygen commands for printing the backslash \ and # characters respectively.
Note, however, that the #ORM... directives will all appear on the same line. I'm not sure how to avoid this (update: see my edit below). Does anyone else have any ideas?
Finally, as a side note, your documentation for $id should be
* #param $id number
note the order of $id and number. See the documentation for \param.
Edit: An alternative way of doing this would be to wrap the Doctrine relavent parts in \verbatim and \endverbatim tags. This will preserve the line breaks in your class description which will be more readable.

Resources