【问题标题】:symfony2 Object of class could not be converted to stringsymfony2 类的对象无法转换为字符串
【发布时间】:2014-08-08 09:26:55
【问题描述】:

我在创建新用户时遇到了一些奇怪的错误。

Catchable Fatal Error: Object of class Pr\UserBundle\Entity\Group could not be converted to string in /var/www/symfony/webprojekt/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 120 

发生这种情况是因为我不想为一个用户分配多个用户组(例如 ROLE_USER),所以我将选项“用户组”更改为单个选择。

我在下面添加了用户和组的实体,但我没有看到任何问题 - 我又卡住了:(

/**
* @ORM\Entity
* @ORM\Table(name="sys_user")
*/
class User extends BaseUser
{
/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
* @ORM\Column(type="string", length=8, nullable=true)
*/
protected $mobilepin;

/**
 * @ORM\Column(type="integer", nullable=true)
 */
protected $client;

/**
 * @ORM\Column(type="integer", nullable=true)
 */
private $client_id;

/**
 * @ORM\Column(type="string", length=16383, nullable=true) //16383 = max varchar utf8
 */
private $imageurl;

/**
 * @ORM\Column(type="string", length=255, nullable=true)

 */
protected $firstname;

/**
 * @ORM\Column(type="string", length=255, nullable=true)

 */
protected $lastname;

    /**
 * @ORM\Column(type="string", length=255, nullable=true)

 */
protected $emailalert;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 */
private $phone;

/**
 * @ORM\Column(type="integer", nullable = true)


 */
protected $lock_state;

/**
* @ORM\Column(type="array", nullable=true)
 * @Assert\NotBlank()(message="Check locations")
 */
private $locations;

/**
* @ORM\Column(type="string", length=255, nullable=true)
 */
private $usergroups;

/**
 * @ORM\Column(type="string", length=5, options={"fixed" = true, "default" = "de_DE"})
   */
private $locale = 'de_DE';

/**
 * @ORM\Column(type="string", length=32)
 */
private $timezone = 'UTC';
 /**
 * @ORM\Column(type="array", nullable=true)

 */
private $created='1';

还有我组的那个

/**
* @ORM\Entity
* @ORM\Table(name="user_group")
*/
class Group
{
/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;



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


/**
 * @ORM\Column(type="string", length=255, nullable=true)

 */
protected $name;

    /**
   * @ORM\Column(type="string", length=255, nullable=true)

 */
protected $roles;

至少我是如何创建选择的:

->add('usergroups', 'entity', array('class' => 'PrUserBundle:Group','property' => 'name','required' => true, 'empty_value'    => 'Benutzergruppe','multiple'  => false, 'expanded'  => false))

这很奇怪:(

编辑:如果我从

更改“用户组”
 * @ORM\Column(type="string", length=255, nullable=true)

 * @ORM\Column(type="array", nullable=true)

它工作正常。但是,保存数据看起来像

O:26:"Pr\UserBundle\Entity\Group":3:{s:5:"

这看起来很奇怪和错误。我想过保存分配组的ID

【问题讨论】:

    标签: arrays string symfony object entity


    【解决方案1】:

    如果您想在 User 和 UserGroups 之间创建关系,您需要更改列定义:

    /**
    * @ORM\Column(type="string", length=255, nullable=true)
    */
    private $usergroups;
    

    /**
    * @ManyToMany(targetEntity="Group")
    */
    private $usergroups;
    

    然后在组中定义反向关系:

    Group {
        /**
        * @ManyToMany(targetEntity="User")
        */
        private $users;
    

    您可以在此处阅读有关教义关系的更多信息:http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html 或此处:http://symfony.com/doc/current/book/doctrine.html#entity-relationships-associations

    彼得

    【讨论】:

      【解决方案2】:

      尝试将用户实体中的“用户组”字段映射到组实体,而不是使其成为字符串字段。

      /**
       * @ManyToOne(targetEntity="Group")
       * @JoinColumn(name="group_id", referencedColumnName="id")
       */
      private $usergroups;
      

      您还可以在此处查看有关映射的文档: http://symfony.com/doc/current/book/doctrine.html#relationship-mapping-metadata http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#association-mapping

      【讨论】:

      • 谢谢大家。现在我得到一个 noob-AnnotationException: [Semantical Error] 属性 Pr\UserBundle\Entity\User::$usergroups 中的注释“@ManyToOne”从未导入。您是否可能忘记为此注释添加“使用”语句? :)
      • 在 Pr\UserBundle\Entity\User 中添加 use 语句 "use Doctrine\ORM\Mapping\ManyToOne;"
      • 好的。 JoinColumn 也有使用语句吗?概述使用 manyToMany 和其他方法时必须包含哪些内容?
      • 你可以这样做:“使用 Doctrine\ORM\Mapping 作为 ORM;”然后是“@ORM\ManyToOne”、“@ORM\JoinColumn”等。
      • 没有仍然得到那个异常......我会检查一些事情并回复你。
      猜你喜欢
      • 2016-01-11
      • 1970-01-01
      • 2022-01-05
      • 2015-07-27
      • 2012-04-29
      • 2011-04-06
      • 2011-11-30
      • 2011-08-30
      相关资源
      最近更新 更多