I know that it is possible to specify that you want array type instead of object type when you run query with Doctrine. However, I happen to be working with the code that I can't edit which returns to me the result from a query as an object and I want to be able to convert that to array somehow. It seems like in the older version doctrine used to have something like toArray() which can be used.
Is there something similar to that now which I can use?
No, doctrine 2 uses the data mapper pattern and doesn't make any assumptions about the PHP class. If the class doesn't provide a toArray() method explicitly, then you'd need to create the array manually with the object's getter methods.
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.
When I'm seralize array I've got something like this:
{"0":{"id":1,"name":"Kowalscy","familyMembers":[{"id":1,"familyMemberType":1,"user":{},"name":"Pawel","isAdmin":true,"notes":[],"email":""},{"id":2,"familyMemberType":2,"user":{},"name":"Mama Koper","isAdmin":false,"notes":[],"email":""}],"children":[]},
"1":{"id":2,"name":"Spiochy","familyMembers":[{"id":3,"familyMemberType":2,"user":{},"name":"Test Nowak","isAdmin":false,"notes":[],"email":""}],"children":[]}, .....
But I have problem with deseralization that array using JSON, I think that it should look like:
[{object}, {object}]
Is there way to change that?
Here is my code from symfony2
$result = $serializer->serialize($families, 'json',SerializationContext::create()->setGroups(array('getFamilies')));
EDIT:
I've found that "getArrayResult()" returns array, but it doesn't contain my relation entity
From memory, it is a bad practice to have an array as top level in json.
What are "top level JSON arrays" and why are they a security risk?
Moreother, if you use array, you lost the ids of your objects. So it is normal that you lost relations.
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'm using symfony2 and have accessed an entity using a doctrine query, which I then pass to my twig template. This entity has variables that I would like to loop through without having to explicitly name each one. Is there a way I can do this with twig or should I try a different design?
Twig documentation says:
A sequence can be either an array or an object implementing the
Traversable interface.
So it should work as long as you implement one of the Traversable interfaces (IteratorAggregate or Iterator).
Struts2 inserts java.lang.Strings into Maps as default, but... is there a way to override that behavior for some parameters and insert different Objects using many custom Type Converters?
For example, in a Java class we can declare a HashMap and put something like:
myHashMap.put(“name”,”myName”); //this is a String
myHashMap.put(“id”,new Integer(“101”)); //this is an Integer
myHashMap.put(“date”,java.util.Calendar.getInstance().getTime()); //this is a Date
Is it possible to assign the correct java.lang.Object directly from the <s:form> using Type Converters?
If you have something like this in your <s:form>:
<s:textfield name="myHashMap['name']"/>
<s:textfield name="myHashMap['id']"/>
<s:textfield name="myHashMap['date']"/>
Every value ends as a java.lang.String inside the HashMap, instead of having a String, an Integer an a Date...
I tried to create my own Type Converter with no luck... I guess Struts2 wants a POJO with setters and getters for each parameter, but the HashMap uses the “put(Key,Value)” method.
I have the setters and getters for “myHashMap”, but I thought Struts would somehow use it like a POJO when setting the parameters (when calling “put(Key,Value)”).
I created “MyAction-conversion.properties” file and wrote this:
date=app.converter.MyDateConverter
It didn't work... Then I tried also doing this:
myHashMap['date']=app.converter.MyDateConverter # (didn't work)
myHashMap.date=app.converter.MyDateConverter # (didn't work)
The converter isn't called at all! I do have “MyDateConverter” class and it is working fine. If I use a POJO (instead of the HashMap) and create the setters and getters inside the POJO for “name”, “id” and “date” it works great. But the thing is that I want to use something more generic, like a HashMap, in order to change the name of the parameters in the form or add more without having to create another POJO.
For the moment, it works using the HashMap if you expect to receive only Strings, but I don't know how to call a Type Converter to receive custom Objects. The converter isn't called and I end always with Strings.
struts2 can convert java.util.Date from String without coding your converter. Infact,'date' is String type ,struts can not find it by it's content but type.
Doubtful, and if you can, don't. Doing so would require the use of a raw map and casting to retrieve the values. Type safety is your friend.
But the thing is that I want to use something more generic, like a HashMap, in order to change the name of the parameters in the form or add more without having to create another POJO.
That isn't a better design. Stick with the POJO approach.