How to access an entity within an entity in twig - symfony

In my controller, I return the entity so that my twig template can use it like so:
return $this->render('review/index.html.twig',[
"list" => $applications
]);
$applications is a query that returns the object I'm looking for:
$applications = $this->getDoctrine()->getRepository(ApplicationQueue::class)->findBy(
array("assignment_mod_user" => $this->getUser()->getId())
);
And within my twig, I use the dump function to see if it's retrieving what I'm looking for. This is what is returned:
As you can see, there are two entities associated to this entity. In twig when I tired to do this, it failed to retrieve the data within:
{% for application in list %}
{{application.application.[whateverhere]}}
{% endfor %}
How do I access the entities within an entity in twig when the data is already being pushed? The current output returns the error of:
Neither the property "application" nor one of the methods "application()", "getapplication()"/"isapplication()"/"hasapplication()" or "__call()" exist and have public access in class "App\Entity\ApplicationQueue".

You need to add an accessor to get the value of $application. This property is currently not accessible from outside the ApplicationQueue class.
class ApplicationQueue
{
protected $application;
public function getApplication(): CreatorApplication
{
return $this->application;
}
}

You have tu put double curly braces around your variable, like that :
{{ application.application.name }}
Look at the twig doc :
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}

Related

How can I determine if key is an object (twig)?

I want to determine if my key is an object:
{% for key in columns %}
{% if key is object %}
This is an object
{% else %}
This in not an object
{% endif %}
{% endfor %}
But I get the error message:
Unknown "object" test.
You can create your own Twig extension. I see you've tagged your question with Symfony so assuming you use Twig in Symfony, you can follow this tutorial:
https://symfony.com/doc/3.4/templating/twig_extension.html
What you need to do is add new TwigTest based on this example:
https://twig.symfony.com/doc/2.x/advanced.html#tests
You should end up with something like this:
// src/AppBundle/Twig/AppExtension.php
namespace AppBundle\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigTest;
class AppExtension extends AbstractExtension
{
public function getTests()
{
return array(
new TwigTest('object', array($this, 'isObject')),
);
}
public function isObject($object)
{
return is_object($object);
}
}
Code above is not tested, but should work fine.
An easy way to check if a variable is an object or a string :
{% if var.id|default('') is not same as ('') %}

Should I repack the fetched entity object before passing it to the twig template?

I'm using Symfony 4. I have a fetched object $user that has relationships with other entities. I have setup all the getter so I can get other information from that $user object.
$user = $em->getDoctrine()->getRepository(User::class)->find($id);
$usergroup = $user->getGroup()->getName();
What I'll do is to create a new object for repacking the information I needs from the $user object before passing it to the template.
# controller
$repack_user = new \stdClass();
$repack_user->id = $user->getId();
$user_friends = $user->getFrends();
$friends_gf = [];
foreach($user_friends as $friend) {
$friends_gf[] = $friend->getGirlfriend()->getName();
}
$repack_user->friends_gf = $friends_gf;
return $this->render("home.html.twig", ['user' => $repack_user]);
And then in the template, I unpacked it with similar procedures.
# twig template
{{ user.id }}
{% for gf in user.friends_gf %}
{{ gf }}
{% endfor %}
But since I can also call entity function inside twig, I can skip all the whole repacking in the controller and pass the $user object right into the template.
# skipped repack in controller
# twig template
{{ user.getID() }}
{% for friend in user.getfriends %}
{{ friend.getGirlfriend().getName() }}
{% endfor %}
The first way is kind of redundant because I have to do it twice. The second way is kind of hard to read. So which one is the more reasonable approach? What is the common practice for this?
Common practice is definitely to hand over your entity graph directly to the view.
Why do you think your second example is harder to read?
If you don't want those chained calls in the template you might want to consider adding another getter in your User entity. Something like:
public function getNamesOfFriendsGirlfriends()
{
$gfNames = [];
foreach($this->getFriends() as $friend) {
$gfNames[] = $friend->getGirlfriend()->getName();
}
return $gfNames;
}
And then call that in your template:
{% for gfName in user.namesOfFriendsGirlfriends %}
{{ gfName }}
{% endfor %}
And if you need many of these helpers and don't want to spoil your nice and clean entities you might want to consider wrapping them in a Decorator object before using it in the view layer.

Using raw Cypher to query Neo4j in Symfony

