I have a list of tasks stored in the doctrine database which I return like this:
$task = $this->getDoctrine()
->getRepository('SeotoolMainBundle:Tasks')
->findAll();
return array('form' => $form->createView(), 'message' => '', 'list_tasks' => $task);
In one row the ID of and entry from a second table is stored. I don't want only output the ID but also the Name, Description and so on stored in the other table. With normal PHP & MYSQL this would be done by using JOIN - how can I do it with Symfony & TWIG ?
Twig Output:
{% for task in list_tasks%}
{{ task.Id }}
{{ task.TaskTitle }}
{{ task.TaskDescription }}
{{ task.TaskTypes }} /* Here I want not only get the ID but also other fields stored in the database with TaskType ID = task.TaskTypes */
{{ task.User }} /* Here I want not only get the ID but also other fields stored in the database with User ID = task.User */
{% endfor %}
I am assuming that TaskType and User are entities, which belong within the Tasks entity. In which case, try the following. Note that I am getting just one task with id of 1 in my example:
$task = $this->getDoctrine()->getRepository('SeotoolMainBundle:Tasks')->find(1);
$taskTypes = $task->getTaskTypes();
$user = $task->getUser();
return array(
'form' => $form->createView(),
'message' => '',
'list_tasks' => $task,
'task_types' => $taskTypes,
'user' => $user,
);
And in your Twig:
{% for task_type in task_types %}
{{ task_type.Id }} // or whatever fields a task_type has
{% endfor %}
And the same for the user
Edit:
As you are wanting to have ALL tasks processed at once, I wonder if simply the following willl work:
{% for task_list in task_lists %}
{% for task_type in task_list.taskType %}
{{ task_type.Id }} // or whatever fields a task_type has
{% endfor %}
{% endfor %}
I did it now on this way, but I dont think, that it is 100% correct and conform.
How to do better? Luckily this works for me actually:
$task = $this->getDoctrine()
->getRepository('SeotoolMainBundle:Tasks')
->findAll();
$taskTypes = $this->getDoctrine()
->getRepository('SeotoolMainBundle:TaskTypes')
->findAll();
$user = $this->getDoctrine()
->getRepository('SeotoolMainBundle:User')
->findAll();
return array(
'form' => $form->createView(),
'message' => '',
'list_tasks' => $task,
'list_task_types' => $taskTypes,
'list_user' => $user
);
TWIG:
{% for task in list_tasks %}
Task ID: {{ task.ID }} <br/>
{% for type in list_task_types if type.id == task.tasktypes %}
{{ type.tasktypetitle }} <br/>
{% endfor %}
{% for user in list_user if user.id == task.user %}
{{ user.username }} <br/>
{% endfor %}
<hr/>
{% endfor %}
Related
I have a problem with properties in (probably) Twig. I have controller in Symfony where getCategories(), getWords(), getTranslations() methods (from Doctrine) return the objects (relations). Every property in the controller is an array because I call findAll() method (from Doctrine again) which returns the array. Finally I return all the properties from controller to view (Twig file) where I try display the results by Twig for loop.
The problem is the Twig loop only iterates over flashcards property (I know why ;)) and I have no idea how to make many-properties iterating. I'd like the loop to iterate over all properties returned by the controller.
In the controller foreach loop I tried update the flashcards array with new associative keys such as: category, word and translation so that all the results returned by Doctrine (including relations) are stored in one flashcards property but then Symfony throws exceptions.
I wondered if create one array in the controller to which I would push the flashcards, cateogry, word and translation arrays and then return this one array to the view but I don't think this is good practice.
Here's the controller method code:
public function showAllCards()
{
$flashcards = $this->getDoctrine()->getRepository(Flashcards::class)
->findAll();
foreach ($flashcards as $flashcard) {
$category = $flashcard->getCategories()->getName();
$word = $flashcard->getWords()->getWord();
$translation = $flashcard->getTranslations()->getWord();
}
return $this->render('try_me/index.html.twig', [
'flashcards' => $flashcards,
'category' => $category,
'word' => $word,
'translation' => $translation
]);
}
Here's the Twig loop code:
{% for flashcard in flashcards %}
{{ word }}
<br>
{{ flashcard.pronunciation }}
<br>
{{ flashcard.exampleSentence }}
<br>
{{ category }}
<br>
{{ translation }}
<br>
{% endfor %}
I tried to execute the following controller code...
public function showMeAll()
{
$flashcards = $this->getDoctrine()->getRepository(Flashcards::class)
->findAll();
foreach ($flashcards as $flashcard) {
$flashcards['categories'] = $flashcard->getCategories()->getName();
$flashcards['words'] = $flashcard->getWords()->getWord();
$flashcards['translations'] = $flashcard->getTranslations()->getWord();
}
return $this->render('try_me/index.html.twig', [
'flashcards' => $flashcards,
]);
}
...with the following Twig loop...
{% for flashcard in flashcards %}
{{ flashcard.words }}
<br>
{{ flashcard.pronunciation }}
<br>
{{ flashcard.exampleSentence }}
<br>
{{ flashcard.categories }}
<br>
{{ flashcard.translations }}
<br>
{% endfor %}
...but then Symfony says:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Proxies__CG__\App\Entity\Words could not be converted to string").
Could you give me some tips how to solve this problem, please? I'd like the Twig loop to iterates over many properties (flashcard, word, category, translation). Or write if there's a better solution, please.
Thank you in advance for every answer!
According to your snippets, I'm guessing you want something like the following:
{% for flashcard in flashcards %}
{% for word in flashcard.getWords() %}
{{ word }}<br />
{% endfor %}
{{ flashcard.getPronunciation() }}<br>
{{ flashcard.getExampleSentence() }}<br>
{% for category in flashcard.getCategories()() %}
{{ category.getName() }}<br />
{% endfor %}
{% for translation in flashcard.getTranslations() %}
{{ translation.getWord() }}<br />
{% endfor %}
{% endfor %}
Have a look at this section of the documentation. Basically if you had foo.bar, twig will test if bar is a public property of foo and if not test if there is a public getter, getBar, to fetch bar.
Some sidenotes in both of your loops, the values category, word and translation will only hold the last value of your flashcards, because you are overwriting the value each time.
I have been trying to figure out how I can pull the url of a node content type using an entity reference.
Apparently using {{ links.entity.uri }} or {{ links.entity.url }} does not work
<div>
{% for links in node.field_related_items %}
<h2>{{ links.entity.label }}</h2>
<a href="{{ links.entity.uri }} " ></a>
{% endfor %}
</div>
This worked for me:
This will only take you to /node/entity_id though and not /entity_name/entity_id
I would like to expand upon this answer by saying that you should add a Drupal::entityQuery() to your .theme file. Inside of this query, you'll want to grab the "alias" which is the link to the content. In your case this will give you these links /migrations and /aboutiom.
Here's an example of what that might look like...
function theme_name_preprocess_node(&$variables) {
$query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'related_links')
$nids = $query->execute();
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
$related_links = [];
foreach ($nodes as $item) {
$alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$item->id());
$related_links[] = [
'title' => $item->getTitle(),
'id' => $item->id(),
'field' => $item->get('field_name')->value,
'alias' => $alias,
];
}
$variables['related_links'] = $related_links;
}
Now in your twig template you can do something like this...
{% for link in related_links %}
{{ link['title'] }}
{% endfor %}
Why is it that when I write my test in php…
foreach ($rounds as $round){
$assignment = $em->getRepository(‘WorkBundle:Doc’)->findOneBy(array(
‘user’ => $user->getId(),
’whichRound’ => $round,
));
if (!$assignment){
echo “assign ”.$round. “ to user”;
}else{
echo “already assigned to ”.$round. “ to user”;
}
}
return array (
'user' => $user,
'assignment' => $assignment,
'rounds' => $rounds,
);
…it works correctly. When assignment is null, it will output the “assign ”.$round. “ to user”; and when it’s not null it will output “already assigned to ”.$round. “ to user”;.
However when I go in my twig template with the returned variables above and do…
{% for round in rounds %}
{% if assignment is null %}
<h2>{{ user }} successfully added to {{ round }}</h2>
{% else %}
<h2>{{ user }} has already been assigned to the {{ round }}</h2>
{% endif %}
{% endfor %}
…it does not work correctly? It will instead output the same message twice…in an example, if the first round is null and the second one it not null, it will output the second message {{ user }} has already been assigned to the {{ round }} twice.
What am I messing up?
When you go through the foreach loop in your code, you're setting $assignment each time. When you return the array, you're only returning the last time $assignment is set.
It looks like $rounds is an array of numbers and you'd like to associate the round with an assignment result. Based on that, I'd advise building a new array like this:
$results = array();
foreach ($rounds as $round) {
$row = array(
'round' => $round,
'assignment' => $em->getRepository('WorkBundle:Doc')->findOneBy(array(
'user' => $user->getId(),
'whichRound' => $round,
))
);
if ($row['assignment']) {
echo "Already assigned $round to user.";
} else {
echo "Assign $round to user.";
}
$results[] = $row;
}
return array(
'user' => $user,
'results' => $results,
);
Your Twig template would then look like this:
{% for row in results %}
{% if row.assignment is null %}
<h2>{{ user }} successfully added to {{ row.round }}</h2>
{% else %}
<h2>{{ user }} has already been assigned to the {{ row.round }}</h2>
{% endif %}
{% endfor %}
I currently have an array full of pre-populated form fields:
$fields = array('title','first_name')
$info = array(
'title' => 'Mr',
'first_name' => 'John',
'last_name' => 'Smith'
)
As you can see this particular fields array only contains the title and first name.
My objective is to cycle through the fields array and see if I have any information in my $info array to pre-populate the field with.
Something like:
foreach (fields as field) {
if (field is in $info array) {
echo the_field_value;
}
}
But obviously in Twig, currently I have something like:
{% for key, field in context.contenttype.fields %}
{% if key in context.content|keys %} << is array
{{ key.value }}<< get the value of the field
{% endif %}
{% endfor %}
Any help is greatly appreciated.
this example dump what you need:
{% set fields = ['title','first_name'] %}
{% set info = { 'title': 'Mr', 'first_name': 'John', 'last_name': 'Smith' } %}
{% for key in fields %}
{% if key in info|keys %}
{{ info[key] }}
{% endif %}
{% endfor %}
Result:
Mr John
Here the working solutions: http://twigfiddle.com/i3w2j3
Hope this help
I have form $form, which have text field for each entity User.
$form = $this->createFormBuilder();
$form->add('users', 'collection');
foreach ($query->getResult() as $user)
{
$form->get('users')->add($user->getName(), 'text', array('data' => $user->getName()));
}
I need to access specific input form by its ID number. So i have to be able to do something like this:
{{ form_widget(form.users[0]) }}
But it doesn't work. I can't use this:
{% for user in form.users %}
I need to access text fields specificly by their id's
Found the solution.
{% set interator = form.users.iterator %}
{{ interator.seek(0) }}
{{ form_widget(interator.current) }}