I have a many-to-many relationship. Entities are Actor and Pys (Films and TV shows). When I want the cast of film the relation works fine (returns all the actor and actress of a film) but the problem comes when I want to retrive all the films of an actor.
This is the relation in the phpmyadmin:
This is the error that appear:
Key "actor" for array with keys "0" does not exist in ActorBundle:Default:actor.html.twig at line 3
Here the necessary code:
Actor's DefaultController
class DefaultController extends Controller
{
public function peliculasAction($actId)
{
$em = $this->getDoctrine()->getManager();
$peliculas = $em->getRepository('ActorBundle:Actor')->findPeliculas($actId);
return $this->render('ActorBundle:Default:actor.html.twig', array('peliculas' => $peliculas
));
}
}
ActorRepository
class ActorRepository extends EntityRepository
{
public function findPeliculas($actId)
{
$em = $this->getEntityManager();
$consulta = $em->createQuery('
SELECT p, d, a
FROM ActorBundle:Actor a
JOIN a.pys p JOIN p.director d
WHERE a.actId = :actId
ORDER BY p.pysAnyo DESC
');
$consulta->setParameter('actId', $actId);
return $consulta->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker')->getResult();
}
}
actor.html.twig
(...)
{% block title %}{{ peliculas.actor.actNombre ~ ' ' ~ peliculas.actor.actApellidos }}{% endblock %}
{% block article %}
<section class="article">
<div id="datos-actor-director-genero">
<strong>{{ 'Nombre:' | trans }} </strong>{{ peliculas.actor.actNombre ~ ' ' ~ peliculas.actor.actApellidos }}</br>
<strong>{{ 'Fecha de nacimiento:' | trans }} </strong>{{ peliculas.actor.actFechaNacimiento | localizeddate('long', 'none') }}
</div>
<table>
{% for pys in peliculas %}
<tr id="fechaVotacion">
<td colspan="4">{{ pys.pysAnyo }}</td>
(...)
You are getting a doctrine collection from
$em->getRepository('ActorBundle:Actor')->findPeliculas($actId);
So in Twig, peliculas is a collection too, do a for..in or peliculas[0] if you only want the first element.
peliculas has no key actor, only numbres (0 : first actor, 1 : second actor, etc...)
Related
Symfony 3.0 :
In my project, I have many entities which contain more than 50 fields, so for the twig which shows every entity, I decided to automate the display of the 50 fields by a simple loop.
First problem: how to get entity's all fields names, I resolved this by creating a custom twig filter:
<?php
// src/HomeBundle/Twig/HomeExtension.php
namespace HomeBundle\Twig;
class HomeExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
new \Twig_SimpleFilter('object_keys', array($this, 'getObjectKeys')),
);
}
public function getObjectKeys($object)
{
//Instantiate the reflection object
$reflector = new \ReflectionClass( get_class($object) );
//Now get all the properties from class A in to $properties array
$properties = $reflector->getProperties();
$result=array();
foreach ($properties as $property)
$result[] = $property->getName();
return $result;
}
public function getName()
{
return 'app_extension';
}
}
?>
The second problem which generates the error right now is: how to access object properties within a loop:
{% for property in article|object_keys%}
<tr>
<th>
{{property|capitalize}}
{# that's work clean #}
</th>
<td>
{{ attribute(article,property) }}
{# that's generate the error #}
</td>
</tr>
{% endfor %}
The error :
An exception has been thrown during the rendering of a template
("Notice: Array to string conversion"). 500 Internal Server Error -
Twig_Error_Runtime
Finally, the error is fixed on the getObjectKeys method of the filter,
so when it returns an array that I create manually it works:
return array("reference","libelle");
But, when I send an array created within a loop => Error.
I dumped the two arrays in the twig, they were equivalents, but the second still generating an error.
Most likely one of your properties is returning an array rather than a simple string, integer, .... A solution here could be to store the value in a variable and check whether the stored value is an array. Depending on that check do something with the value or otherwise just output the variable
{% for property in article|object_keys%}
<tr>
<th>
{{property|capitalize}}
</th>
<td>
{% set value = attribute(article,property) %}
{% if value is iterable %}{# test if value is an array #}
{{ value | join(', ') }}
{% else %}
{{ value }}
{% endif %}
</td>
</tr>
{% endfor%}
I have one entity Article and an other entity Image with a bidrectional relation OneToMany and ManyToOne :
class Article
{
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Image", mappedBy="article")
*/
private $images;
}
class Image
{
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Article", inversedBy="images")
* #ORM\JoinColumn(nullable=true)
*/
private $article;
}
In my controller I use #paramconverter to get the article I want :
/**
* #Route("/blog/{slug}", name="article")
* #ParamConverter("article", class="AppBundle:Article")
*/
public function articleAction(Article $article)
{
return $this->render('default/article.html.twig', array(
'article' => $article,
));
}
Now my problem is that I want to identify the ONLY image with the attributes "main = true" in all the "article.images" I have.
What is the best solution?
In my wiew I can do somehting like this but it's not the best I think :
{% for image in article.images %}
{% if image.main %}
<img src="{{ asset( image.src ) }}" alt="{{ image.alt }}" title="{{ image.title }}">
{% endif %}
{% endfor %}
I'd like to use something like :
{{ article.mainImg }}
How can I achieve this please? And is this the best solution?
Doctrine provides a collection filter mechanism you could use to get the "main image":
public function articleAction(Article $article)
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq("main", true))
->setMaxResults(1);
$mainImg = $article->getImages()->matching($criteria)->first();
return $this->render('default/article.html.twig', array(
'article' => $article,
'mainImg' => $mainImg
));
}
More information on filtering doctrine collections: Filtering collections
I did not test the code myself, but it should convey the idea of how it can be done.
I use This function to get data
public function UserAction()
{
$easyuser = $this->getDoctrine()->getrepository('AppBundle:User')->findall();
foreach($easyuser as $user){
$id = $user->getid();
$username = $user->getUsername();
$email = $user->getEmail();
$roles = $user->getRoles();
}
return $this->render('easycall/user.html.twig', ['easyuser' => $easyuser, 'roles' => $roles]);
}
and in twig i use this code to show data
{% for entity in easyuser %}
<tr>
<td>{{entity.id}}</td>
<td>{{entity.username}}</td>
<td>{{entity.email}}</td>
{% for role in entity.roles %}
<td>{{role}}</td>
{% endfor %}
</tr>
{% endfor %}
The problem is that i get all the roles if the user is ROLE_SUPER_ADMIN, i want to get only the first value from every array.
i tried something like reset() but it did'nt work, any suggestion??
This is also a picture how the results look likes.
1st item from array shoul be somethink like
{{entity.roles | first}}
but it's simply 1st item from array, i'm not sure if it always be more "powerfull role"
I have a problem with displaying data with age who I must calculate with birth_date attribute
Controller
public function listageAction()
{
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'select DATE_DIFF( CURRENT_DATE(),p.dateNaissance)
from PersonnelBundle:Personnel p'
);
$personnels = $query->getResult();
return $this->render('PersonnelBundle:Default:listage.html.twig');
}
View
{% for personnel in personnels %}
<li> {{ personnel.nomP }} {{ personnel.prenomP }} </li>
<li> {{ personnel.age}} </li>
{% endfor %}
I have this error:
Variable "personnels" does not exist in PersonnelBundle:Default:listage.html.twig at line 6
Your problem is not calculating a date, you haven't injected the $personnels variable into a twig template. Take 5 minutes to read the documentation on views.
http://symfony.com/doc/current/quick_tour/the_view.html
Specifically: this example is relevant to you.
$this->render('default/index.html.twig', array(
'variable_name' => 'variable_value',
));
I have a one to many relationship in doctrine.I want to count each related field and display them in Twig for loop
so far
A Vp is related to Voters.Vp has many Voters and Voters has one Vp
I want to count each related Voters per Vp
public function getAllVp()
{
return $this
->createQueryBuilder('v')
->select('vp.id,COUNT(v.id) as num')
->from('Voters', 'v')
->join('v.Vp', 'vp')
->orderBy('v.id', 'ASC')
->getQuery()
->getResult()
;
}
I want this in Twig like
{% for vp in vps %}
{{ vp.firstname }}
{{ vp.num }}//number of voters
{% endfor %}
controller
$vice_president = $em->getRepository('Bundle:Vp')->getAllVp();
return $this->render('Bundle:Vp:all_vp.html.twig', array(
'vps' => $vice_president,
));
doctrine
fields:
firstname:
type: string
length: 255
lastname:
type: string
length: 255
photo:
type: string
length: 255
oneToMany:
voters:
targetEntity: Voters
mappedBy: vp
I got this error
[Semantical Error] line 0, col 94 near 'vp, Voters v': Error: Class Project\Bundle\DuterteBundle\Entity\Vp has no association named Vp
How to correctly achieve this in Doctrine?
Update
voters.orm.yml
manyToOne:
vp:
targetEntity: Vp
cascade: { }
mappedBy: null
inversedBy: voters
joinColumn:
name: vp_id
referencedColumnName: id
orphanRemoval: false
I can achieved this by simply calling the related 'voters' and add a filter in Twig.But my intention is to count the data in doctrine, reuse it in other templates or convert it to json for the future,e.g in Angular JS
{% if vp.voters|length > 0 %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
<td>{{ vp.id }}</td>
<td>{{ vp.getFullName() }}</td>
<td>{{ vp.voters|length|number_format }}</td>
</tr>
{% endif %}
Above is a working code but I want to do the count in Doctrine , not in template
Expected result
id fullname counts
1 George Bush 45
2 ali gail 1999
4 Mae Young 45
......
First of all, you can remove mappedBy: null in your Voter mapping.
PHP oriented :
Ok you can try this PHP solution to add a new method in your entity Vp like :
public function getVotersCount(){
return count($this->voters);
}
And in your twig view you can do :
{{ vp.getVotersCount() }}
Doctrine oriented : (http://docs.doctrine-project.org/en/latest/reference/events.html#lifecycle-events)
In your Vp Entity orm mapping :
fields:
firstname:
type: string
length: 255
lastname:
type: string
length: 255
photo:
type: string
length: 255
oneToMany:
voters:
targetEntity: Voters
mappedBy: vp
lifecycleCallbacks:
postLoad: [ countVotersOnPostLoad ]
And also a new attribute, a getter and countVoters method :
protected $votersCount;
public function getVotersCount(){
return $this->votersCount;
}
public function countVotersOnPostLoad ()
{
$this->votersCount = count($this->voters);
}
And in your view, simply do :
{{ vp.votersCount }}
My work around for this is to create a service.
<?php
namespace Project\Bundle\DutBundle\Twig;
class AllVpExtension extends \Twig_Extension
{
protected $em;
public function __construct($em)
{
this->em = $em;
}
public function getFunctions()
{
return array(
//this is the name of the function you will use in twig
new \Twig_SimpleFunction('number_votes_vp', array($this, 'b'))
);
}
public function getName()
{
//return 'number_employees';
return 'vp_app_extension';
}
public function b($id)
{
$qb=$this->em->createQueryBuilder();
$qb->select('count(v.id)')
->from('DutBundle:Voters','v')
->join('v.vp','c')
->where('c.id = :x')
->setParameter('x',$id);
$count = $qb->getQuery()->getSingleScalarResult();
return $count;
}
}
Now in order to count each vp's related voters, I can call a service and send the result to twig
public function all_vpAction()
{
$em = $this->getDoctrine()->getManager();
$vice_president = $em->getRepository('DutBundle:Vp')->findAll();
//communicate to service container
$data = $this->container->get('duterte.twig.vp_app_extension');
$datas = array();
foreach ($vice_president as $value) {
$datas[] = array('id' => $value->getId(),'firstname' => $value->getFirstname() . ' ' . $value->getLastname(),'numbers' => (int)$data->b($value->getId()));
}
$vice = $datas;
return $this->render('DutBundle:Vp:all_vp.html.twig', array(
'vps' => $vice,
));
//or we can wrap this in json
$serializer = $this->container->get('jms_serializer');
$jsonContent= $serializer->serialize($vice,'json');
return $jsonContent;
}
With this set up, I can wrap this into json and and using custom twig filter, I can display data sorted either in Angular or in plain Twig template or both
By the way, my view
{% extends '::base.html.twig' %}
{% block body %}
{% block stylesheets %}
{{ parent() }}
<style type="text/css">
#img-responsive{
height: 320px;
/*width: 300px;*/
}
</style>
{% endblock %}
<div class="section-heading">
<h2>Best Tandem Of the Day</h2>
</div>
<div class="row">
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="/img/dut.jpg" id="img-responsive">
<div class="caption">
<h3>President</h3>
</div>
</div>
</div>
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="/img/unknown.jpg" id="img-responsive">
<div class="caption">
<h3>Vice-President</h3>
</div>
</div>
</div>
</div>
<hr />
<div ng-app="myApp" ng-controller="customersCtrl">
Search Here: <input type="text" placeholder="search" ng-model="searchMe"/><br />
<table class="table">
//names//
<thead>
<tr>
<th>Full Name</th>
<th>Middlename</th>
<th>Lastname</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in names">
<td>//x.id//</td>
<td>//x.firstname//</td>
<td>//x.numbers//</td>
</tr>
</tbody>
</table>
</div>
<div class="table-responsive">
<table class="table table-hover table-bordered table-condensed" id="table1">
<thead>
<tr>
<th>#</th>
<th>Bet</th>
<th>Votes</th>
<!--th>Photo</th-->
</tr>
</thead>
<tbody>
{% for v in vps | sortbyfield('numbers') %}
{% if v.numbers > 0 %}
<tr>
<td>{{ v.id }}</td>
<td>{{ v.firstname }}</td>
<td>{{ v.numbers }}</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script src="//code.angularjs.org/1.4.8/angular.js"></script>
<script>
var app = angular.module('myApp', []);
app.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('//');
$interpolateProvider.endSymbol('//');
});
app.controller('customersCtrl',['$scope','$http',function($scope, $http) {
$http.get("{{ path('vp_president') }}")
.success(function (response) {
$scope.names= JSON.parse(response);
});
</script>
{% endblock %}