Sonata Media Bundle delete image programmatically - symfony

It seems that there is no documentation about how to delete images programmatically with Sonata Media Bundle.
I found another post saying that you need to call delete on the media manager.
Here is my mapping:
/**
* #var Application\Sonata\MediaBundle\Entity\Media
*
* #ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"persist", "remove"}, fetch="LAZY")
* #ORM\JoinColumn(name="image_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $image;
And this is the code I use to delete:
$this->mediaManager = $this->getContainer()->get('sonata.media.manager.media');
$previousMedia = $myEntity->getImage();
$provider = $this->getContainer()->get($previousMedia->getProviderName());
$provider->removeThumbnails($previousMedia);
$this->mediaManager->delete($previousMedia);
The association is correctly remove (the entity image field is now null) but on my gallery the object still exists and looks like this:
What am I doing wrong?

Related

How to fix "ProductSelection has no field or association named selectionType" doctrine error

Problem:
I read all the similar questions, but it didnt helped me out, so i post another question.
So i write the entity to my database table, and i forgot one field, so i added to the entity, and when i try to use it in my where section i got this error:
Uncaught PHP Exception Doctrine\ORM\Query\QueryException: "[Semantical Error] line 0, col 85 near 'type = :type': Error: Class SystemBundle\Entity\ProductSelection has no field or association named type" at /www/csibaf/SymfonyLealkudtuk/vendor/doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php line 63
The Background
I have a table name product_selection. In these i have some field, and i have a product_selection_type field, that i forgot to write in my Entity. So added later and now i got the error.
If i comment out, the where section in my query its perfectly fine and working, but if i place it in again it cause the error.
I already tried to clear the cahce with doctrine:clear:cahce-meatadat/query/result but it dindt helped me. So i checked the memchache and there are disabled. We dont use APC cache either so this isnt the problem. We tried all of this and nothing.
I tried to make another class with a whole different name, but the contetn is the same, just the class name that i changed, and if i cahnge the entity to this in my query builder, its working fine.
Someone were in simillar situation? Or can you get me another suggestion?
My codes:
ProductSelection.php
namespace SystemBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="product_selection")
**/
class ProductSelection
{
/**
* #ORM\Id
* #ORM\Column(name="product_selection_id", type="integer")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="ProductAll", inversedBy="selections")
* #ORM\JoinColumn(name="product_selection_product_id", referencedColumnName="product_id", onDelete="CASCADE")
**/
protected $product;
/**
* #ORM\Column(name="product_selection_quantity", type="integer")
*/
protected $quantity;
/**
* #ORM\Column(name="product_selection_price", type="integer")
*/
protected $price;
/**
* #ORM\Column(name="product_selection_date", type="datetime")
*/
protected $date;
/**
* #ORM\Column(name="ps_user_tracking_source", type="string")
*/
protected $userTrackingSource;
/**
* #ORM\Column(name="product_selection_identify", type="string")
*/
protected $identify;
/**
* #ORM\ManyToOne(targetEntity="UserBase")
* #ORM\JoinColumn(name="product_selection_userbase_id", referencedColumnName="userbase_id", onDelete="CASCADE")
**/
protected $userbase;
/**
* #ORM\Column(name="product_selection_type", type="integer")
*/
protected $type;
below this just the getters and the setters...
}
QueryBuilder section:
$selections = $this->entityManager->createQueryBuilder()
->select('ps')
->from('SystemBundle\Entity\ProductSelection', 'ps')
->where('ps.product = :id')
->andWhere('ps.type = :type')
->setParameters(array('id'=>$productId,'type'=>ProductSelectionConstant::NORMAL_CART));
$selections = $selections->getQuery()
->useQueryCache($this->container->getParameter('useQueryCache'))
->useResultCache($this->container->getParameter('useResultCache'))
->getResult();
My table fields:
"PRODUCT_SELECTION_TYPE" NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE,
"PRODUCT_SELECTION_PRODUCT_ID" NUMBER(10,0) NOT NULL ENABLE,
"PRODUCT_SELECTION_QUANTITY" NUMBER(4,0) NOT NULL ENABLE,
"PRODUCT_SELECTION_PRICE" NUMBER(12,0) NOT NULL ENABLE,
"PRODUCT_SELECTION_DATE" DATE NOT NULL ENABLE,
"PS_USER_TRACKING_SOURCE" VARCHAR2(69 BYTE),
"PRODUCT_SELECTION_IDENTIFY" VARCHAR2(32 BYTE),
"PRODUCT_SELECTION_ID" NUMBER(10,0) NOT NULL ENABLE,
"PRODUCT_SELECTION_USERBASE_ID" NUMBER(10,0)
So if someone can help me out it would be very nice. Thanks a lot.

