When I'm using Doctrine I have one problem. When I'm creating entities using console command there are classes with private properties and get and set methods for each propery. But If I used twig I wouldn't have problems, but I'm using angular and when I need all properties such a keys I have to use getPropertyName method for each property. I don't want to create my own model system, but go through the loop for each property it isn't good and convenient. Because I have to send to the angular entire objects . How can I do it easier?
The main problem that I want get object from DB make json_encode and send it to front-end, but instead of that I have to make a loop through every object and rewrite any property that I need into new array. I need something that isn't crazy like this)
The easier way is to serialize the object in json. You could do that with JMSSerializerBundle
Related
While designing a class I assumed that having an $id property will make that class Entity rather than a value object.
I also have a toArray() method which converts the object to associative array and that response is send to post and patch api's.
Now I have the following questions:
POST works,
since I’m not sending the id in the body. But for PATCH is it fine if I set the property dynamically after object creation? For Ex:
$redCircle = new Circle(“red”);
$redCircle->id = 10;
$api->patch($redCircle->toArray());
Your point of view is very technical, as opposed to DDD.
You should design your Aggregates and nested Entities according to the business rules (the invariants).
While designing a class I assumed that having an $id property will make that class Entity rather than a value object.
This is not true. There are cases when a local Value object (that comes from a remote Aggregate, in another Bounded context) needs an ID property in order to be kept up-to-date (i.e. by background tasks). The Anti-corruption layer would need this property, so the reasons are pure technical.
POST works, since I’m not sending the id in the body. But for PATCH is it fine if I set the property dynamically after object creation?
Again, this is not a DDD view of the problem. In DDD, an Aggregate execute commands: one does not simply update its internal state directly; this would break its encapsulation.
But to answer your question, considering that you have a CRUD app: In order to mutate parts of an Entity you would need to load it from the Repository before mutation. The Repository would set the ID along with the other properties.
One of the best ways to locate entities is to use a RESTful API, so the client would not need to construct the URLs for the PATCH operation.
I'm writing an API where I have a Controller that POSTs a new object, GETs it back and can PUT/PATCH updates to it. The problem is that there's a difference in properties between the two different actions. For example, when I POST a new object I want to ensure that the 'id' of it is returned so that it can be used to identify it for the GET/PUT/PATCH endpoints. It doesn't matter if it comes back via the GET (it's just a duplication of data at that point) but I certainly don't want it passed for the PUT or PATCH as the id is immutable.
So what's the best way to mark this up in swagger so that I can have different versions of the same Definition? I've seen that you can use 'allOf' to add Definitions to other properties, but I'm wondering if there's a way of saying 'not these properties in the definition'?
If I could do the latter I could make one Definition of the object as a whole and simply knock out the things that aren't required to be returned or submitted when referencing it at the Controller. Is this possible? Am I making sense?
(Just to make things more interesting, my swagger.json file is being generated by swagger-php based on Annotations in my controller and entity files)
I am facing the same issue while writing our spec file, and didn't know how to fix it but what I used is the property "readOnly":true, this way the documentation says that this is a readonly property, you can only read it through GET/POST methods but you cannot send it via PATCH/ PUT.. hope this helps
Using Doctrine, is it possible to map to properties which don't actually exist using magic methods?
I'm doing the mapping with YAML.
For example, if I wanted to map to a property named "demo", but SomeClass::$demo didn't actually exist. I'd want to some combination of __get(), __set(), __isset() and __call() to handle $demo (and getDemo() and setDemo()) and do something else with them.
I've tried setting this up, but I'm getting an error:
Uncaught exception 'ReflectionException' with message 'Property My\Bundle\DemoBundle\Entity\SomeClass::$demo does not exist'
I'm not sure if there is something special with the ReflectionProperty that causes it to miss my magic methods, or if I'm maybe missing a magic function. However, as far as I can tell, ReflectionProperty should interact with them.
Any ideas?
UPDATE:
Upon further investigation, it looks like the ReflectionProperty constructor will throw an exception and won't trigger the magic methods.
Does anyone else know of means to map Doctrine to dynamic properties?
Thanks.
UPDATE 2:
To example what I'm trying to accomplish.
Basically, I have a generic User object which just contains the base properties needed to handle actually being a user (roles, password, salt, username, etc.). However, I want to be able to extend this object to add application-and-user-specific meta data.
So, say I create a Forum bundle. I could then I could dynamically hook up meta data related to the user for use with the Forum. I don't want to put it directly in the User bundle, because then the User bundle becomes less flexible.
If I could somehow dynamically inject new data in to the user, it could all be loaded in a single query with the user, instead of having to be loaded in a separate query. I know there are some other methods to do this, which I've already explored and even used to a limited extend. However, it'd be much nicer if I could dynamically create these associations, which really shouldn't be that difficult of a leap.
If you don't need to search on these dynamic properties then just add a property called data to your entity and map it to a doctrine array type. Now do your majic stuff and store the dynamic properties in the data array.
A second approach might be along these lines: http://symfony.com/doc/current/cookbook/doctrine/resolve_target_entity.html. For each installation you might be able to give the administrators of making a custom entity.
But as long as you don't need to directly query on your dynamic properties then the first method works well.
Seems codeigniter only allows one instance of a library I can't design a solution with objects holding arrays of corresponding details as I usually would. I'm considering just doing AJAX calls to make additional queries from the view to fill the would be objects with their corresponding details. Does anyone have ideas for a more elegant solution?
there is so a way to call an object more than once:
$this->load->library('someclass', $params, 'alc');
the CodeIgniter User Guide explains that very well. the third parameter alc will be what you now use to call that object injunction with the other, I have to do this myself a few times.
i wouldn't do the require route as CI does a nice job of handling all those objects calls on its own.
You can still instantiate multiple objects just not through the codeigniter $this super class. You can simply include the libray file you need manually using require() or include() for example and use $object = new MyClass();
You can then pass any objects you make to the view using $this->load->view('viewname', array('object', $object )); but beware objects (class variables) are converted to array elements when passed to the view in this way. So you can still manipulate them as objects before passing to the view as you would in any other application.
First things first. I may be completely off track with this. I'm still learning with Mongo and NOSql solutions in general. We have a new component to our app we are writing and instead of sitting down and messing with a relation database we wanted to use something that would fit better with our objects.
So let's take a simple example:
Class User extends \Model {
public $name;
public $hobbies;
}
$name would just be a string. But lets say $hobbies is an object or an array of objects. I want to just be able to throw this into a data store and be able to retrieve it later.
At first I went down the road of breaking the object down into an array and storing that in Mongo and then pulling it back out and populating an object. Pretty simple with a generic import and export method I made. The problem comes when I have some robust objects that have other objects as member variables and so on. At that point I could still export into a multidimensional array and store it fine. But importing back into the objects became problematic.
The other option I could do is just seralize() the object and store that in mongo along with some descriptive data.
Sooooo. Thoughts?
Part of my problem here is that I'm new to the NOSql products and not sure their full limitations/potential. Am I just looking at Mongo wrong and trying to make it do something it's not meant to do? I'd prefer not to use some 3rd party module and would rather write something simple and lightweight.
Although I didn't want to use a 3rd party app, Doctrine's ODM for Mongo seems to do exactly what I wanted. Got it set up and seems to be working good so far.
http://www.doctrine-project.org/projects/mongodb-odm.html
I think serialize is the way to go here. Then you can use the magic methods __sleep and __wakeup for each class to handle any tricky situations.
The other option here to serialize your objects into arrays instead of just using "serialize". If I'm not mistaken, you can actually override the "serialize" method in these sub-objects and basically have them serialize themselves into arrays (or more specifically hash-tables).
If Doctrine does this for you, then all the better. But if you just want this feature you can probably cook your own.