【问题标题】:Error with relationships between entities and setter function实体和 setter 函数之间的关系错误
【发布时间】:2013-09-22 17:50:22
【问题描述】:

我从昨天开始与这个问题作斗争,但我没有找到任何解决方案。

我有两个相互关联的实体如下:

// src/FEB/UserBundle/Entity/User.php

namespace FEB\UserBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

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

public function __construct()
{
    parent::__construct();
    // tu propia lógica
}
}

帐户.php

namespace FEB\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Account
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="FEB\UserBundle\Entity\AccountRepository")
*/
class Account
{
    /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
     * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

    /**
    * Many-To-Many, Unidirectional
    *
    * @var ArrayCollection $idusr
    *
    * @ORM\ManyToMany(targetEntity="\FEB\UserBundle\Entity\User")
    * @ORM\JoinTable(name="accounts_users",
    *      joinColumns={@ORM\JoinColumn(name="account_id", referencedColumnName="id")},
    *      inverseJoinColumns={@ORM\JoinColumn(name="usr_id", referencedColumnName="id")}
    * )
    */
    private $idusr;

    public function __construct() {
         $this->idusr = new \Doctrine\Common\Collections\ArrayCollection();
    }

/**
 * Set idusr
 *
 * @param string $idusr
 * @return Account
 */
public function setIdusr($idusr)
{
    $this->idusr = $idusr;

    return $this;
}

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

没有要构建的表单,因为用户不需要输入任何数据,所以只使用setter,而不是AccountType中的buildForm方法。 在 Account entity 中,除了与 User 实体“setIdusr”相关的字段的 setter 之外,所有 setter 函数都可以正常工作。它向我抛出了这个错误:

Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, string given, called in C:\xampp\htdocs\FEB\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php on line 528 and defined in C:\xampp\htdocs\FEB\vendor\doctrine\collections\lib\Doctrine\Common\Collections\ArrayCollection.php line 47

我该如何解决这个问题?

先谢谢你了。

编辑:我将尝试解释它应该如何运作:当我创建一个新帐户时,它应该自动在关系表“account_user”中创建一个条目,其中包含新帐户 ID 和用户 ID 的 ID。

【问题讨论】:

  • 提供的2个类/文件不是报错中提到的文件!你能提供错误行吗? UnitOfWork.php 第 528 行 ArrayCollection.php 第 47 行
  • 错误很明显。您正在将一个字符串传递给某个需要数组的类 __construct($variable)。
  • 那些类:UnitOfWork.php on line 528 ArrayCollection.php line 47 是 Symfony2 的核心类。我不写那些类。我在其他情况下建立了这些实体关系,它工作得很好,唯一的区别是他们建立了表单,以便用户输入数据,现在是输入数据的应用程序,没有表单。
  • 我将尝试解释它应该如何运作:当我创建一个新帐户时,它应该自动在关系表“account_user”中创建一个条目,其中包含新帐户 ID 和用户 ID 的 ID .

标签: php entity-framework symfony doctrine-orm


【解决方案1】:

据我现在所见,在父类中,您的 $id 是私有的,而在子类中,您再次将其定义为受保护。

所以要么不要使用相同的变量名。
或在父级中将其定义为受保护。

【讨论】:

  • 同样的错误,我定义了 $id protected 并得到同样的错误。
【解决方案2】:

我已经解决了!!!, 我必须在 Account 实体中创建一个“addUser”方法,然后我必须传递一个 User 对象参数。 我传递了一个字符串作为参数,这就是错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多