I've created a superclass for my entities:
class superclass{
/** #column(type="string", length=1,
* options={"default":"c"})
*/
protected $status;
/** ...more things **/
}
The next step is create my own repository, so it replace EntityRepository. The aim is set the status value depending of:
Entity is removed - 'd'
Entity is modified - 'm'
Entity is created - 'c'
So I need to override methods from EntityRepository: find(), findBy(), delete(),...
I didn't found any work related. Could it not be good idea?
Any clues?
Thanks in advance
Exists a project with similar dataflow. https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/softdeleteable.md
With simple annotations it creates and manages the deleted fields and extract the not deleted fields using the defaults EntityManager methods.
You can create your own filters for the Doctrine's operations:
http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-associations.html
Related
In Symfony2, I just try recently to think in terms of traits, to create some sort of behaviors.
Let's say I have an address attribute in an entity. I externalized attributes, getters and setters related to this in an AddressableTrait.
But what if address become an entity? I started to try to define my OneToMany relation in my trait, as if it was in a regular entity :
use Doctrine\ORM\Mapping as ORM;
class AddressableTrait {
/**
* #var
* #ORM\OneToMany(targetEntity="XXXX\GlobalBundle\Entity\Address", inversedBy="What to put here" )
*/
protected $addresses;
/**
* #return ArrayCollection
*/
public function getAddresses()
{
return $this->addresses;
}
/**
* #param ArrayCollection $addresses
*/
public function setAddresses($addresses)
{
$this->addresses = $addresses;
}
}
What to put in the inversedBy? The purpose of the trait if precisely to embed all the behavior feature, so I think that at least using traditionnal annotation/YML/XML,it's not possible to achieve.
I digged a bit into it and found this very interesting link that seems to allow you to defines relation via events, but there is still logic to add to "finish" relations.
UPDATE :
Using the above link, I managed to created dynamic ManyToMany relation. the schema update works when creating, but if I comment the dynamic relation, a schema:update --dump-sql doesn't remove it. It seems to work add-only. Any clue to force the dynamic mapping to stick to the real relations addition/removal?
Thanks a lot for your answers !
Nicolas
I encountered a problem using traits in entities. For regular database values (scalar, DateTime) traits worked fine, but when I tried to define entity relations in traits the doctrine migrations bundle would convert the property to a varchar field.
The only way I could find to fix creating proper entity relation properties was by moving them out of the trait and into the entity class itself.
Maybe there are already answers regarding this topic but I cannot find any information. If there are answers already, just post any links.
My question is:
I work with Symfony 2.5 and doctrine 2
A common problem I have is that I have an entity that I want to persist to different tables in the database. For this I usually create a superclass and extend it in two concrete entities that I persist to the database.
Now I have two entities deriving from the same base class. Is it possible to convert from one to another. Of course I have to persist them to the database separately.
One example. I have two classes: "quote" and "order" which are basically the same and extend the superclass "project"
/**
* #ORM\MappedSuperclass
*/
abstract class project {
/**
* #ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* #ORM\ManyToOne(targetEntity="Bundle\Customer")
*/
private $customer;
/**
* #ORM\ManyToOne(targetEntity="Bundle\CustomerContact")
*/
private $customercontact;
... (getters/setters etc.)
}
class quote extends project { ... }
class order extends project { ... }
Now once a quote is confirmed I want to "convert" it to an order and keep all the base data (stored in the "project" part). Also I want to keep the quote itself so I kind of want to create a new order from the quote instead of using the "project" directly and just set a status there.
Of course I could write a constructor or something but I am searching for a way to clone that part of an object.
Maybe there is an alternative way where the class is not extended but the "project"-part is used within the class itself. Not sure but it seems to me like a lot of people could have run into such a problem already and there might be a smart solution for this kind of situation.
I'm happy with any links to docs too if applicable.
I am having problems with Doctrine MongoDB ODM. Scenario is very simple (I think).
I have set of documents with COLLECTION_PER_CLASS inheritance model (Base, Product, etc). This all works well and I can persist objects as usual.
/**
* #ODM\Document(collection="content_base")
* #ODM\InheritanceType("COLLECTION_PER_CLASS")
*/
class Base {
/**
* #ODM\Document(collection="content_products")
*/
class Product extends Base {
Now, I have another Document (Item) and I want to create ReferenceOne to inherited Documents:
/**
* #ODM\ReferenceOne(targetDocument="Base")
*/
private $content;
And this is where I am having problems, because Doctrine ClassLoader complains about class Base being redeclared:
Fatal error: include(): Cannot redeclare class content\documents\types\base in /vendor/composer/ClassLoader.php on line 183
I am a bit confused why would this be a problem, or otherwise what is correct way of doing this?
UPDATE
I have just noticed, if I remove 'targetDocument' from ReferenceOne annotation I get results I wanted. Is this the correct way to do it?
Thanks,
Greg
Region:
namespace Acme\RegionBundle\Entity;
class Region
{
private $id;
/**
* #ORM\OneToMany(targetEntity="User")
* #ORM\JoinColumn(name="region_id", referencedColumnName="id")
*/
private $users;
}
User:
namespace Acme\UserBundle\Entity;
class User
{
private $id;
private $region_id;
}
How to relate entities from different bundle without mentioning fully specified entity path i.e. hard coding dependency.
Is there any better approach ?
Can Resolve Target Entity Listener be a solution. I couldn't understand how it could be applied here ?
The resolve target entity listener allows you to re-define associations at runtime. It allows you basically to map something like following:
#ORM\OneToMany(targetEntity="My\Namespace\UserInterface")
As you can see, mapping an interface as target entity does not make much sense. It becomes really useful when you tell that every My\Namespace\UserInterface has to be replaced with a Other\Namespace\User reference.
I'm working on mechanism that will allow me create forms automatically from class annotations.
For example there is a class called "News" with some custom annotations.
/**
* #Admin\Form(name="news")
*/
class News
{
/**
*
* #Admin\Field(name="title", type="text")
*/
private $title;
}
My goal is to write mechanism that will check if exists class with "Form" annotation and create form based on this class fields.
Where should I put this mechanism? First I was thinking about owerwritting FormFactory but I believe there is a better place for such thing, maybe Extension?
There already is a bundle that does what you're asking for: http://knpbundles.com/FlintLabs/FormMetadataBundle
However, if you'd like to create it yourself, you should create a bundle and within it create a custom annotation driver based on the doctrine2 specs (as Symfony uses Doctrine for reading annotations)
In Symfony2, you can add functionality to existing form fields through the use of "Form Type Extensions".
To apply your extension to all field types, set the return value of the getExtendedType() method to "form", i.e:
public function getExtendedType()
{
return 'form';
}
I haven't figured out how to fetch the annotations from the form extension yet.