Symfony Doctrine Delete only one site in one to one relationship

I probably can't think clearly anymore right now.
I have an entity Image related (oneToOne) to the entity airline.
Images can be replaced or deleted but airlines should still be kept after the images has been deleted.
How do I set this up in my Entity class?
Right now I have:
Airline entity
/**
* #ORM\OneToOne(targetEntity="AppBundle\Entity\Image", inversedBy="airline")
* #ORM\JoinColumn(name="image_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $image;
Image entity
/**
* #ORM\OneToOne(targetEntity="AppBundle\Entity\Airline", inversedBy="image", orphanRemoval=true)
* #ORM\JoinColumn(name="airline_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $airline;
But I get an ForeignKeyConstraintViolation Exception because it tries to delete the airline entity, too. I'm sure I have to set the onDelete differently or the orphanRemoval is misplaced_?
I already played around with it but couldn't figure it out.
Thank you!

Symfony2, Sonata : Customize title of collection

I've been able to translate most of the titles but i still have some non-friendly titles on collections (Table of relation)
Aire\AppBundle\Entity\ProjectSupported:000000002d1a645a000000015441bb1f
How could i custom them?
At best it could be the name of the related object ($investor->getName() and $project->getName() for exemple), at worst just a string.
In that case i'm using en entity with 2 relations
/**
* Owning Side
*
* #ORM\ManyToOne(targetEntity="Investor", inversedBy="supportedProject")
* #ORM\JoinColumn(name="investor_id", referencedColumnName="id")
**/
private $investor;
/**
* Owning Side
*
* #ORM\ManyToOne(targetEntity="Project", inversedBy="supportedProject")
* #ORM\JoinColumn(name="project_id", referencedColumnName="id")
**/
private $project;
Any hints or solutions?
Sonata is using the __toString method for text representation of objects.

ManyToMany on same table

I have a project in which I have a OneToMany relationship on the same database.
Currently it is designed like this:
/**
* #ORM\OneToMany(targetEntity="MyEntity", mappedBy="myCopiedItem")
*/
protected $mySource;
/**
* #ORM\ManyToOne(targetEntity="MyEntity", inversedBy="mySource")
* #ORM\JoinColumn(name="selected_myentity_copy_id", referencedColumnName="id")
*/
protected $myCopiedItem;
But now I have to make this relationship ManyToMany. So I did this:
/**
* #ORM\ManyToMany(targetEntity="MyEntity", mappedBy="myCopiedItem")
*/
protected $mySource;
/**
* #ORM\ManyToMany(targetEntity="MyEntity", inversedBy="mySource")
* #ORM\JoinTable(name="entity_has_copy")
*/
protected $myCopiedItem;
but the "entity_has_copy" table that symfony created has only 1 item (myentity_id) and I want to have 2 fields "myentity_id" & "selected_myentity_copy_id" which are both actualy id's from my "myentity" table...
What do I have to modify in order to have both id's in my generated table?
I'm sure I've missed something, but I cannot figure out WHAT :(
Note: Entity / table names were renamed for privacy
Solved this!
I had to add the relationship inside the definition...
So this is the correct definition for the JoinTable part:
/**
* #ORM\ManyToMany(targetEntity="MyEntity", inversedBy="mySource")
* #ORM\JoinTable(name="entity_has_copy",
* joinColumns={#ORM\JoinColumn(name="entity_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="entity_copy_id", referencedColumnName="id")}
* )
*/
protected $myCopiedItem;
Hope this will help others that are having same issue...
If you want to read more about how associations between entities are mapped with Doctrine, here's a good URL!

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 !

Resources