I am trying to go throught this tutorial: http://www.sitepoint.com/adding-social-network-features-php-app-neo4j/ But using the Symfony Framework instead of Silex.
I have been able to set up Neo4j to run with Symfony and am able to right user data to the graph. Now I would like to display all user email addresses in a list. I have taken this script:
public function home(Application $application, Request $request)
{
$neo = $application['neo'];
$q = 'MATCH (user:User) RETURN user';
$result = $neo->sendCypherQuery($q)->getResult();
$users = $result->get('user');
return $application['twig']->render('index.html.twig', array(
'users' => $users
));
}
And adapted it to read:
public function showUsersAction()
{
$em = $this->container->get('neo4j.manager');
$query = 'MATCH (n:`User`) RETURN n';
$users = $em->cypherQuery($query);
//print_r($users);
return $this->render('UserBundle:Account:showUsers.html.twig', array('users' =>$users));
}
And The twig looks as follows:
{% extends '::base.html.twig' %}
{% block content %}
<h1>get all users:</h1>
<ul>
{% for user in users %}
<li>{{ user.property('email') }}</li>
{% endfor %}
</ul>
{% endblock %}
But something in the twig is wrong, im getting the error:
Method "property" for object "Everyman\Neo4j\Query\Row" does not exist in UserBundle:Account:showUsers.html.twig at line 6
The problem was found in the syntax of the twig file. After consulting this page: https://github.com/jadell/neo4jphp/wiki/Cypher-and-gremlin-queries it became clear, that I had to include user['n'] in my twig template. The twig template now looks as such:
{% extends '::base.html.twig' %}
{% block content %}
<h1>get all users:</h1>
<ul>
{% for user in users %}
<li>{{ user['n'].getProperty('email') }}</li>
{% endfor %}
</ul>
{% endblock %}
I'm the author of the article you mentioned. The thing is that you use a different neo4j library than the one used in the article, hence neoclient, so the methods used in the article are different than the methods provided with neo4jphp.
As NeoClient uses heavily the Symfony components, integrating it in Symfony is really easy, you just need to override the DI. Example here : https://github.com/graphaware/GithubNeo4j/tree/master/src/GraphAware/Neo4jBundle
You'll then be able to use the methods illustrated in the 3 articles I wrote on Sitepoint.
So your problem with the twig template is that he doesn't find the getProperty method of the node object class, which is normal as neo4jphp returns Row object classes.
If you switch back to neoclient, as in the article, in the Twig template you can just write :
{% for user in users %}
<li>{{ user.getProperty('email') }}</li>
{% endfor %}

SonataAdmin: replace ID in breadcrumbs

How can I replace Object's ID in SonataAdmin breadcrumbs by some other text?
If I set __toString() in my document, it works only for editing. When I attempt to create new record, there is something like MyDocument:0000000000e09f5c000000006a48ef49 in the last breadcumb.
I'm searching for a method which allows me to set some text as the last breadcump if Document::toString() returns null.
This behaviour is implemented directly in the entity:
public function __toString()
{
return $this->getFoo() ? : '-';
}
Bundles are using variants of this, including return (string)$this->getFoo(); or $this->getFoo() ? : 'n/a'; etc.
Related question: toString method for SonataAdminBundle Listing in Symfony2
BTW something cool to know, you can completely customize the breadcrumb via a Twig template:
{% block sonata_breadcrumb %}
{% set _breadcrumb %}
<li>Home</li>
<li>Library</li>
<li class="active">Data</li>
{% endset %}
{{ parent() }}
{% endblock %}

How can I change symfony2 form fields default options globally?

Is there a way to change default options for form fields globally in symfony2?
More specifically, I want to change the render of ALL datetime fields to use single_text instead of the default choice widget.
Can it be done? Or do I need to implement a custom type and set the default in there, like for example the birthdate type?
I prefer an option that leads to minimal changes in the codebase.
The post is old, but you can use an alternative method, overriding the DateType symfony class ...
service.yml
services:
form.type.date:
class: "YourApp\YourBundle\Form\DateType"
tags:
- { name: "form.type", alias: "date" }
DateType.php
<?php
namespace YourApp\YourBundle\Form;
use Symfony\Component\Form\Extension\Core\Type\DateType as SymfonyDateType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class DateType extends SymfonyDateType
{
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions( $resolver );
$resolver->setDefault( 'widget', 'single_text' );
}
}
You can check if the service is taken by container
$ ./app/console debug:container | grep form.type.date
form.type.date YourApp\YourBundle\Form\DateType
form.type.datetime Symfony\Component\Form\Extension\Core\Type\DateTimeType
You have to define a form theme.
It's very easy and requires only a little bit coding time. First of all, you have to know which block to customize; in that case, you can do something like
{% block my_data_widget %}
{% spaceless %}
{% if type is defined and type == 'date' %}
// do all your customization
{% else %}
// don't know ...
{% endif %}
{% endspaceless %}
{% endblock form_widget_simple %}
Now that you have defined this snippet of code, you can use it into your main template (or whatever you use into your form views) in that way
{% form_theme form 'YourBundle:Form:myDataWidget' %}
Last but not least important, you have to place your form theme into Resources/views folder. In my example, your path will be Resources/views/Form/myDataWidget
Update
Did you tried with
{% set type = type|default('single_text') %}
or something like that?

Resources