Symfony 2 PHP Template Engine rendering Objects - php

I kind of prefer using PHP Template Engine to Twig in symfony2 due to some flexibility, and something I am more used to.
For instance, I will like to use things like 'ternary If' when printing out some stuff.
My problem now is that, My data got rendered on the template as Objects. using (array) seems not to help as well as I ended up getting things like;
array (size=4)
'�news\newsBundle\Entity\News�id' => int 1
'�news\newsBundle\Entity\News�title' => string 'News title 1' (length=12)
'�news\newsBundle\Entity\News�body' => string 'Some body text' (length=14)
'�news\newsBundle\Entity\News�createdDate' =>
object(DateTime)[306]
public 'date' => string '2014-11-05 19:41:48.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/London' (length=13)
How do I get results like a normal array e.g array('id'=> int 1) without the namespace stuff in it.
Thanks in advance

You may want to change hydration mode in your queries to get entities as arrays:
http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html#array-hydration
Symfony2, Doctrine 2: getResult Object

Related

Why does add_post_meta create a nested array?

I can't figure out why that when trying to add a custom post meta value from a set of checkboxes, by default a nested array set is being created, where I would just have a single array of the values I've saved.
So when I perform add_post_meta($post_id, 'Procedure', $_POST['bv_procedures']); and then do a var_dump of the value, I am returned with this:
array (size=1)
0 =>
array (size=2)
0 => string '13419' (length=5)
1 => string '13416' (length=5)
Why is the first index there, and how can I just have a single array containing the two values?
-- UPDATE --
I have since noticed somewhat strange behaviour with this now:
var_dump(get_post_meta($post->ID, 'Procedure', true)); will result in this being displayed on the screen when editing this post:
array (size=1)
0 => string '13419' (length=5)
However, I'm looking in the 'Custom Fields' section, and Procedure is not listed in there at all, whilst other post meta pairs are. So I have no idea what's going on here, why is this not being listed? And also, I don't understand why stating get_post_meta to return a string value, returns an array (albeit not nested). Surely, this is a bug?

mongodb find nested field not work in php

i am new on php and mongodb. i want to find all data which match the condition in php.
the following mongodb structure:
'Food' =>
array (size=4)
'_id' =>
object(MongoId)[36]
public '$id' => string '587e4a89974711036c97d3e5' (length=24)
'CaseId' => string 'c002' (length=4)
'Level' => string '4' (length=1)
'food' => string 'banana' (length=6)
i try to search 'food' value in Food object. i have do some research. i have some method to deal with. however, i cannot find anything. i don't know where wrong? hope someone can teach me.
here is my query:
$cursor=$collection->find(array("Food.food"=>array('$in'=>["banana"])));
$cursor=$collection->find(array('Food'=>array('food'=>"banana")));
$cursor=$collection->find(array('Food.food'=>"banana"));
after i research, i know MongoDB supports dot notation. resource from
enter link description here
also i find some get nested field method from
enter link description here
i following the link 2 to typing in mongodb compass. i can get the result i want, but when i follow the structure to enter in php. it have not any result.
what happen in these code?? i am confused. thank you.
i find other method to solve this problem. if someone meet this case like me, you can try below method.
$query= array(
'$match'=>array('Food.food'=>'banana')
);
$cursor=$collection->aggregate($query);
you can type $cursor['result'] to receive the searching result.

Getting data from multidimensional arrays (render array) in twig

