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

标签 php symfony twig

在我的 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/

相关文章:

php - 使用 jquery 跨站点发布 JSON 数据

unit-testing - PHPUnit - Setter 不修改我的模拟

php - Symfony2 Twig 获取子实体的总计数

php - 由一系列国家/地区填充的下拉列表

PHP 7.2.14 json_decode 函数对浮点值进行四舍五入

php - WordPress 过去三天的所有帖子

php - 投票脚本,简化数据库查询的可能性

javascript - 当原始来源不可用时,如何从自己的服务器加载 JQuery?

php - FOSUserBundle 自定义错误信息 change_password

twig - Twig_Error_Syntax :cannot extend from a block