I'm reading the Domain Driver Design book and there is a recommendation to do the methods followed by domain behaviors and not just getters and setters (anemic objects).
But to map an entity on Symfony forms, if I don't put the setter, the form cannot fill the field.
It shows me the error:
Could not determine access type for property "myField" in class "MyNamespace\MyClass".
How do you guys deal with that? There is a way do map the field without the setters or the data transformer?
As I know Doctrine does not use the setters to fill the object.
Thank you guys!
As ostrolucky pointed out here you can pass the property_path option, read more here.
You can use value objects instead https://webmozart.io/blog/2015/09/09/value-objects-in-symfony-forms/
Related
For example:
public function getField() {
return ucfirst($this->field);
}
Given that an entity has getters that do some changes on the database value before returning it, how can those changes also be applied when using the getArrayResult() method ?
For example, Laravel has accessors (http://laravel.com/docs/5.0/eloquent#accessors-and-mutators). The entity getter can be used in the same way.
When using getArrayResult(), the value for the "field" will not have the first character capitalized.
Thank you!
Well, it's the same behaviour as laravel almost :)
Take a look at Hydrators
.
Hydrators are the processors that bind your raw db output to various data types in doctrine. Thus you have Doctrine_Core::HYDRATE_RECORD which is the standard hydrator(aka the thing called when you use $query->getResult()).
If you use $query->getArrayResult(), it uses the Doctrine_Core::HYDRATE_ARRAY Hydrator.
If you need a more detailed description, please let me know.
I'm getting really confused by entity properties and what they mean. I understand that get and set allow the application to interact with protected and private properties, but what about add and remove?
When running the command
php app/console doctrine:generate:entities bundle:entity
Sometimes it will generate getters and setters and other times it will generate add and remove properties (and usually a get method as well). I've noticed that it also sometimes depends on the relationship with other entities (i.e. OnetoMany), but not always :-S
Nowhere in the Symfony documentation that I can find is this addressed, and it's causing me to see the message "Neither the property "x" nor one of the methods exist and have public access" way too often. Can anybody provide a succinct explanation of this?
add and remove are used to deal with collections. If for example your Entity (Let's say A) contains a collection of B elements, then the command will provide an addB() and a removeB() public methods to help you add and remove elements from your collection. It'll also provide a getter which returns the whole collection.
The command generates methods based on the type of attributes you're working with (ArrayCollection, string, ...)
For xxxToMany associations, Doctrine will generate a "adder" and a "remover" instead of a plain setter. The idea is to easily add and/or remove a single object from the collection without needing to pass around the entire collection everytime to the setter.
Note though that these generated methods are an implementation detail you are free to revise. If you prefer a single setter method for example, feel free to implement that one yourself.
I personally don't rely on the accessor generation of Doctrine anymore. Doing it manually allows greater control of your entity's API, and is also quite easy in an IDE like Netbeans or PHPStorm.
I have some personal data structure mixed with "standard fields". I would like to avoid the manual work on simple fields (with datastore native API):
toPersist.setProperty("field1", value1);
toPersist.setUnindexedProperty("field2", value2);
but I still want to get the prefilled Entity instance toPersist so I can add my own #Ignore fields my self
For example:
Entity filled = OfyService.ofy().save().entity(this).fill();
filled.setProperty("mySpecialField", jsonValue);
//...
// I want to save my entities alone
datastore.put( filled );
reversely I'd like to get the Entity object representing each entry in a load() call.
Is this possible? or do I have to dive into Objectify code to hack it?
thanks for your answers
I don't follow your question exactly, but I'm pretty sure what you're looking for are the #OnLoad and #OnSave annotations. You add them to methods within your entity classes, and those methods will be called just after an entity is loaded, or just before one is saved, respectively. The documentation for them is here.
Edit:
After your comments (below) I now understand what you are trying to accomplish. Yes, Objectify supports this (though I have never tried it myself). You want to use the Saver.toEntity() and Loader.fromEntity() methods. It appears you can use them like this:
// Use Objectify to convert a POJO into an Entity
Entity filled = ofy().save().toEntity(myPojo);
// Use Objectify to convert an Entity into a POJO
Object pojoCopy = ofy().load().fromEntity(filled);
I have a POJO which declares a collection of List<Long>.
We're currently sending an ArrayCollection of Number, and it's arriving at the client as an ArrayCollection, populated with Integers.
Given the issues with Type Erasure & generics I understand how this is happening. How do I fix it?
Unfortunately because of some internal limitations, the model cannot be changed. Ie - we can't use a subtype, and we can't implement IExternalizable.
Are there other options for registering custom type converters in LCDS, other than IExternalizable? Alternatively, are there any annotations we can use that indicate the type to be used for a collection?
Regards
Marty
It looks like https://bugs.adobe.com/jira/browse/BLZ-115. I would try using a custom marshaller, like this one.
I'm using NHibernate to administer my entities, and to have lazy loading enabled I need to make my properties return an IList<>. Problem is that .NET throws an exception as it can't serialize an interface when I'm trying to pass the entity. This makes perfect sense.
What I need to know is how I can control which fields to serialize, and which not to? My best bet so far is to work around this problem by copying the contents of IList<> into a List<> before serializing the object, but to do that I need to tell .NET that I don't want the IList<> property serialized :)
Just wanted to let you guys know that I found the answer to be the
[System.Xml.Serialization.XmlIgnore] attribute :)
MSDN has an area on Serializing Objects, but what you want is Selective Serialization. So basically, you can mark any property you don't want serialized with the attribute, [NonSerialized]. There is an example in the second link.