I have a render array for a form in my D7 module that looks like this:
'form' =>
array
'#id' => string 'demo-form' (length=9)
'#action' => string '/sprint07/' (length=10)
'name' =>
array
'#title' => string 'Username' (length=8)
'#maxlength' => int 13
'#size' => int 15
'#type' => string 'textfield' (length=9)
'#required' => boolean true
'#input' => boolean true
'#autocomplete_path' => boolean false
... snip ...
I'm using twig-for-drupal to create a theme, but I can't seem to get the values back, I've tried {{ form['name']['#type'] }}, {{ form.name.#type }}, {{ form.name['#type'] }} and so on, but they all come back blank.
Other fields such as form['#action'] works fine, so how should I access the information more than one array in? I can't change the module too much as the php template in its native state uses render(form);
-- Update --
Added some more debugging to this, and seems that I can use {{ form|dump('v') }} to show me the whole array as above, but when I do {{ form.name|dump('v') }} I just get a string, which I assume means that twig is automatically detecting its a render array, and "helpfully" converting it for me.. So I guess that makes my question: how do I turn off the auto-rendering?
"name" sub array don't contain the "#id" key.
try if work
{{ form.name.#title }}
Turns out it isn't currently possible to do this on a case-by-case basis, TFD is currently set up to auto-render anything it thinks is a render array, and to turn this off you need to set autorender to FALSE in twig_get_instance(). I've tried this myself however and it still seems to be turning the array into a string, so this could be a bug within TFD, or I just don't understand the usage, which is equally, if not more, likely.
From ReneB's sandbox:
Autorender
This version of the TWIG engine uses auto render to prevent themers get RSI from typing {{node.field_somefield|render}} for every single field they want to render from the render array (of doom) so the can safely type {{node.field_something}}
On rendering of the compiled template TFD check if the called variable is a string, callable or array. If it's a string it simple does echo $string, if it's a callable it return a proper method() for it. And if it's an array, it assumes it's a renderable array and maps it to the render($string); method of drupal.
This way the objects hidden with hide() are respected.
To turn this is for now, you have to set autorender to FALSE in the twig_get_instance() method of twig.engine.
I'am working on a {% noautorender %} {% end noautorender %} block structure.

Better data structure or algorithm than iterative searching on multidimensional arrays to find corresponding values?

For a site I am working on I use a library to get a list of states. It returns a numerically indexed array of states, each with three keys: stateCode, stateName, and stateSeg. It looks like this:
array
0 => &
array
'stateCode' => string 'AL' (length=2)
'stateName' => string 'Alabama' (length=7)
'stateSeg' => string 'alabama-al' (length=10)
1 => &
array
'stateCode' => string 'AK' (length=2)
'stateName' => string 'Alaska' (length=6)
'stateSeg' => string 'alaska-ak' (length=9)
2 => &
array
'stateCode' => string 'AZ' (length=2)
'stateName' => string 'Arizona' (length=7)
'stateSeg' => string 'arizona-az' (length=10)
I often find myself with one of the three values and needing to look up its corresponding value. To do this I find myself constantly having to iterate through the array of states to find the data I need. Like this:
foreach ($this->data['stateList'] as $state)
{
if ($state['stateCode'] == $searchParams['state'])
{
$stateSeg = $state['stateSeg'];
break;
}
}
$url = BASEURL . '/' . $stateSeg . ".html";
This seems inefficient to me. I think the most efficient solution I’ve been able to come up with is to turn states into objects and put them in array with multiple keys for stateCode, stateSeg, and stateName each pointing to the same state object, so they can be referenced like this:
stateList[‘CA’]->getStateSeg();
or
stateList[‘Arizona’]->getStateCode();
or
stateList[‘alaska-ak’]->getStateName();
etc…
This also seems like kind of a hack which would result in a rather large array (150 keys pointing to 50 objects) with replicated data (keys replicating data stored within objects).
Anyway, just thought I’d see if there is some kind of pattern for this type of problem. This array of states isn't the only thing I’ve come across where I’ve had to do this sort of iterative searching on multidimensional arrays to find corresponding values.
Question is tagged PHP and the code above is in PHP, but I am interested in elegant solutions in any language.
If php supports references and I know the state, I'd just pass a reference to the appropriate array element and extract from it the necessary field.
Alternatively, if you never know in advance what state you can get, create and use a map (associative container/array), let its efficient implementation take care of quickly finding whatever you need. Seems like you may need several of them.
Also, I wonder if you could get rid of everything except the "alaska-ak" strings. The data appears highly redundant.
I think your basic idea with the object and the arrays is not that bad, but instead of creating actually objects, I would just refer to the existing objects (better: array data). Let's see your original list again:
array
0 => &
array
'stateCode' => string 'AL' (length=2)
'stateName' => string 'Alabama' (length=7)
'stateSeg' => string 'alabama-al' (length=10)
1 => &
array
'stateCode' => string 'AK' (length=2)
'stateName' => string 'Alaska' (length=6)
'stateSeg' => string 'alaska-ak' (length=9)
2 => &
...
Each state object has an identifier, the array key: 0, 1, 2, ... .
All you need to do is to create three indexes based on key. You use the value as key (e.g. "AL" for "stateCode" index) and as value you take the array index, 0:
$indexStateCode['AL'] = 0;
You can then already look this up quickly:
$states[$indexStateCode['AL']];
Encapsulate this into a class with ArrayAccess and then on request instantiate the state object. You don't need it earlier.
Could you store the states in a mysql/sqlite table and use the database engine to do the lookup?
This seems inefficient to me
It isn't. Even worse-case, iterating through 50 items is probably an order of magnitude faster than querying a db.
a library to get a list of states
Not sure why you'd need a library to do this. But I'd either change the library to return the array how you need it, or wrap it in another module.
The data is somewhat redundant... All you need is two items: the state code and the state name. You can construct the "state seg" from those two. So keep a state code map and a state name map.

PHP String Split

I need to split a string into chunks of 2,2,3,3 characters and was able to do so in Perl by using unpack:
unpack("A2A2A3A3", 'thisisloremipsum');
However the same function does not work in PHP, it gives this output:
Array
(
[A2A3A3] => th
)
How can I do this by using unpack? I don't want to write a function for it, it should be possible with unpack but how?
Thanks in advance,
Quoting the manual page of unpack :
unpack() works slightly different
from Perl as the unpacked data is
stored in an associative array.
To accomplish this you have to
name the different format codes and separate them by a slash /.
Which means that, using something like this :
$a = unpack("A2first/A2second/A3third/A3fourth", 'thisisloremipsum');
var_dump($a);
You'll get the following output :
array
'first' => string 'th' (length=2)
'second' => string 'is' (length=2)
'third' => string 'isl' (length=3)
'fourth' => string 'ore' (length=3)
I've never used this function, but according to the documentation, the A character means "SPACE-padded string". So I'd hazard a guess that it's only taking the first two characters of the first word.
Have you tried unpack("A2A2A3A3", 'this is lorem ipsum'); ?

Categories