我当前的Client-Entity有一个卸载区和一个加载区,它们都是ClientArea-Entities。
namespace ACME\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Sorien\DataGridBundle\Grid\Mapping as GRID;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* ACME\DemoBundle\Entity\Client
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="ACME\DemoBundle\Entity\ClientRepository")
*/
class Client
{
enter code here/**
* @ORM\OneToMany(targetEntity="ClientArea",mappedBy="client", cascade={"persist", "remove"})
*/
public $unloading_areas;
/**
* @ORM\OneToMany(targetEntity="ClientArea",mappedBy="client", cascade={"persist", "remove"})
*/
public $loading_areas;
}
_
class ClientArea
{
/**
* @ORM\ManyToOne(targetEntity="Client")
*/
public $client;
}
这不起作用,因为客户端只能映射1个关联。 我如何正确映射关系?
答案 0 :(得分:1)
要创建实体关系,您需要在连接表时使用密钥。您的客户端类应定义id
密钥,您需要初始化集合,如下所示:
class Client
{
//....
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="ClientArea", mappedBy="client", cascade={"persist", "remove"})
*/
public $unloading_areas;
/**
* @ORM\OneToMany(targetEntity="ClientArea", mappedBy="client", cascade={"persist", "remove"})
*/
public $loading_areas;
public function __construct() {
// Initialize collections
$this->unloading_areas = new \Doctrine\Common\Collections\ArrayCollection();
$this->loading_areas = new \Doctrine\Common\Collections\ArrayCollection();
}
// ....
}
您的ClientArea类应该如下所示:
class ClientArea
{
// ....
/**
* @ORM\Column(name="client_id", type="int", nullable=false)
*/
private $clientId;
/**
* @ORM\ManyToOne(targetEntity="Client")
* @JoinColumn(name="client_id", referencedColumnName="id")
*/
public $client;
// ....
}
现在,应该正确映射这两个实体。 要了解有关Doctrine中关联映射的更多信息,请阅读此处的文章:http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html
希望这有帮助。