How to pass entire objects from Controller to Twig in Symfony 3? - arrays

I'm triyng to pass an object to Twig.
The object is the representation of an Entity, obtained via the
getDoctrine()->getManager()->getRepository(/*repoName*/)->find(id);
This actually works but how can I display all of its values in a html table in Twig?
I tried serialization but with no success, maybe I am missing something, please help.
Thanks in advance!
UPDATE:
What I actually want to achieve is to iterate to that object WITHOUT knowing its keys, a sort of
foreach (field in object) print key, value

$object = $em->getDoctrine()->getManager()->getRepository(/*repoName*/)->find(id);
You need pass this variable to the template by :
return $this->render('Anypath/your_template.html.twig', ['obj'=>$object]);
than from the twig :
{{obj.id}} or {{obj.name}}
depends on your fields inside object.

In your controller:
return $this->render('path/template.html.twig', ['entity'=>$entity]);
and in your template (replace your_attribute_name by any attribute of your entity):
{{ entity.your_attribute_name }}

Once you send your object to you twig template
return $this->render("AppBundle:Records:template.html.twig", [
"$object" => $object
]);
You can just do:
{{ object.field }}
That correspond to doing $object->getField() in PHP
Then just build your list manually in your twig
You you wanna loop through your object have a look at thise subject
Twig iterate over object properties
{% for key, value in my_object|cast_to_array %}
This might help

Related

Get original object name from firebase object inside ng-repeat

I get from firebase an object with the following code:
var ref = firebase.database().ref().child("categories");
ref.orderByChild("status")
.equalTo('featured')
.on('value', function(snap) {
console.log('Categories in range', snap.val());
$scope.categories = snap.val();
})
The object I get contains a couple of objects with the data which look like my example here:
Example Object from firebase
The results i use in my template view with ng-repeat.
<ul class="list">
<li ng-repeat="(category_id, category) in categories | toArray | orderBy:'position'">
<div style="background-image: url({{category.images[0].src}});" ng-click="category_action(category_id)">{{category.name}}
</div>
</li>
My problem is now that I want to give the original object name back with ng-click. I tried to use the (key, value) solution from angular for ng-repeat but I dont get the original object name back and only a index number like 0,1,2,3 etc.
What I need is the original name like in this example KU8JfAZRCsPJy9uOMqm.
If i only get the index number (0,1,2) back how can I know in my controller which object name this was originaly? Like for example $scope.categories.???
Thank you for your help in advance, I did research but found only people with the same problem but no working solution.
PS: I use firebase 3.x and angular 1.x

How do I ask AngularJS to recalculate the value of a complex function?

I have an AngularJS controller with the following function:
$scope.getExampleValue = function(exampleId) {
// calculate a value using underscore's _.where()
// clause against two JSON arrays on the $scope
// and the exampleId parameter passed in
return computedValue;
}
The function's parameter (exampleId) is rendered from the server, so the resulting HTML looks like this:
<div ng-controller="ExampleController">
...
<span>{{ getExampleValue(3) }}</span>
<span>{{ getExampleValue(4) }}</span>
<span>{{ getExampleValue(5) }}</span>
...
</div>
The problem I have is that AngularJS doesn't know to call getExampleValue() again when the JSON arrays used in the function have changed: they're 2 simple JSON arrays, new JSON items can be added or removed, or properties of existing JSON items in either array can be modified which affect the result.
I've looked at $scope.watch() and $scope.watchCollection() but I'm unsure how I can use them without changing my approach to bind against already computed values rather than against the function I prefer.
Essentially I think I'm asking how to notify AngularJS that a complicated bound value has changed, then I could wrap that notification up in a $scope.watch()..
Thanks for your help.
You are looking for $scope.$apply:
When you change the JSON asyncly, run:
$scope.$apply(function () {
// update the properties on $scope
});

Display one item from array in AngularJS without using ng-repeat

I have JSON array like:
var data = [
{
"name": "Jim",
"age" : 25
},
{
"name": "Jerry",
"age": 27
}
];
in HTML:
<ul>
<li ng-repeat="val in data | filter:{age: age}:true">{{val.name}}</li>
</ul>
The above code is working fine as i want the output, but i don't want to use ng-repeat where it creates loop and then i filter.
is there any another way where i can directly select the DATA from array where age = age ??
Filters can be used in any angular expression (there is nothing ngRepeat-specific about them).
(They can be even used in JavaScript.)
E.g. the following expression displays the data associated with the first user (limitTo:1) that has an age of 25 (filter:{age: 25}), formatted as JSON (json):
{{data | filter:{age: 25}:true | limitTo:1 | json}}
See, also, this short demo.
As mentioned by #link in comments, there is no way to pluck the object with the required age from the array without looping through it. However, you're right in thinking that using ng-repeat here is not appropriate if you only want to display a single object from the array, so ideally the required object should be stored in your controller.
One approach would be to use $filter in your controller instead:
$scope.obj_with_correct_age = $filter("filter")(data, {age: age});
<li>{{ obj_with_correct_age.name }}</li>
I realize this is an old thread but for completeness.
This works:
{{data.Systems[data.Detail.SystemId].System}}
This way, you don't need to filter the data.
If you want to parse single value of name. like {"name":"value"}.
so after $http post, i am assuming that your response in var data= response.
Now find, like console.log(data[0].name);
Here, result will be Jim

AngularJS - connect ng-options to an object named in a property

I'm working on a form generator implementation, and I want to have ng-options evaluate a property of the current 'field' and return the select 'options' as listed in something like $scope.options.
This means that I can setup the 'option' containing objects in my controller, and have the form elements use the correct object to populate the dropdown.
My form is built from a relational database, so hard coding the target object isn't going to help (although it does work, it's just not scalable)
I'm stuck figuring out how to get ng-options to evaluate an expression which makes the jump from variable to object name.
Maybe there's just a better way to do this altogether?
<select id="{{field.Name}}" ng-model="field.Value"ng-options="option.ID as option.Value for option in [SOMETHING THAT EVALUATES TO AN OBJECT NAMED IN THE BINDING PROPERTY]" ng-required="field.Required"></select>
I have a fiddle here: JSFiddle
I would make an object containing objects of your options
$scope.optiongroups = {group1: [...], group2: [...]}
and then
ng-options="value as option for option in optiongroups[dynamic var here]"

AngularJS not able to include markup in IDs?

I have an array of 3 items and want them to be "ng-repeated"
<li ng-repeat="item in obj.items id="testobj{{testobj.number}}">
</li>
When I look at the page, it appears that the id of the "li" is just "testobj" for all 3 items and not testobj1 testobj2 testobj3 like I was expecting. What is the issue?
Your ng-repeat attribute is missing a final ".
The {{ }} binding is probably coming back with no data and so being treated as if it was an empty string. I see no reference to testobj (the scope variable) anywhere outside of your binding. Is this defined, or should your id read id="testobj{{item.number}}" or something similar?

Resources