Typo3 is really driving me crazy - typo3

my findbypay ( pay beeing here as a parameter) returns the exact number of entries in the database but the objects are null ...
here is how I use it in an action :
public function getMyCompanysAction()
{
$argument = $this->request->getArguments('id');
$companies = $this->entrepriseRepository->findBypay(2)->toArray(); // just to test with the id 2
return json_encode($companies);
}
the outputed json contains 3 arrays but are all empty .. [{},{},{}].
how could this really happen ? knowing that when I use the same method and assign it to the view , the objects are complete and
I mean this way :
public function listAction()
{
$this->view->assign('lists', $this->entrepriseRepository->findBypays(2));
}

Related

single if v/s ternary operator - performance

Need some understanding regarding performance when using a single if vs ternary operator.
I create a wrapper instance wherein I initialize its variables. Eg below :
public class MyWrapperClass{
public string sUserName;
public MyWrapperClass(){
this.sUserName = '';
}
}
Now, while I assign the values returned from back-end to the wrapper variables, I use a single if to check if the value returned from the back-end is not blank.
Below is what I do:
if(String.isNotBlank(myObj.myField__c)){
myWrapperInstance.sUserName = myObj.myField__c;
}
I don't have an else condition here because I've already initialized the the variable as an empty string and hence if no value exists for the field, then my variable would hold an empty string.
However, I need to understand if it makes sense to use below:
myWrapperInstance.sUserName = String.isNotBlank(myObj.myField__c) ? myObj.myField__c : myObj.myField__c;
OR
myWrapperInstance.sUserName = myObj.myField__c != null ? myObj.myField__c : myObj.myField__c;
I need help understanding what is the better of the two from performance standponint?

Spring Batch - Invoke read() method in Reader multiple times

I am trying to implement calling read() method in the itemReader multiple times.
For Eg:
I have a list of POJO in which I will have one string variable with values either A or B or C.
I have to sort this list based on alphabetical order and segment it into three list for each value. i.e., list for value A and list for value B
and list for value C.
I need to send each list to the read() method in the itemReader one by one.
Once List for A is processed and write, then I need to send List for B and so on..
Is this doable? Any help is appreciated.
Although I am not very clear on what you are trying to achieve, I don't see any reason it cannot be done.
I assume you mean either of this:
1. You want the "item" to be process to be a whole list of POJO with same ABC Type, or
2. You want the item to be the POJO itself, and you want them to be processed in order of ABC Type
2 is straight-forward. At the first read, prepare all the POJOs, sort it. I assume they are in some kind of
In psuedo code, it looks like this
class MyReader implements ItemReader<MyPojo> {
private List<MyPojo> values;
MyPojo read() {
if (values == null) {
values = getPojos();
sort values;
}
if (values.isEmpty()){
return null;
} else {
return values.popFront();
}
}
}
1 is nothing more complicated. You will need to group POJOs with same ABC type in same list, and return the lists one by one. It can be easily done by using a TreeMap<String, List<MyPojo>>
In psuedo code, it looks like this
class MyReader implements ItemReader<List<MyPojo>> { // note the item is List<MyPojo>
private NavigableMap<String, List<MyPojo>> values;
List<MyPojo> read() {
if (values == null) {
values = new TreeMap<>();
pojos = getPojos();
for (pojo : pojos) {
if (values do not contain pojo.abcType() ) {
values.put(pojo.abcType(), new ArrayList(pojo));
} else {
values.get(pojo.abcType()).add(pojo);
}
}
}
if (values.isEmpty()){
return null;
} else {
return values.popFirstEntry().value();
}
}
}
If your list of items is fully available (you have a List<Pojo> loaded with all items) you can:
use a ListItemReader and inject into the ordered list
use a custom ItemReader and sort items after first ItemReader.read()
About break the best way is to use a custom CompletionPolicy based on pojo 'string variable'; in this manner your writer will receive a list where POJO's 'string variable' has the same values for all list items (check How to read csv lines chunked by id-column with Spring-Batch? for sample code).

Setting data with an event listener and the FormEvent::PRE_SET_DATA Event in Symfony2

