gpt4 book ai didi

php - 无法访问字符串变量的属性

转载 作者:行者123 更新时间:2023-12-01 13:32:28 25 4
gpt4 key购买 nike

在我的 Controller 中,我只是在我的 Twig View 上传递一些用户。
这是我的功能:

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,));
}

这是我的 Twig View ,我想在 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>

我收到了这个错误:
无法访问字符串变量 ("") 上的属性 ("name")。
对于这一行:
<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;
}
}

最佳答案

如果您正在使用对象,则无需将默认值设置为字符串。将其设置为 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>

关于php - 无法访问字符串变量的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45146952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com