我遇到了一个问题,其中我有两个要向其中添加数据的实体(一个业务实体和一个地址实体),以及一个可以输入业务的页面以及一个地址。
一个企业可以有许多不同的站点,因此可以有许多不同的地址。每个地址实体都有一个业务属性,它引用业务实体的 ID(这通过外键等反映在数据库中)。
我遇到的问题是,当在表单中输入企业和地址时,提交失败,因为 symfony 没有将新创建的企业 ID 插入到新创建的地址中。事实上,据我所知,根据堆栈跟踪,地址查询首先被执行。
地址实体
namespace xxx\BusinessFinderBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Address
*
* @ORM\Table(name="Address")
* @ORM\Entity
*/
class Address
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="AddressOne", type="string", length=255, nullable=true)
*/
private $addressone;
/**
* @var string
*
* @ORM\Column(name="AddressTwo", type="string", length=255, nullable=true)
*/
private $addresstwo;
/**
* @var string
*
* @ORM\Column(name="AddressThree", type="string", length=255, nullable=true)
*/
private $addressthree;
/**
* @var string
*
* @ORM\Column(name="AddressFour", type="string", length=255, nullable=true)
*/
private $addressfour;
/**
* @var string
*
* @ORM\Column(name="City", type="string", length=255, nullable=true)
*/
private $city;
/**
* @var string
*
* @ORM\Column(name="County", type="string", length=255, nullable=true)
*/
private $county;
/**
* @var string
*
* @ORM\Column(name="PostCode", type="string", length=255, nullable=true)
*/
private $postcode;
/**
* @var string
*
* @ORM\Column(name="Description", type="string", length=255, nullable=true)
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="Latitude", type="string", length=45, nullable=true)
*/
private $latitude;
/**
* @var string
*
* @ORM\Column(name="Longitude", type="string", length=45, nullable=true)
*/
private $longitude;
/**
* @var \Business
*
* @ORM\ManyToOne(targetEntity="Business", inversedBy="address", cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="Business", referencedColumnName="id")
* })
*/
private $business;
/**
* @var \Country
*
* @ORM\ManyToOne(targetEntity="Country", inversedBy="address")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="Country", referencedColumnName="id")
* })
*/
private $country;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set addressone
*
* @param string $addressone
* @return Address
*/
public function setAddressone($addressone)
{
$this->addressone = $addressone;
return $this;
}
/**
* Get addressone
*
* @return string
*/
public function getAddressone()
{
return $this->addressone;
}
/**
* Set addresstwo
*
* @param string $addresstwo
* @return Address
*/
public function setAddresstwo($addresstwo)
{
$this->addresstwo = $addresstwo;
return $this;
}
/**
* Get addresstwo
*
* @return string
*/
public function getAddresstwo()
{
return $this->addresstwo;
}
/**
* Set addressthree
*
* @param string $addressthree
* @return Address
*/
public function setAddressthree($addressthree)
{
$this->addressthree = $addressthree;
return $this;
}
/**
* Get addressthree
*
* @return string
*/
public function getAddressthree()
{
return $this->addressthree;
}
/**
* Set addressfour
*
* @param string $addressfour
* @return Address
*/
public function setAddressfour($addressfour)
{
$this->addressfour = $addressfour;
return $this;
}
/**
* Get addressfour
*
* @return string
*/
public function getAddressfour()
{
return $this->addressfour;
}
/**
* Set city
*
* @param string $city
* @return Address
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* @return string
*/
public function getCity()
{
return $this->city;
}
/**
* Set county
*
* @param string $county
* @return Address
*/
public function setCounty($county)
{
$this->county = $county;
return $this;
}
/**
* Get county
*
* @return string
*/
public function getCounty()
{
return $this->county;
}
/**
* Set postcode
*
* @param string $postcode
* @return Address
*/
public function setPostcode($postcode)
{
$this->postcode = $postcode;
return $this;
}
/**
* Get postcode
*
* @return string
*/
public function getPostcode()
{
return $this->postcode;
}
/**
* Set description
*
* @param string $description
* @return Address
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set latitude
*
* @param string $latitude
* @return Address
*/
public function setLatitude($latitude)
{
$this->latitude = $latitude;
return $this;
}
/**
* Get latitude
*
* @return string
*/
public function getLatitude()
{
return $this->latitude;
}
/**
* Set longitude
*
* @param string $longitude
* @return Address
*/
public function setLongitude($longitude)
{
$this->longitude = $longitude;
return $this;
}
/**
* Get longitude
*
* @return string
*/
public function getLongitude()
{
return $this->longitude;
}
/**
* Set business
*
* @param \xxx\BusinessFinderBundle\Entity\Business $business
* @return Address
*/
public function setBusiness(\xxx\BusinessFinderBundle\Entity\Business $business = null)
{
$this->business = $business;
return $this;
}
/**
* Get business
*
* @return \xxx\BusinessFinderBundle\Entity\Business
*/
public function getBusiness()
{
return $this->business;
}
/**
* Set country
*
* @param \xxx\BusinessFinderBundle\Entity\Country $country
* @return Address
*/
public function setCountry(\xxx\BusinessFinderBundle\Entity\Country $country = null)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* @return \xxx\BusinessFinderBundle\Entity\Country
*/
public function getCountry()
{
return $this->country;
}
/**
* toString
*
* @return string
*/
public function __toString()
{
return $this->addressone . " " . $this->postcode;
}
}
商业实体:
namespace xxx\BusinessFinderBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Business
*
* @ORM\Table(name="Business")
* @ORM\Entity
*/
class Business
{
public function __construct()
{
$this->address = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="Name", type="string", length=255, nullable=true)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="Description", type="text", nullable=true)
*/
private $description;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Address", mappedBy="business", cascade={"persist", "remove"})
*/
private $address;
/**
* Get address
*
* @return \Doctrine\Common\Collections\Collection $address
*/
public function getAddress()
{
return $this->address;
}
/**
* Set address
*
* @param \xxx\BusinessFinderBundle\Entity\Address $address
* @return Business
*/
public function setAddress(\xxx\BusinessFinderBundle\Entity\Address $address)
{
$this->address[] = $address;
return $this;
}
/**
* Add address
*
* @param \xxx\BusinessFinderBundle\Entity\Address $address
* @return Business
*/
public function addAddress(\xxx\BusinessFinderBundle\Entity\Address $address)
{
$this->address[] = $address;
return $this;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Business
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
* @return Business
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* toString
*
* @return string
*/
public function __toString()
{
return (string)$this->name;
}
/**
* Remove address
*
* @param \xxx\BusinessFinderBundle\Entity\Address $address
*/
public function removeAddress(\xxx\BusinessFinderBundle\Entity\Address $address)
{
$this->address->removeElement($address);
}
}
ClinicsAdmin(这是业务实体的管理员)
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use xxx\BusinessFinderBundle\Entity\Clinic;
use xxx\BusinessFinderBundle\Entity\Address;
class ClinicsAdmin extends Admin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->add('Name')
->add('Description')
->add('address')
->add('_action', 'actions', array(
'actions' => array(
'view' => array(),
'edit' => array()
)
)
);
}
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('Name', 'text')
->add('Description', 'textarea')
->add('address', 'sonata_type_admin',
array(
'by_reference' => true,
'data_class' => null,
'label' => false,
'delete' => false
),
array()
);
}
}
地址管理员
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sonata\AdminBundle\Route\RouteCollection;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use xxx\BusinessFinderBundle\Entity\Country;
class AddressAdmin extends Admin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->add('AddressOne')
->add('AddressTwo')
->add('AddressThree')
->add('AddressFour')
->add('City')
->add('County')
->add('Country')
->add('Description')
->add('Latitude')
->add('Longitude')
->add('_action', 'actions', array(
'actions' => array(
'view' => array(),
'edit' => array()
)
)
);
}
protected function configureRoutes(RouteCollection $collection)
{
$collection
->remove('create')
->remove('delete')
->remove('edit')
;
}
public function create($object)
{
var_dump($object);die();
}
public function preUpdate($obj)
{
var_dump($obj);die();
}
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('AddressOne', 'text')
->add('AddressTwo', 'text', array('required' => false))
->add('AddressThree', 'text', array('required' => false))
->add('AddressFour', 'text', array('required' => false))
->add('City', 'text')
->add('County', 'text')
->add('PostCode', 'text')
->add('country', 'sonata_type_model')
->add('Description', 'textarea', array('required' => false))
->add('Latitude', 'text', array('required' => false))
->add('Longitude', 'text', array('required' => false))
/*
->add('Address', 'entity', array(
'class' => 'xxxBusinessFinderBundle:Address',
'required' => true,
'multiple' => true,
'expanded' => true,
'by_reference' => false
),
array (
'edit' => 'list'
)
)*/;
}
}
这些在它们插入数据库的意义上起作用,但没有任何东西插入到地址实体的业务字段中,一旦我将其设置为不为空,查询就完全失败了。我想弄清楚的是如何从新创建的 Business 中获取 ID 并将其插入新创建的 Address 实体中。
任何和所有的帮助表示赞赏:)