I'm attempting to modify some data in a form based on other data that is in the form. From the docs:
The FormEvents::PRE_SET_DATA event is dispatched at the beginning of the Form::setData() method. It can be used to:
Modify the data given during pre-population
Modify a form depending on the pre-populated data (adding or removing fields dynamically).
This bolded line made me believe it should be possible to do something like the following in my buildForm function:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventListener(\Symfony\Component\Form\FormEvents::PRE_SET_DATA,
function(\Symfony\Component\Form\FormEvent $event) use ($options) {
if ($options['default_to_nickname']) {
$securityContext = $this->container->get('security.context');
$nickname = null;
if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY')) {
$user = $securityContext->getToken()->getUser();
$nickname = $user->getNickName();
if (isset($nickname))
{
$options['data'] = $nickname;
//$event->setData($nickname); //this doesn't work either
}
}
}
}
);
}
However, neither options['data'] = $nickname; nor $event->setData($nickname); result in the form being populated with the correct data. I can see that the data is being set in the FormEvent object when I call $event->setData($nickname);, but when the form is actually rendered, the data was not set. Am I expecting the wrong thing? I've looked into if something else might be modifying it after this, and there isn't. Any help would be very welcome!
You have to call $event->getForm()->get('your_property')->setData($nickname). Replace get() method by add($property, $type, ['data' => $nickname]) if the property doesn't exist yet.
See : http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#adding-an-event-subscriber-to-a-form-class

Entity Framework - Linq to Entities - strange issue with Anonymous function

Following is the code, I am trying:
public List<Movie> GetMovies()
{
Func<Movie, Movie> prepareMovieOutput =
(input) =>
{
input.DisplayHtmlContent = String.Empty;
return input;
};
var moviesOutput = from m in db.Movies.ToList()
select prepareMovieOutput(m);
return moviesOutput.ToList();
}
public List<Movie> SearchMovies(string searchTerm)
{
var moviesOutput = db.Movies.Where(m => m.Name.Contains(searchTerm)).ToList();
return moviesOutput.ToList();
}
The GetMovies function is working properly, as it returns List collection after clearing DisplayHtmlContent field, whereas, SearchMovies function is supposed to return Movie collection with DisplayHtmlContent field, but inspite of that it returns that field empty.
If I set DisplayHtmlContent to some fixed value (like, "ABC"),both GetMovies and SearchMovies return the list with all Movie having DisplayHtmlContent field as "ABC" value. I don't understand why the function defined in one method should affect the other one. and also how to fix this issue?
Ideally, I want GetMovies to hold all Movie with that particular field as empty string, and SearchMovies to hold all Movie with that field containing value.
Any help on this much appreciated.
this was due to the use of repository. I have removed it and it started working fine. with having EF 5, I didn't need to use repository

How to get form element value inside Zend_Form::isValid() method?

I'm looking for best way of getting form element values inside isValid() method.
I had something like this isValid():
public function isValid($data) {
$start = (int)($data['start_hour'] . $data['start_minute']);
$end = (int)($data['end_hour'] . $data['end_minute']);
if ($start >= $end) {
$this->getElement('start_hour')->addError('Start time should be less than end time');
return false;
}
return parent::isValid($data);
}
but problem is that when my data structure changes I have to change validation too.
For example, now values of start_hour, start_minute, etc becomes elements of multidimensional array, and I need edit validation like
public function isValid($data) {
$start = (int)($data['send']['start_hour'] . $data['send']['start_minute']);
$end = (int)($data['send']['end_hour'] . $data['send']['end_minute']);
.......
}
It would be great to get value of element by permanent key (like element name), so my isValid could looks like:
public function isValid($data) {
$start = (int)($this->getElement('start_hour')->getValue() . $this->getElement('start_minute')->getValue());
$end = (int)($this->getElement('end_hour')->getValue() . $this->getElement('end_minute')->getValue());
.......
}
but $this->getElement('start_hour')->getValue() inside validation method return an empty value.
Is this possible to get element value in such way?
Thanks.
Try with $this->getValue('start_hour');
If this code takes place in the isValid() method of the form, then sure you could do it as you have described. It's just that isValid() usually needs some data passed - $form->isValid($_POST), for example - and you just end up ignoring it (assuming the parent is Zend_Form, which has an empty isValid() method; intermediate ancestors could potentially inspect the passed data). I would consider that to be potentially confusing.
Al alternative could be to create a custom validator, attach it to one of the form elements (say, the start_hour elements). The signature for the validator's isValid() can use the optional $context parameter - isValid($value, $context = null). When you call $form->isValid($data), it will pass that $data as the $context to the the element validator. You can then use that $context variable to inspect the other values (start_min, end_hour, end_min, etc).
Try calling
$form->populate($data)
Before calling isValid that way the data will be in your form.
Then $this->getValue('start_hour'); should work from within isValid().
So to be sure:
Somewhere in your code (probably controller) there is somthing like:
$this->view->form = new MyForm();
$this->populate($data); //add this
if($this->view->form->isValid($data)){
//do stuff
}