Symfony2, Doctrine, How set datetime to Entity - datetime

i can not understand how can i persist datetime to database. I have string
(string) $oXml->currentTime
actually it's not a string but we convert it, so how can i add it to entity without error
Fatal error: Call to a member function format() on a non-object in...
current code
$currentTime = \DateTime::createFromFormat('Y-m-d H:m:s', (string) $oXml->currentTime);
$cachedUntil = \DateTime::createFromFormat('Y-m-d H:m:s', (string) $oXml->cachedUntil);
$oApiKeyInfo
->setCurrentTime($currentTime)
->setCachedUntil($cachedUntil)
not working :(

You need to pass the a DateTime object. Create it with a new statement, you can specify the time to use with the first constructor parameter.
$currentTime = new \DateTime((string) $oXml->currentTime);
$cachedUntil = new \DateTime((string) $oXml->cachedUntil);
$oApiKeyInfo->setCurrentTime($currentTime)
->setCachedUntil($cachedUntil);
If you need to specify a Timezone you can use the DateTimeZone class and pass it as the second parameter to the DateTime constructor.

Related

how can I override the toString method in openedge?

I have a serializable class that I would like to provide my own toString when being serialized to JSON.
DEFINE PUBLIC PROPERTY address1 AS CHARACTER NO-UNDO
GET.
SET.
METHOD PUBLIC OVERRIDE CHARACTER toString( ):
DEFINE VARIABLE result AS CHARACTER NO-UNDO.
RETURN address1 + address2 + city + country.
END METHOD.
END CLASS. ```
I am also assigning the class to a temptable and using the write-json method of a dataset to output but I get the standard toString representation .."myClass": {
"prods:objId": 1,
"myClass": {
"address1": "xxxxx"
}
}
can I somehow override the toString being used ?
The JsonSerializer does not use ToString() ,nor does it give you any control over the format that's produced. The Serialize method describes what data is written. If you want this ability added into the ABL, you can add an "Idea" at https://openedge.ideas.aha.io/ideas ; OE product management review these ideas periodically.
If you want control today over what is written, you will need to roll your own. By way of example, OE has the IJsonSerializer interface, which allows types to declare that they can be serialised using the JsonSerializer class.

value of type integer cannot be converted to 1-dimensional array

Curriculum is an .asmx file which has list of web service methods to return particular values, and this method GetMyEmployeeId is one such webmethod i will be calling from my webform, which returns an array containing current user (homepage) id
Public Function GetMyEmployeeId() As Integer()
Return New Integer() {Current.HomepageUserId}
End Function
I would like to call this webservice method in my webform and have to get the current employeeID, so that I can pass it to my different method which is DoSomething(here it takes the returned employeeID as parameter)
_curTree is the object of the curriculum class.
Private Function GetEmployeeActual() As Items
Dim item as Items
Dim employeeID As Integer()
'I am guessing that, am declaring the employeeID wrong ( it should not be an integer datatype may be because the GetMyEmployeeID returns an array of current user ID's)
employeeID = _curTree.GetMyEmployeeId()
item = DoSomething(employeeID)
'I am getting the error here as"value of type integer cannot be converted to 1-dimensional array"
Return item
End Function
Pls help me to proceed
This error is caused because you are trying to pass as a parameter or function return value a single integer when it expects an array. You didn't include enough in your code to be more specific. (Which line is the error on? What is employeeIDFilter? What is DoSomething?)

Set default display value for Symfony2 form field

I've got a form with some display only fields in it. These fields are usually DateTime values... but when empty/null I would like to display the string "never".
EDIT:
To be more explicit: The field should show the DateTime value from the database and if null the string 'never' should be displayed.
How should I do that?
Thanks in advance
You can use Symfony2 Data Transformers :
In the transform() function you can check if your date is null and then return the 'never' string. Otherwise return a string representation of your date.
In the reverseTransform() function you can check if the string is 'never' and then construct a null DateTime object. Otherwise, you transform the given string into a valid DateTime object with something like 'strtotime()` PHP function.

Comparing DateTime object in symfony (with propel)

I want to compare two DateTime objects in php code. I'm using Symfony 1.4 with Propel.
$article = $obj->getArticle();
if($article->getVisibleFrom() <= new DateTime()) {
DO_SOMETHING();
}
The problem is that I'm getting string from getVisibleFrom() getter (instead of DateTime object).
In database visible_from field is type of DATETIME.
I read that with Doctrine I could use function getDateTimeObject('visible_from').
You can change the default behavior at build time by changing build properties: http://www.propelorm.org/reference/buildtime-configuration.html#datetime_settings
And here is the doc for the temporal getters: http://www.propelorm.org/reference/active-record.html#temporal_columns
William

How to access a field's value via reflection (Scala 2.8)

Consider the following code:
class Foo(var name: String = "bar")
Now i try to get the value and the correct type of it via reflection:
val foo = new Foo
val field = foo.getClass.getDeclaredField("name")
field.setAccessible(true)
//This is where it doesn't work
val value = field.get(????)
I tried things like field.get(foo), but that just returns an java.lang.Object but no String. Basically I need the correct type, because I want to invoke a method on it (e. g. toCharArray).
What is the suggested way to do that?
As others have mentioned, the reflection methods return Object so you have to cast. You may be better using the method that the Scala compiler creates for field access rather than having to change the visibility of the private field. (I'm not even sure if the name private field is guaranteed to be the same as that of the accessor methods.)
val foo = new Foo
val method = foo.getClass.getDeclaredMethod("name")
val value = method.get(foo).asInstanceOf[String]
getDeclaredField is a method of java.lang.Class.
You have to change foo.getDeclaredField("name") to foo.getClass.getDeclaredField("name") (or classOf[Foo].getDeclaredField("name")) to get the field.
You can get the type with getType method in class Field but it won't help you because it returns Class[_]. Given than you know that the type is a String you can always cast the value returned using field.get(foo).asInstanceOf[String]
AFAIK, reflection always work with Object, and you have to cast the results yourself.
This is how one can get list of fieldnames and its value of a case class:
First, using reflection, get fields info as follows -
val TUPLE2_OF_FIELDNAME_TO_GETTERS = typeOf[<CLASS>].members
.filter(!_.isMethod)
.map(x => (x.name.toString, classOf[<CLASS>].getDeclaredMethod(x.name.toString.trim)))
How to use it?
getFieldNameAndValue(obj: <CLASS>): Seq[(String, String)] {
var output = Seq[(String, String)]()
for(fieldToGetter <- TUPLE2_OF_FIELDNAME_TO_GETTERS) {
val fieldNameAsString = fieldToGetter._1
val getter = fieldToGetter._2
val fieldValue = getter.invoke(obj).toString
output += (fieldName, fieldValue)
}
}
foo.getClass.getDeclaredField("name").getString(foo)
should work if you want to avoid asInstanceOf. get* is available for various types

Resources