尝试更新用户时出现Symfony2错误

时间:2013-10-18 17:23:24

标签: php symfony doctrine-orm

当我尝试更新捆绑包中的用户时出现此错误。我不确定发生了什么以及如何解决它。任何帮助将非常感激。谢谢!

 Catchable Fatal Error: Object of class WIC\UserBundle\Entity\User could not be converted to string in /Applications/XAMPP/xamppfiles/htdocs/symfonydev/app/cache/dev/classes.php line 5662 

我的用户实体:

<?php

namespace WIC\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use WIC\CommonBundle\DoctrineExtensions\Mapping\Annotation as Common;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;


/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="WIC\UserBundle\Entity\UserRepository")
 * @Common\Loggable(logEntryClass="WIC\UserBundle\Entity\UserLog")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 * @UniqueEntity(fields="username", message="Username has already been taken.")
 * @UniqueEntity(fields="email", message="Email Address has already been taken.")
 */
class User implements UserInterface, \Serializable
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @var string
 *
 * @ORM\Column(name="username", type="string", length=20, unique=true, nullable=false)
 * @Common\Versioned
 * @Assert\NotBlank
 * @Assert\Regex(
 *  pattern="/^[a-zA-Z0-9_-]{5,20}$/",
 *  message="Username can only have letters, numbers, underscore and hyphen. Must be 5 to 20 characters long."
 * )
 */
protected $username;

/**
 * @ORM\Column(type="string", length=32)
 */
protected $salt;

/**
 * @var string
 *
 * @ORM\Column(name="password", type="string", length=40)
 * @Common\Versioned
 * @Assert\NotBlank
 * @Assert\Regex(
 *  pattern="/^.*(?=.{5,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/",
 *  message="Password must be at least 5 characters long and contain at least one lower case letter, one upper case letter and one number."
 * )
 */
protected $password;

/**
 * @var string
 *
 * @ORM\Column(name="first_name", type="string", length=30)
 * @Common\Versioned
 * @Assert\NotBlank(message="Please Enter Your First Name")
 */
protected $first_name;

/**
 * @var string
 *
 * @ORM\Column(name="last_name", type="string", length=30)
 * @Common\Versioned
 * @Assert\NotBlank(message="Please Enter Last Name")
 */
protected $last_name;

/**
 * @var string
 *
 * @ORM\Column(name="email", type="string", length=255, nullable=false)
 * @Common\Versioned
 * @Assert\NotBlank(message="Please Enter A Valid Email Address")
 * @Assert\Email
 */
protected $email;

/**
 * @var integer
 *
 * @ORM\Column(name="status", type="integer", nullable=false)
 * @Common\Versioned
 */
protected $status;

/**
 * @ORM\OneToOne(targetEntity="WIC\PreferencesBundle\Entity\PreferencesUser", mappedBy="user")
 */
protected $preferences;

/**
 * @ORM\ManyToOne(targetEntity="WIC\AccountBundle\Entity\Account", inversedBy="users", cascade={"remove","persist"})
 * @ORM\JoinColumn(name="account_id", referencedColumnName="id", nullable=false)
 * @Common\Versioned
 */
protected $account;

/**
* @ORM\ManyToMany(targetEntity="WIC\RoleBundle\Entity\Role")
* @ORM\JoinTable(name="user_role",
*   joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
*   inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*/
protected $userRoles;

/**
 * @var datetime $created
 *
 * @Common\Timestampable(on="create")
 * @ORM\Column(type="datetime")
 */
protected $created;

/**
 * @var datetime $updated
 *
 * @Common\Timestampable(on="update")
 * @ORM\Column(type="datetime", nullable=true)
 */
protected $updated;

/**
 * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
 */
protected $deletedAt;


public function __construct()
{
    $this->status = 1;
    $this->salt = md5(uniqid(null, true));
    $this->userRoles = new ArrayCollection();
}

/*    
public function __sleep()
{
    return array('id');
}
*/

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set username
 *
 * @param string $userName
 * @return User
 */
public function setUserName($userName)
{
    $this->username = $userName;

    return $this;
}

/**
 * @inheritDoc
 */
