I have the next entities
AppBundle/Entity/User.php
namespace AppBundle\Entity;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
* #ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* #ORM\Table(name="fos_user_user")
*
*/
class User extends BaseUser
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\OneToMany(targetEntity="SmsHistory", mappedBy="user", cascade={"persist"}, orphanRemoval=true)
*/
private $smsHistory;
public function __construct()
{
parent::__construct();
$smsHistory = new ArrayCollection;
}
/**
* Get id
*
* #return int $id
*/
public function getId()
{
return $this->id;
}
/**
* #param \Doctrine\Common\Collections\ArrayCollection $smsHistory
*/
public function setSmsHistory($smsHistory){
if (count($smsHistory) > 0) {
foreach ($smsHistory as $i) {
$this->addSmsHistory($i);
}
}
return $this;
}
/**
* Add smsHistory
*
* #param \AppBundle\Entity\SmsHistory $smsHistory
*
* #return User
*/
public function addSmsHistory(\AppBundle\Entity\SmsHistory $smsHistory)
{
$smsHistory->setUser($this);
$this->smsHistory->add($smsHistory);
}
/**
* Remove smsHistory
*
* #param \AppBundle\Entity\SmsHistory $smsHistory
*/
public function removeSmsHistory(\AppBundle\Entity\SmsHistory $smsHistory)
{
$this->smsHistory->removeElement($smsHistory);
}
/**
* Get smsHistory
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getSmsHistory()
{
return $this->smsHistory;
}
AppBundle/Entity/SmsHistory.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* SmsHistory
*
* #ORM\Table(name="sms_history")
* #ORM\Entity(repositoryClass="AppBundle\Repository\SmsHistoryRepository")
*/
class SmsHistory
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="smsHistory")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* #ORM\ManyToOne(targetEntity="Contact", inversedBy="smsHistory")
* #ORM\JoinColumn(name="contact_id", referencedColumnName="id")
*/
private $contact;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set user
*
* #param \AppBundle\Entity\User $user
*
* #return SmsHistory
*/
public function setUser(\AppBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set contact
*
* #param \AppBundle\Entity\Contact $contact
*
* #return SmsHistory
*/
public function setContact(\AppBundle\Entity\Contact $contact = null)
{
$this->contact = $contact;
return $this;
}
/**
* Get contact
*
* #return \AppBundle\Entity\Contact
*/
public function getContact()
{
return $this->contact;
}
AppBundle/SmsHistory/Contact.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Contact
*
* #ORM\Table(name="contact")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ContactRepository")
*/
class Contact
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="contact")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* #ORM\OneToMany(targetEntity="SmsHistory", mappedBy="contact", cascade={"persist"}, orphanRemoval=true)
*/
private $smsHistory;
public function __construct() {
$smsHistory = new ArrayCollection;
}
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set user
*
* #param \AppBundle\Entity\User $user
*
* #return Contact
*/
public function setUser(\AppBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Add smsHistory
*
* #param \AppBundle\Entity\SmsHistory $smsHistory
*
* #return User
*/
public function addSmsHistory(\AppBundle\Entity\SmsHistory $smsHistory)
{
$smsHistory->setContact($this);
$this->smsHistory->add($smsHistory);
}
/**
* Remove smsHistory
*
* #param \AppBundle\Entity\SmsHistory $smsHistory
*/
public function removeSmsHistory(\AppBundle\Entity\SmsHistory $smsHistory)
{
$this->smsHistory->removeElement($smsHistory);
}
/**
* Get smsHistory
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getSmsHistory()
{
return $this->smsHistory;
}
All entities are relationed with others.
In my UserAdmin i added in configureFormFields the field for add Contact and for add SmsHistory:
->add('contact', 'sonata_type_collection', array(
'cascade_validation' => true,
'by_reference' => true,
), array(
'edit' => 'inline',
'inline' => 'table',
))
->add('pushHistory', 'sonata_type_collection', array(
'cascade_validation' => true,
'by_reference' => true,
), array(
'edit' => 'inline',
'inline' => 'table',
))
In SmsHistoryAdmin i added the Contact field, to select a Contact:
->add('contact','sonata_type_model')
When I add a SmsHistory from UserAdmin, I want to display only Contact relationated with the current User i am editing, but all Contact of all User are displayed.
How I can do this?
Thank you!
I got the solution, I hope help someone.
In SmsHistoryAdmin, change this line:
->add('contact','sonata_type_model', array())
With this:
->add('contact', null, [
'query_builder' => $this->getAllowedContactQueryBuilder(),
])
And add this functions:
/**
* #return \Doctrine\Common\Persistence\ObjectManager|object
*/
protected function getEntityManager()
{
return $this->getContainer()->get('doctrine')->getManager();
}
/**
* #return null|\Symfony\Component\DependencyInjection\ContainerInterface
*/
protected function getContainer()
{
return $this->getConfigurationPool()->getContainer();
}
/**
* #return mixed
*/
private function getAllowedContactQueryBuilder()
{
if (!$this->getSubject()) {
return null;
}
return $this->getContactRepository()
->getContactByUserQueryBuilder($this->getSubject()->getUser());
}
/**
* #return \Doctrine\Common\Persistence\ObjectRepository
*/
public function getContactRepository()
{
return $this->getEntityManager()->getRepository('AppBundle:Contact');
}
And now the entity is filtering the Contact relationated with User.
Related
I would like to use a controller for downloading files.
The request is coming via an entity url and an included custom controller.
In the invoke function the requested file will be sent to the browser.
The problem is that the swagger documentation will be shown in a browser, the output of the controller will be ignored.
Only via a postman request the file content will be shown.
A solution would be to disable swagger for this entity.
Entity:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\GetMediaObjectAction;
/**
* #ApiResource(
* attributes={
* "route_prefix"="/api"
* },
* collectionOperations={},
* itemOperations={
* "archiveapi_get_file"={
* "path"="/file/{company_id}/{id}/{filename}",
* "defaults"={"_api_receive"=false},
* "read"=false,
* "requirements" = {
* "company_id" = "\d+",
* "id" = ".+",
* "filename" = "(.+)\.(.+)",
* },
* }
* },
* normalizationContext={"groups"={"MediaObject:read"}}
* )
*/
class MediaObject
{
/**
* #var string|null
*
* #ApiProperty(identifier=true)
*/
protected $id = null;
/**
* #var string|null
*
* #ApiProperty(identifier=true)
*/
protected $companyId = null;
/**
* #var string|null
*
* #ApiProperty(identifier=true)
*/
protected $filename = null;
/**
* #return string|null
*/
public function getId(): ?string
{
return $this->id;
}
/**
* #param string|null $id
* #return MediaObject
*/
public function setId(?string $id): MediaObject
{
$this->id = $id;
return $this;
}
/**
* #return string|null
*/
public function getCompanyId(): ?string
{
return $this->companyId;
}
/**
* #param string|null $companyId
* #return MediaObject
*/
public function setCompanyId(?string $companyId): MediaObject
{
$this->companyId = $companyId;
return $this;
}
/**
* #return string|null
*/
public function getFilename(): ?string
{
return $this->filename;
}
/**
* #param string|null $filename
* #return MediaObject
*/
public function setFilename(?string $filename): MediaObject
{
$this->filename = $filename;
return $this;
}
}
Controller:
<?php
namespace App\Controller;
use App\Entity\MediaObject;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use function PHPUnit\Framework\throwException;
final class GetMediaObjectAction extends AbstractController
{
/**
* #Route(
* name="archiveapi_get_file",
* path="/file/{companyId}/{id}/{filename}",
* methods={"GET"},
* defaults={
* "_api_resource_class"=MediaObject::class,
* "_api_item_operation_name"="archiveapi_get_file",
* "_api_receive"=false,
* "_api_respond"=false
* }
* )
*/
public function __invoke(Request $request, $companyId, $id, $filename): BinaryFileResponse
{
$projectDir = $this->getParameter('kernel.project_dir');
$file = sprintf("%s/var/data/invoices_%s/%s/%s", $projectDir, $companyId, $id, $filename);
if (!file_exists($file)) {
throw new NotFoundHttpException('file not available');
}
return new BinaryFileResponse($file);
}
}
I have the following entities, form types and controller
Entity Client
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Client
*
* #ORM\Table(name="client")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ClientRepository")
*/
class Client
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* #ORM\OneToMany(targetEntity="Contacts", mappedBy="client", cascade={"persist"})
*/
private $contacts;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Client
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set address
*
* #param string $address
*
* #return Client
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Constructor
*/
public function __construct()
{
$this->contacts = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add contact
*
* #param \AppBundle\Entity\Contacts $contact
*
* #return Client
*/
public function addContact(\AppBundle\Entity\Contacts $contact)
{
$this->contacts[] = $contact;
return $this;
}
/**
* Remove contact
*
* #param \AppBundle\Entity\Contacts $contact
*/
public function removeContact(\AppBundle\Entity\Contacts $contact)
{
$this->contacts->removeElement($contact);
}
/**
* Get contacts
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getContacts()
{
return $this->contacts;
}
}
Contacts entity is as below
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Contacts
*
* #ORM\Table(name="contacts")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ContactsRepository")
*/
class Contacts
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="fullname", type="string", length=255)
*/
private $fullname;
/**
* #var int
*
* #ORM\Column(name="user_type", type="smallint")
*/
private $userType;
/**
* #ORM\ManyToOne(targetEntity="Client", inversedBy="contacts")
* #ORM\JoinColumn(name="client_id", referencedColumnName="id")
*/
private $client;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set fullname
*
* #param string $fullname
*
* #return Contacts
*/
public function setFullname($fullname)
{
$this->fullname = $fullname;
return $this;
}
/**
* Get fullname
*
* #return string
*/
public function getFullname()
{
return $this->fullname;
}
/**
* Set userType
*
* #param integer $userType
*
* #return Contacts
*/
public function setUserType($userType)
{
$this->userType = $userType;
return $this;
}
/**
* Get userType
*
* #return int
*/
public function getUserType()
{
return $this->userType;
}
/**
* Set client
*
* #param \AppBundle\Entity\Client $client
*
* #return Contacts
*/
public function setClient(\AppBundle\Entity\Client $client = null)
{
$this->client = $client;
return $this;
}
/**
* Get client
*
* #return \AppBundle\Entity\Client
*/
public function getClient()
{
return $this->client;
}
}
Client form type is as below
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Entity\Client;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
class ClientForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array('attr' => array('class' => 'form-control', 'placeholder' => 'Client Name')))
->add('address', TextType::class, array('attr' => array('class' => 'form-control', 'placeholder' => 'Address')))
->add('contacts', CollectionType::class, array(
// each entry in the array will be an "email" field
'entry_type' => ContactsForm::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
))
->add('save', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Client::class,
));
}
}
Contacts form type is as below
<?php
namespace AppBundle\Form;
use AppBundle\Entity\Contacts;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class ContactsForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('fullname', TextType::class, array('attr' => array('class' => 'form-control', 'placeholder' => 'Full Name')))
->add('user_type', TextType::class, array('attr' => array('class' => 'form-control', 'placeholder' => 'Co-Owner'))
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Contacts::class,
));
}
}
Finally the controller to store data in two different tables being client as main table and contacts having foreign ken as client_id from table client is as below
/
**
* #Route("/client/add", name="add_client")
*/
public function addClientAction(Request $request)
{
$client = new Client();
$form = $this->createForm(ClientForm::class, $client);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$client = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($client);
$em->flush();
$this->addFlash(
'notice',
'Client Added!'
);
return $this->redirectToRoute('homepage');
}
return $this->render('default/addClient.html.twig', array('form' => $form->createView()));
}
The problem here is the data is added in both tables but the foreign key of the added client is not stored in table contacts, the value is null
This solution seems to work for some people may be for older version of symfony
But not luck to me. How to insert the foreign id as well. Am new to symfony and I am using symfony 3.2
It seems for me like a little mess up.
I guess you should add a contact from ContactController addAction.
Than in your Contact form type add EntityType, so you can choose a client for your contact. Here is some examples: http://symfony.com/doc/current/reference/forms/types/entity.html
Or you can set client manually by simple $contact->setClient($client) before you presist.
PS: rename Contacts entity to Contact. It is a little confusing)
This is my first Symfony project, and I can't figure out how to solve my problem.
Basically, i'm trying to make an invoice using a form.
I have a "Facturation" (ie invoice) Entity, a "TypeOfService" Entity, and a "Service" Entity (the sole attribute of which is the quantity of the type of service needed for the invoice) that acts as an association class.
I'd like to dynamically add "New Service" fields to my FacturationType form using Javascript (probably AngularJS). So I have to create N new Service entities that each associate with both my Facturation entity and an existing TypeOfService entity.
Here's my Facturation entity:
use Doctrine\ORM\Mapping AS ORM;
/**
* Facturation
*
* #ORM\Table(name="facturation")
* #ORM\Entity
*/
class Facturation
{
...
/**
* #ORM\OneToMany(targetEntity="Service", mappedBy="facturation")
*/
private $service;
/**
* Add service
*
* #param \AppBundle\Entity\Service $service
* #return Facturation
*/
public function addService(\AppBundle\Entity\Service $service)
{
$this->service[] = $service;
return $this;
}
/**
* Remove service
*
* #param \AppBundle\Entity\Service $service
*/
public function removeService(\AppBundle\Entity\Service $service)
{
$this->service->removeElement($service);
}
/**
* Get service
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getService()
{
return $this->service;
}
}
Then Service:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* Service
*
* #ORM\Table(name="service")
* #ORM\Entity
*/
class Service
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="integer", nullable=true)
*/
private $quantity;
/**
* #ORM\ManyToOne(targetEntity="TypeOfService", inversedBy="service")
* #ORM\JoinColumn(name="type_of_service_id", referencedColumnName="id")
*/
private $typeOfService;
/**
* #ORM\ManyToOne(targetEntity="Facturation", inversedBy="service")
* #ORM\JoinColumn(name="facturation_id", referencedColumnName="id")
*/
private $facturation;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set quantity
*
* #param integer $quantity
* #return Service
*/
public function setQuantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Get quantity
*
* #return integer
*/
public function getQuantity()
{
return $this->quantity;
}
/**
* Set typeOfService
*
* #param \AppBundle\Entity\TypeOfService $typeOfService
* #return Service
*/
public function setTypeOfService(\AppBundle\Entity\TypeOfService $typeOfService = null)
{
$this->typeOfService = $typeOfService;
return $this;
}
/**
* Get typeOfService
*
* #return \AppBundle\Entity\TypeOfService
*/
public function getTypeOfService()
{
return $this->typeOfService;
}
/**
* Set facturation
*
* #param \AppBundle\Entity\Facturation $facturation
* #return Service
*/
public function setFacturation(\AppBundle\Entity\Facturation $facturation = null)
{
$this->facturation = $facturation;
return $this;
}
/**
* Get facturation
*
* #return \AppBundle\Entity\Facturation
*/
public function getFacturation()
{
return $this->facturation;
}
}
And finally TypeOfService
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* TypeOfService
*
* #ORM\Table(name="type_of_service")
* #ORM\Entity
*/
class TypeOfService
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(nullable=true)
*/
private $name;
/**
* #ORM\Column(type="integer", nullable=true)
*/
private $pricePerUnit;
/**
* #ORM\OneToMany(targetEntity="Service", mappedBy="typeOfService")
*/
private $service;
...
/**
* Constructor
*/
public function __construct()
{
$this->service = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add service
*
* #param \AppBundle\Entity\Service $service
* #return TypeOfService
*/
public function addService(\AppBundle\Entity\Service $service)
{
$this->service[] = $service;
return $this;
}
/**
* Remove service
*
* #param \AppBundle\Entity\Service $service
*/
public function removeService(\AppBundle\Entity\Service $service)
{
$this->service->removeElement($service);
}
/**
* Get service
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getService()
{
return $this->service;
}
}
Can anyone point me in the right direction ?
For anyone looking, I've cracked it.
This was what I was looking for:
http://symfony.com/doc/current/cookbook/form/form_collections.html
This allows for dynamic field additions, using JS.
Be careful to cascade your association. In my case:
class Facturation
{
....
/**
* #ORM\OneToMany(targetEntity="Service", mappedBy="facturation", cascade={"persist", "remove"})
*/
private $services;
Is there a way to handle on entity easily image with sonata media bundle?
I try all solution I find but nothing work....
Many problems :
- unable to delete image from collection
- unable to add more than one file each time
- unable to see thumbnails
This bundle is not easy to use, I think I will rewrite an another bundle I won't spend no more time.
I think I'm doing to the good thing :
<?php
namespace Immo\FichierBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Immeuble
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Immo\FichierBundle\Entity\ImmeubleRepository")
*/
class Immeuble
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="numero", type="integer")
*/
private $numero;
/**
* #var Immo\EtablissementBundle\Entity\Etablissement
*
* #ORM\OneToOne(targetEntity="Immo\EtablissementBundle\Entity\Etablissement", cascade={"persist"})
*/
private $agence;
/**
* #var Immo\FichierBundle\Entity\Adresse
*
* #ORM\OneToOne(targetEntity="Immo\FichierBundle\Entity\Adresse", cascade={"persist"})
*/
private $adresse;
/**
* #var string
*
* #ORM\Column(name="vente", type="string", length=255)
*/
private $vente;
/**
* #var string
*
* #ORM\Column(name="coproindiv", type="string", length=255)
*/
private $coproindiv;
/**
* #var string
*
* #ORM\Column(name="exregulier", type="string", length=255)
*/
private $exregulier;
/**
* #var string
*
* #ORM\OneToMany(targetEntity="Immo\FichierBundle\Entity\ImmeubleHasPhoto", mappedBy="immeuble",cascade={"all"})
* #ORM\OrderBy({"ordre"="ASC"})
*/
private $photos;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function __toString() {
return $this->getNumero().' '.$this->getAdresse();
}
/**
* Constructor
*/
public function __construct()
{
$this->photo = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set numero
*
* #param integer $numero
* #return Immeuble
*/
public function setNumero($numero)
{
$this->numero = $numero;
return $this;
}
/**
* Get numero
*
* #return integer
*/
public function getNumero()
{
return $this->numero;
}
/**
* Set agence
*
* #param Immo\EtablissementBundle\Entity\Etablissement $agence
* #return Immeuble
*/
public function setAgence(\Immo\EtablissementBundle\Entity\Etablissement $agence)
{
$this->agence = $agence;
return $this;
}
/**
* Get agence
*
* #return Immo\EtablissementBundle\Entity\Etablissement
*/
public function getAgence()
{
return $this->agence;
}
/**
* Set adresse
*
* #param Immo\FichierBundle\Entity\Adresse $adresse
* #return Immeuble
*/
public function setAdresse(\Immo\FichierBundle\Entity\Adresse $adresse)
{
$this->adresse = $adresse;
return $this;
}
/**
* Get adresse
*
* #return Immo\FichierBundle\Entity\Adresse
*/
public function getAdresse()
{
return $this->adresse;
}
/**
* Set vente
*
* #param string $vente
* #return Immeuble
*/
public function setVente($vente)
{
$this->vente = $vente;
return $this;
}
/**
* Get vente
*
* #return string
*/
public function getVente()
{
return $this->vente;
}
/**
* Set coproindiv
*
* #param string $coproindiv
* #return Immeuble
*/
public function setCoproindiv($coproindiv)
{
$this->coproindiv = $coproindiv;
return $this;
}
/**
* Get coproindiv
*
* #return string
*/
public function getCoproindiv()
{
return $this->coproindiv;
}
/**
* Set exregulier
*
* #param string $exregulier
* #return Immeuble
*/
public function setExregulier($exregulier)
{
$this->exregulier = $exregulier;
return $this;
}
/**
* Get exregulier
*
* #return string
*/
public function getExregulier()
{
return $this->exregulier;
}
/**
* Addphoto
*
* #param \Immo\FichierBundle\Entity\ImmeubleHasPhoto $photo
* #return Personne
*/
public function addPhotos(\Immo\FichierBundle\Entity\ImmeubleHasPhoto $photo)
{
$photo->setImmeuble($this);
$this->photos[] = $photo;
return $this;
}
/**
* Remove removePhoto
*
* #param \Immo\FichierBundle\Entity\ImmeubleHasPhoto $photo
*/
public function removePhotos(\Immo\FichierBundle\Entity\ImmeubleHasPhoto $photo)
{
$this->photos->removeElement($photo);
}
/**
* {#inheritdoc}
*/
public function setPhotos($photos)
{
$this->photos = new ArrayCollection();
foreach ($photos as $photo) {
$this->addPhoto($photo);
}
}
/**
* Get photo
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getPhotos()
{
return $this->photos;
}
}
The admin class :
/**
* #param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->tab('Général')
->with('Général')
->add('numero')
->add('agence')
->add('adresse','sonata_type_model_list')
->add('vente')
->add('coproindiv')
->add('exregulier')
->end()
->end()
->tab('Photos')
->with('Liste photos')
->add('photos', 'sonata_type_collection', array(
'label' => false,
'type_options' => array('delete' => true),
'cascade_validation' => true,
'btn_add' => 'Ajouter une photo',
"required" => false
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'ordre'
)
)
->end()
->end()
;
}
The join entity :
<?php
namespace Immo\FichierBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* ImmeubleHasPhoto
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Immo\FichierBundle\Entity\ImmeubleHasPhotoRepository")
*/
class ImmeubleHasPhoto
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="nom", type="string", length=255,nullable=true)
*/
private $nom;
/**
* #var integer
* #ORM\Column(name="ordre", type="integer")
*/
private $ordre;
/**
* #ORM\ManyToOne(targetEntity="Immeuble", inversedBy="photos",cascade={"all"},fetch="LAZY")
* #ORM\JoinColumn(name="immeuble_id", referencedColumnName="id", nullable=false)
*/
private $immeuble;
/**
* #var \Application\Sonata\MediaBundle\Entity\Media
* #ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media",cascade={"all"},fetch="LAZY")
* #ORM\JoinColumn(name="photo_id", referencedColumnName="id", nullable=false)
*/
private $photo;
public function __construct() {
}
public function __toString() {
return $this->getNom();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function setImmeuble(\Immo\FichierBundle\Entity\Immeuble $immeuble)
{
$this->immeuble = $immeuble;
return $this;
}
/**
* Get Immeuble
*
* #return \Immo\FichierBundle\Entity\Immeuble
*/
public function getImmeuble()
{
return $this->immeuble;
}
/**
* Set photo
*
* #param string $photo
* #return Application\Sonata\MediaBundle\Entity\Media
*/
public function setPhoto(\Application\Sonata\MediaBundle\Entity\Media $photo=null)
{
$this->photo = $photo;
return $this;
}
/**
* Get photo
*
* #return Application\Sonata\MediaBundle\Entity\Media
*/
public function getPhoto()
{
return $this->photo;
}
/**
* Set nom
*
* #param string $nom
* #return ImmeubleHasPhoto
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* #return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set ordre
*
* #param integer $ordre
* #return ImmeubleHasPhoto
*/
public function setOrdre($ordre)
{
$this->ordre = $ordre;
return $this;
}
/**
* Get ordre
*
* #return integer
*/
public function getOrdre()
{
return $this->ordre;
}
}
And finally the admin class for my join entity :
/**
* #param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$link_parameters = array();
if ($this->hasParentFieldDescription()) {
$link_parameters = $this->getParentFieldDescription()->getOption('link_parameters', array());
}
if ($this->hasRequest()) {
$context = $this->getRequest()->get('context', null);
if (null !== $context) {
$link_parameters['context'] = $context;
}
}
$formMapper
->add('nom',null,array('label'=>'Titre'))
->add('photo','sonata_type_model_list', array('required' => false), array(
'link_parameters' => array('context' => 'Photos_immeuble')
))
->add('ordre','integer')
;
}
Is there something wrong? I've passe many day to find why I'm unable to attache image but I found nothing.... I really think this bundle isn't functionnal....
Thanks for your help.
When doing a simple query in the main controller such as:
public function newAdCreatedAction($id)
{
$em = $this->getDoctrine()->getManager()->getRepository('websiteAdsBundle:AdsList');
$article = $em->findById($id);
return $this->render('websiteAdsBundle:Default:newAdCreated.html.twig',array('article'=>$article));
}
I've got a blank page in the log file I got the following message error (if that helps):
//Bunch of kernelerrors... here and then:
[2014-06-09 14:07:53] doctrine.DEBUG: SET NAMES UTF8 [] []
[2014-06-09 14:07:53] doctrine.DEBUG: SELECT t0.id AS id1, t0.username AS username2,t0.email AS email3, t0.telephone AS telephone4, t0.display_phone AS display_phone5, t0.title AS title6, t0.description AS description7, t0.country AS country8, t0.region AS region9, t0.department AS department10, t0.address AS address11, t0.city AS city12, t0.zip_code AS zip_code13, t0.status_pro AS status_pro14, t0.creationtime AS creationtime15, t0.updatetime AS updatetime16, t0.publication AS publication17 FROM AdsList t0 WHERE t0.id = ? ["53"] []
[2014-06-09 14:07:55] emergency.EMERGENCY: Allowed memory size of 134217728 bytes exhausted (tried to allocate 125573121 bytes) {"type":1,"file":"/home/alfonso/sites/ads.website.com/public_html/vendor/twig/twig/lib/Twig/Extension/Debug.php","line":66} []
I think this is related to the #ORM\Join statement but I can't figure out what is it.
I guess with bidirectional associations, Twig’s dump is not working
the website/AdsBundle/Entity/AdsList.php class:
<?php
namespace website\AdsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use website\AdsBundle\Entity\Photos;
/**
* AdsList
*
* #ORM\Table()
* #ORM\Entity
* #ORM\Entity(repositoryClass="website\AdsBundle\Entity\AdsListRepository")
* #ORM\HasLifecycleCallbacks()
*/
class AdsList
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="username", type="string", length=255)
*/
private $username;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="telephone", type="string", length=255)
*/
private $telephone;
/**
* #var string
*
* #ORM\Column(name="display_phone", type="string", length=255)
*/
private $displayPhone;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="description", type="text")
*/
private $description;
/**
* #var string
*
* #ORM\Column(name="country", type="string", length=255)
*/
private $country;
/**
* #var string
*
* #ORM\Column(name="region", type="string", length=255)
*/
private $region;
/**
* #var string
*
* #ORM\Column(name="department", type="string", length=255)
*/
private $department;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="city", type="string", length=255)
*/
private $city;
/**
* #var string
*
* #ORM\Column(name="zip_code", type="string", length=255)
*/
private $zipCode;
/**
* #var string
*
* #ORM\Column(name="status_pro", type="string", length=255)
*/
private $statusPro;
/**
* #var \DateTime
*
* #ORM\Column(name="creationtime", type="datetime")
*/
private $creationtime;
/**
* #var \DateTime
*
* #ORM\Column(name="updatetime", type="datetime")
*/
private $updatetime;
/**
* #var boolean
*
* #ORM\Column(name="publication", type="boolean")
*/
private $publication;
/**
* #ORM\OneToMany(targetEntity="website\AdsBundle\Entity\Photos",mappedBy="adslist", cascade={"persist"})
*/
protected $photos;
/**
* Set photos
*
* #param \website\AdsBundle\Entity\Photos $photos
* #return AdsList
*/
public function setPhotos(\website\AdsBundle\Entity\Photos $photos = null)
{
$this->photos = $photos;
return $this;
}
/**
* Get photos
*
* #return \website\AdsBundle\Entity\Photos
*/
public function getPhotos()
{
return $this->photos;
}
/**
* Add photos
*
* #param \website\AdsBundle\Entity\Photos $photos
* #return AdsList
*/
public function addPhoto(\website\AdsBundle\Entity\Photos $photos)
{
$this->photos[] = $photos;
$photos->setAdslist($this);
return $this;
}
/**
* Remove photos
*
* #param \website\AdsBundle\Entity\Photos $photos
*/
public function removePhoto(\website\AdsBundle\Entity\Photos $photos)
{
$this->photos->removeElement($photos);
}
public function __construct() {
$this->creationtime=new \Datetime;
$this->photos = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* #param string $username
* #return AdsList
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* #return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set email
*
* #param string $email
* #return AdsList
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set telephone
*
* #param string $telephone
* #return AdsList
*/
public function setTelephone($telephone)
{
$this->telephone = $telephone;
return $this;
}
/**
* Get telephone
*
* #return string
*/
public function getTelephone()
{
return $this->telephone;
}
/**
* Set displayPhone
*
* #param string $displayPhone
* #return AdsList
*/
public function setDisplayPhone($displayPhone)
{
$this->displayPhone = $displayPhone;
return $this;
}
/**
* Get displayPhone
*
* #return string
*/
public function getDisplayPhone()
{
return $this->displayPhone;
}
/**
* Set title
*
* #param string $title
* #return AdsList
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* #param string $description
* #return AdsList
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set country
*
* #param string $country
* #return AdsList
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* #return string
*/
public function getCountry()
{
return $this->country;
}
/**
* Set region
*
* #param string $region
* #return AdsList
*/
public function setRegion($region)
{
$this->region = $region;
return $this;
}
/**
* Get region
*
* #return string
*/
public function getRegion()
{
return $this->region;
}
/**
* Set department
*
* #param string $department
* #return AdsList
*/
public function setDepartment($department)
{
$this->department = $department;
return $this;
}
/**
* Get department
*
* #return string
*/
public function getDepartment()
{
return $this->department;
}
/**
* Set address
*
* #param string $address
* #return AdsList
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* #return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set city
*
* #param string $city
* #return AdsList
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* #return string
*/
public function getCity()
{
return $this->city;
}
/**
* Set zipCode
*
* #param string $zipCode
* #return AdsList
*/
public function setZipCode($zipCode)
{
$this->zipCode = $zipCode;
return $this;
}
/**
* Get zipCode
*
* #return string
*/
public function getZipCode()
{
return $this->zipCode;
}
/**
* Set statusPro
*
* #param string $statusPro
* #return AdsList
*/
public function setStatusPro($statusPro)
{
$this->statusPro = $statusPro;
return $this;
}
/**
* Get statusPro
*
* #return string
*/
public function getStatusPro()
{
return $this->statusPro;
}
/**
* Set creationtime
*
* #param \DateTime $creationtime
* #return test
*/
public function setCreationtime($creationtime)
{
$this->creationtime = $creationtime;
return $this;
}
/**
* Get creationtime
*
* #return \DateTime
*/
public function getCreationtime()
{
return $this->creationtime;
}
/**
* Set updatetime
*
* #param \DateTime $updatetime
* #return Test
*/
public function setUpdatetime($updatetime)
{
$this->updatetime = $updatetime;
return $this;
}
/**
* Get updatetime
*
* #return \DateTime
*/
public function getUpdatetime()
{
return $this->updatetime;
}
/**
* Set publication
*
* #param boolean $publication
* #return test
*/
public function setPublication($publication)
{
$this->publication = $publication;
return $this;
}
/**
* Get publication
*
* #return boolean
*/
public function getPublication()
{
return $this->publication;
}
// Callback
/**
* #ORM\PreUpdate
*/
public function udpateDate()
{
$this->setUpdatetime(new \DateTime());
}
}
And the photos class is the following:
<?php
namespace website\AdsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Photos
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="website\AdsBundle\Entity\PhotosRepository")
* #ORM\HasLifecycleCallbacks
*/
class Photos
{
/**
* #ORM\ManyToOne(targetEntity="website\AdsBundle\Entity\AdsList", inversedBy="photos")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="adslist_id", referencedColumnName="id")
* })
*/
protected $adslist;
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="photo_path", type="string", length=255)
*/
private $photoPath;
/**
* #var string
*
* #ORM\Column(name="thumbnail_path", type="string", length=255)
*/
private $thumbnailPath;
/**
* #Assert\File(maxSize="6000000")
*/
private $file;
private $tempFilename;
public function setFile(UploadedFile $file)
{
$this->file = $file;
if (null !== $this->photoPath) {
$this->tempFilename = $this->photoPath;
// reset values
}
}
public function getFile()
{
return $this->file;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set photoPath
*
* #param string $photoPath
* #return Photos
*/
public function setPhotoPath($photoPath)
{
$this->photoPath = $photoPath;
return $this;
}
/**
* Get photoPath
*
* #return string
*/
public function getPhotoPath()
{
return $this->photoPath;
}
/**
* Set thumbnailPath
*
* #param string $thumbnailPath
* #return Photos
*/
public function setThumbnailPath($thumbnailPath)
{
$this->thumbnailPath = $thumbnailPath;
return $this;
}
/**
* Get thumbnailPath
*
* #return string
*/
public function getThumbnailPath()
{
return $this->thumbnailPath;
}
/**
*
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function preUpload()
{
if($this->file === null){
return;
}
//$this->extension = $this->file->guessExtention(); // In my case I don't need it
$this->photoPath = $this->file->getClientOriginalName();
}
/**
* #ORM\PostPersist()
* #ORM\PostUpdate()
*/
public function upload()
{
if($this->file === null){
return;
}
if(null !== $this->tempFilename)
{
$oldFile = $this->getUploadRootDir().'/'.$this->tempFilename;
if(file_exists($oldFile)){
unlink($oldFile);
}
}
$this->file->move(
$this->getUploadRootDir(),
$this->id.'.'.$this->photoPath
);
}
/**
* #ORM\PreRemove()
*/
public function PreRemoveUpload()
{
$this->tempFilename = $this->getUploadRootDir().'/'.$this->id.''.$this->photoPath;
}
/**
* #ORM\PostRemove()
*/
public function removeUpload()
{
if(filex_exists($this->tempFilename))
{
unlink($this->tempFilename);
}
}
public function getUploadDir()
{
return '/upload/img';
}
public function getUploadRootDir()
{
return __DIR__.'/../../../../web'.$this->getUploadDir();
}
public function getWebPath()
{
return $this->getUploadDir().'/'.$this->getId().'.'.$this->getPhotoPath();
}
public function getMyWebPath()
{
return $this->getUploadDir().'/'.$this->getPhotoPath();
}
/**
* Set adslist
*
* #param \website\AdsBundle\Entity\AdsList $adslist
* #return Photos
*/
public function setAdslist(\website\AdsBundle\Entity\AdsList $adslist = null)
{
$this->adslist = $adslist;
return $this;
}
/**
* Get adslist
*
* #return \website\AdsBundle\Entity\AdsList
*/
public function getAdslist()
{
return $this->adslist;
}
}
EDIT:
I add the piece of twig template asked by bartek:
<body class="metro">
<div class="page secondary">
<div class="page-header">
<div class="div_header">
<img class="logo" src="{{ website_path_swap }}/files/images/logo.png" />
</div>
<div class="div_header">
<h2 class=""></h2>
</div>
</div>
<div class="page-region">
<div class="row">
<div class="span6">
<div>
{{ dump(article) }}
</div>
</div>
</div>
</div>
</div>
</body>
Intermediary answer:
for now I am using
\Doctrine\Common\Util\Debug::dump($article);
in order to get the output without problem. Apparently there are extensions that allows to echo the output in a more properly manner.
If I find something better I will come back to post it here.
In case you ended up here using Symfony 2.6+, make sure you register the DebugBundle in your AppKernel.php
public function registerBundles()
{
//...
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
// ...
}
return $bundles;
}