在我的控制器中,我只是在我的树枝视图上传递了一些用户。 这是我的功能:
public function viewAction($id)
{
$em = $this->getDoctrine()->getManager();
$planningRepo = $em->getRepository('SimonPediBundle:Planning');
$userRepo = $em->getRepository('SimonUserBundle:User');
$user = $this->getUser();
$planningId = $user->getPlanning()->getId();
$planning = $planningRepo->find($planningId);
$users = $userRepo->findByPlanning($planningId);
if (null === $planning) {
throw new NotFoundHttpException("Le planning d'id ".$id. "n'existe pas.");
}
return $this->render('planningAction/planning.html.twig', array('planning' => $planning,
'users' => $users,));
}
这是我的枝条视图,我想在html表格上显示一些信息:
<tbody>
<tr>
<th scope="row">Responsable</th>
{% for i in 1..5 %}
{% set assigned_user = ' ' %}
{% for user in users if user.planningday == i%}
{% set assigned_user = user %}
{% endfor %}
<td>{{assigned_user.name}} {{assigned_user.lastname}}</td>
{% endfor %}
</tr>
<tr>
<th scope="row">Description</th>
{% for i in 1..5 %}
{% set assigned_user = ' ' %}
{% for user in users if user.planningday == i%}
{% set assigned_user = user %}
{% endfor %}
<td>{{assigned_user.planningcontent}}</td>
{% endfor %}
</tr>
</tbody>
我收到了这个错误: 无法访问字符串变量(“”)上的属性(“名称”)。 对于这一行:
<td>{{assigned_user.name}} {{assigned_user.lastname}}</td>
感谢您的帮助!
这是我的用户实体
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="Simon\UserBundle\Repository\UserRepository")
*/
class User extends BaseUser
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
* @Assert\NotBlank(message="Merci d'entrer votre prénom.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=50,
* minMessage="Prénom trop court",
* maxMessage="Prénom trop long",
* groups={"Registration", "Profile"}
* )
*/
protected $name;
/**
* @var string
*
* @ORM\Column(name="lastname", type="string", length=255)
* @Assert\NotBlank(message="Merci d'entrer votre nom.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=50,
* minMessage="Nom trop court",
* maxMessage="Nom trop long",
* groups={"Registration", "Profile"}
* )
*/
protected $lastname;
/**
* @var string
*
* @ORM\Column(name="phone", type="string", length=12)
* @Assert\NotBlank(message="Merci d'entrer un numéro de téléphone.", groups={"Registration", "Profile"}))
* @Assert\Length(
* min=10,
* max=10,
* minMessage="Entrez un numéro de téléphone valide",
* maxMessage="Entrez un numéro de téléphone valide",
* groups={"Registration", "Profile"}
* )
*/
protected $phone;
/**
* @var boolean
*
*@ORM\Column(name="hasAdvert", type="boolean", nullable=true)
*/
protected $hasAdvert;
/**
* @var boolean
*
* @ORM\Column(name="isAdmin", type="boolean", nullable=true)
*/
protected $isAdmin;
/**
*
* @ORM\ManyToOne(targetEntity="Simon\PediBundle\Entity\Planning", cascade={"persist"})
*
*/
protected $planning;
/**
* @var int
*
* @ORM\Column(name="planningday", type="smallint", nullable= true)
*/
protected $planningday;
/**
* @var text
*
* @ORM\Column(name="planningcontent", type="text", nullable= true)
*/
protected $planningcontent;
/**
*@ var string
* @ORM\Column(name="address", type="string", length=255)
*/
protected $address;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return User
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set lastname
*
* @param string $lastname
*
* @return User
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set phone
*
* @param string $phone
*
* @return User
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set hasAdvert
*
* @param boolean $hasAdvert
*
* @return User
*/
public function setHasAdvert($hasAdvert)
{
$this->hasAdvert = $hasAdvert;
return $this;
}
/**
* Get hasAdvert
*
* @return boolean
*/
public function getHasAdvert()
{
return $this->hasAdvert;
}
/**
* Set isAdmin
*
* @param boolean $isAdmin
*
* @return User
*/
public function setIsAdmin($isAdmin)
{
$this->isAdmin = $isAdmin;
return $this;
}
/**
* Get isAdmin
*
* @return boolean
*/
public function getIsAdmin()
{
return $this->isAdmin;
}
/**
* Set planningday
*
* @param integer $planningday
*
* @return User
*/
public function setPlanningday($planningday)
{
$this->planningday = $planningday;
return $this;
}
/**
* Get planningday
*
* @return integer
*/
public function getPlanningday()
{
return $this->planningday;
}
/**
* Set planningcontent
*
* @param string $planningcontent
*
* @return User
*/
public function setPlanningcontent($planningcontent)
{
$this->planningcontent = $planningcontent;
return $this;
}
/**
* Get planningcontent
*
* @return string
*/
public function getPlanningcontent()
{
return $this->planningcontent;
}
/**
* Set address
*
* @param string $address
*
* @return User
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* @return string
*/
public function getAddress()
{
return $this->address;
}
/**
* Set planning
*
* @param \Simon\PediBundle\Entity\Planning $planning
*
* @return User
*/
public function setPlanning(\Simon\PediBundle\Entity\Planning $planning = null)
{
$this->planning = $planning;
return $this;
}
/**
* Get planning
*
* @return \Simon\PediBundle\Entity\Planning
*/
public function getPlanning()
{
return $this->planning;
}
}
答案 0 :(得分:1)
如果您正在使用对象,则无需将默认值设置为字符串。将其设置为null
并检查是否找到了用户
<tbody>
<tr>
<th scope="row">Responsable</th>
{% for i in 1..5 %}
{% set assigned_user = null %}
{% for user in users if user.planningday == i%}
{% set assigned_user = user %}
{% endfor %}
<td>
{% if not assigned_user is null %}
{{assigned_user.name}} {{assigned_user.lastname}}
{% endif %}
</td>
{% endfor %}
</tr>
<tr>
<th scope="row">Description</th>
{% for i in 1..5 %}
{% set assigned_user = null %}
{% for user in users if user.planningday == i%}
{% set assigned_user = user %}
{% endfor %}
<td>
{% if not assigned_user is null %}
{{assigned_user.planningcontent }}
{% endif %}
</td>
{% endfor %}
</tr>
</tbody>
答案 1 :(得分:0)
使用此行
{% set assigned_user = ' ' %}
将“assigned_user”的值设置为“”。 如果您没有符合此条件的用户
{% for user in users if user.planningday == i%}
该值将保持字符串而不是对象。