public function getUserName()
{
    return $this->username;
}

/**
 * @inheritDoc
 */
public function getSalt()
{
    return $this->salt;
}

/**
 * Set password
 *
 * @param string $password
 * @return User
 */
public function setPassword($password)
{
    if ($password) {
        $this->password = $password;
    }  
    return $this;
}

/**
 * @inheritDoc
 */
public function getPassword()
{
    return $this->password;
}

/**
 * Set first_name
 *
 * @param string $firstName
 * @return User
 */
public function setFirstName($firstName)
{
    $this->first_name = $firstName;

    return $this;
}

/**
 * Get first_name
 *
 * @return string 
 */
public function getFirstName()
{
    return $this->first_name;
}

/**
 * Set last_name
 *
 * @param string $lastName
 * @return User
 */
public function setLastName($lastName)
{
    $this->last_name = $lastName;

    return $this;
}

/**
 * Get last_name
 *
 * @return string 
 */
public function getLastName()
{
    return $this->last_name;
}

/**
 * Set email
 *
 * @param string $email
 * @return User
 */
public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

/**
 * Get email
 *
 * @return string 
 */
public function getEmail()
{
    return $this->email;
}

/**
 * Set account
 *
 * @param \WIC\AccountBundle\Entity\Account $account
 * @return User
 */
public function setAccount(\WIC\AccountBundle\Entity\Account $account = null)
{
    $this->account = $account;

    return $this;
}

/**
 * Get account
 *
 * @return \WIC\AccountBundle\Entity\Account 
 */
public function getAccount()
{
    return $this->account;
}

/**
 * @inheritDoc
 */
public function getRoles()
{
    return $this->userRoles->toArray();
}

/**
 * @inheritDoc
 */
public function eraseCredentials()
{
}

/**
 * @see \Serializable::serialize()
 */
public function serialize()
{
    return serialize(array(
        $this->id,
        $this->username,
        $this->password
    ));
}

/**
 * @see \Serializable::unserialize()
 */
public function unserialize($serialized)
{
    list (
        $this->id,
        $this->username,
        $this->password
    ) = unserialize($serialized);
}

/**
 * Add userRole
 *@var \WIC\RoleBundle\Entity\Role
 * @param WIC\RoleBundle\Entity\Role $role
 */
public function addUserRole(\WIC\RoleBundle\Entity\Role $role)
{
    $this->userRoles[] = $role;

    return $this;
}

/**
 * Remove userRole
 *@var \WIC\RoleBundle\Entity\Role
 * @param WIC\RoleBundle\Entity\Role $role
 */
public function removeUserRole(\WIC\RoleBundle\Entity\Role $role)
{
    $this->userRoles->removeElement($role);

    return $this;
}


/**
 * Get userRoles
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getUserRoles()
{
    return $this->userRoles;
}

/**
 * Set userRoles
 *
 * @return User
 */
public function setUserRoles($roles)
{
    $this->userRoles = $roles;

    return $this;
}    

/**
 * Set created
 *
 * @param \DateTime $created
 * @return User
 */
public function setCreated($created)
{
    $this->created = $created;

    return $this;
}

/**
 * Get created
 *
 * @return \DateTime
 */
public function getCreated()
{
    return $this->created;
}

/**
 * Set updated
 *
 * @param \DateTime $updated
 * @return User
 */
public function setUpdated($updated)
{
    $this->updated = $updated;

    return $this;
}

/**
 * Get updated
 *
 * @return \DateTime
 */
public function getUpdated()
{
    return $this->updated;
}

/**
 * Set deletedAt
 *
 * @param \DateTime $deletedAt
 * @return User
 */
public function setDeletedAt($deletedAt)
{
    $this->deletedAt = $deletedAt;

    return $this;
}

/**
 * Get deletedAt
 *
 * @return \DateTime
 */
public function getDeletedAt()
{
    return $this->deletedAt;
}

}

1 个答案:

答案 0 :(得分:2)

我相信你有两个选择:

  1. 在您使用实体的表单定义中添加property选项

  2. 在您的实体中定义__toString() { return $this->username; }

  3. 这是我理解你的问题。