我将这两个类带有 ManyToMany
之间的关联:
苗圃.php
namespace VSCrmBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
* Nursery
*
* @ORMTable(name="Nursery")
* @ORMEntity(repositoryClass="VSCrmBundleRepositoryNurseryRepository")
*/
class Nursery
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=100)
*/
private $name;
/**
* @var string
*
* @ORMColumn(name="phone_number", type="string", length=15, nullable=true)
*/
private $phoneNumber;
/**
* @var DateTime
*
* @ORMColumn(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @var DateTime
*
* @ORMColumn(name="updated_at", type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @var Address
*
* @ORMOneToOne(targetEntity="Address", inversedBy="nursery", cascade={"persist"})
* @ORMJoinColumn(name="fk_address_id", referencedColumnName="id")
*/
private $address;
/**
* @var ArrayCollection
*
* @ORMManyToMany(targetEntity="Staff", mappedBy="nurseries", cascade={"persist"})
*/
private $staff;
/**
* @var ArrayCollection
*
* @ORMOneToMany(targetEntity="Schedule", mappedBy="nursery", cascade={"persist"})
*/
private $schedule;
/**
* @var ArrayCollection
*
* @ORMOneToMany(targetEntity="Contact", mappedBy="nursery", cascade={"persist"})
*/
private $contacts;
/**
* @var ArrayCollection
*
* @ORMOneToMany(targetEntity="RegisterRecord", mappedBy="nursery", cascade={"persist"})
*/
private $registerRecords;
/**
* Nursery constructor.
*/
public function __construct()
{
$this->staff = new ArrayCollection();
$this->schedule = new ArrayCollection();
$this->createdAt = new DateTime();
$this->contacts = new ArrayCollection();
$this->registerRecord = new ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Nursery
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set phoneNumber
*
* @param string $phoneNumber
*
* @return Nursery
*/
public function setPhoneNumber($phoneNumber)
{
$this->phoneNumber = $phoneNumber;
return $this;
}
/**
* Get phoneNumber
*
* @return string
*/
public function getPhoneNumber()
{
return $this->phoneNumber;
}
/**
* Set createdAt
*
* @param DateTime $createdAt
*
* @return Nursery
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param DateTime $updatedAt
*
* @return Nursery
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set address
*
* @param VSCrmBundleEntityAddress $address
*
* @return Nursery
*/
public function setAddress(VSCrmBundleEntityAddress $address = null)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* @return VSCrmBundleEntityAddress
*/
public function getAddress()
{
return $this->address;
}
/**
* Add staff
*
* @param VSCrmBundleEntityStaff $staff
*
* @return Nursery
*/
public function addStaff(VSCrmBundleEntityStaff $staff)
{
$this->staff[] = $staff;
return $this;
}
/**
* Remove staff
*
* @param VSCrmBundleEntityStaff $staff
*/
public function removeStaff(VSCrmBundleEntityStaff $staff)
{
$this->staff->removeElement($staff);
}
/**
* Get staff
*
* @return DoctrineCommonCollectionsCollection
*/
public function getStaff()
{
return $this->staff;
}
/**
* Add schedule
*
* @param VSCrmBundleEntitySchedule $schedule
*
* @return Nursery
*/
public function addSchedule(VSCrmBundleEntitySchedule $schedule)
{
$this->schedule[] = $schedule;
return $this;
}
/**
* Remove schedule
*
* @param VSCrmBundleEntitySchedule $schedule
*/
public function removeSchedule(VSCrmBundleEntitySchedule $schedule)
{
$this->schedule->removeElement($schedule);
}
/**
* Get schedule
*
* @return DoctrineCommonCollectionsCollection
*/
public function getSchedule()
{
return $this->schedule;
}
public function __toString()
{
$stringDeMalade = $this->getName().' '.$this->getAddress()->getLocality()->getPostalCode().' '.$this->getAddress()->getLocality()->getName();
return $stringDeMalade;
}
/**
* Add contact
*
* @param VSCrmBundleEntityContact $contact
*
* @return Nursery
*/
public function addContact(VSCrmBundleEntityContact $contact)
{
$this->contacts[] = $contact;
return $this;
}
/**
* Remove contact
*
* @param VSCrmBundleEntityContact $contact
*/
public function removeContact(VSCrmBundleEntityContact $contact)
{
$this->contacts->removeElement($contact);
}
/**
* Get contacts
*
* @return DoctrineCommonCollectionsCollection
*/
public function getContacts()
{
return $this->contacts;
}
/**
* Add registerRecord
*
* @param VSCrmBundleEntityRegisterRecord $registerRecord
*
* @return Nursery
*/
public function addRegisterRecord(VSCrmBundleEntityRegisterRecord $registerRecord)
{
$this->registerRecords[] = $registerRecord;
return $this;
}
/**
* Remove registerRecord
*
* @param VSCrmBundleEntityRegisterRecord $registerRecord
*/
public function removeRegisterRecord(VSCrmBundleEntityRegisterRecord $registerRecord)
{
$this->registerRecords->removeElement($registerRecord);
}
/**
* Get registerRecords
*
* @return DoctrineCommonCollectionsCollection
*/
public function getRegisterRecords()
{
return $this->registerRecords;
}
}
Staff.php
<?php
namespace VSCrmBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
/**
* Staff
*
* @ORMTable(name="staff")
* @ORMEntity(repositoryClass="VSCrmBundleRepositoryStaffRepository")
*/
class Staff extends User
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORMColumn(name="staff_role", type="string", length=100)
*/
private $staffRole;
/**
* @var ArrayCollection
*
* @ORMManyToMany(targetEntity="Nursery", inversedBy="staff", cascade={"persist"})
* @ORMJoinTable(name="nursery_staff")
*/
private $nurseries;
/**
* Staff constructor.
*/
public function __construct()
{
parent::__construct();
$this->nurseries = new ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set staffRole
*
* @param string $staffRole
*
* @return Staff
*/
public function setStaffRole($staffRole)
{
$this->staffRole = $staffRole;
return $this;
}
/**
* Get staffRole
*
* @return string
*/
public function getStaffRole()
{
return $this->staffRole;
}
/**
* @param Nursery $nursery
*/
public function addNurseries(Nursery $nursery)
{
$this->nurseries[] = $nursery;
}
/**
* @param Nursery $nursery
*/
public function removeNurseries(Nursery $nursery)
{
$this->nurseries->removeElement($nursery);
}
/**
* @return ArrayCollection
*/
public function getNurseries()
{
return $this->nurseries;
}
}
然后,我想显示与苗圃经理链接的所有托儿所( role = ROLE_MANAGER
的工作人员)的下拉级别,所以我有这个控制器:
<?php
namespace VSCrmBundleController;
use SensioBundleFrameworkExtraBundleConfigurationSecurity;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use VSCrmBundleEntityStaff;
use VSCrmBundleFormStaffType;
class ManagerController extends Controller
{
public function dashboardAction($nursery_id)
{
$currentUser = $this->get('security.token_storage')->getToken()->getUser();
$nurseryRepo = $this->getDoctrine()->getRepository('VSCrmBundle:Nursery');
$nursery = $nurseryRepo->findOneBy(array(
'id' => $nursery_id,
'staff' => $currentUser
));
if(!$nursery)
{
throw $this->createNotFoundException("The nursery has not been found or you are not allowed to access it.");
}
return $this->render("VSCrmBundle:Manager:dashboard.html.twig", array(
'nursery' => $nursery
));
}
}
我可以在 view 中显示我的下拉词:
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dashboards<span class="caret"></span></a>
<ul class="dropdown-menu">
{% if is_granted('ROLE_MANAGER') %}
<li class="dropdown-header">Nurseries</li>
{% for nursery in app.user.nurseries %}
<li><a href="{{ path('vs_crm_manager_dashboard', {'nursery_id' : nursery.id}) }}">{{ nursery.name }} dashboard</a></li>
{% endfor %}
{% endif %}
</ul>
</li>
但是,当我单击一个随机托儿所时,我有此错误:
可捕致命错误:参数1传递给Doctrine orm papping defaultQuotestrategy :: getJointablEname()必须是类型阵列,null给定的类型,在c: wamp64 www www www app vendor vendor doctrine orm orm orm orm orm orm orm orm orm orm orm orm orm orm orm orm orm orm orm ormlib doctrine orm persisters entity basicentitypersister.php在第1669行上定义 500内部服务器错误-contexterRorexception **
日志:
[1] SymfonyComponentDebugExceptionContextErrorException: Catchable Fatal Error: Argument 1 passed to DoctrineORMMappingDefaultQuoteStrategy::getJoinTableName() must be of the type array, null given, called in C:wamp64wwwappvendordoctrineormlibDoctrineORMPersistersEntityBasicEntityPersister.php on line 1669 and defined
at n/a
in C:wamp64wwwappvendordoctrineormlibDoctrineORMMappingDefaultQuoteStrategy.php line 97
at SymfonyComponentDebugErrorHandler->handleError('4096', 'Argument 1 passed to DoctrineORMMappingDefaultQuoteStrategy::getJoinTableName() must be of the type array, null given, called in C:wamp64wwwappvendordoctrineormlibDoctrineORMPersistersEntityBasicEntityPersister.php on line 1669 and defined', 'C:wamp64wwwappvendordoctrineormlibDoctrineORMMappingDefaultQuoteStrategy.php', '97', array())
in C:wamp64wwwappvendordoctrineormlibDoctrineORMMappingDefaultQuoteStrategy.php line 97
at DoctrineORMMappingDefaultQuoteStrategy->getJoinTableName(null, object(ClassMetadata), object(MySQL57Platform))
in C:wamp64wwwappvendordoctrineormlibDoctrineORMPersistersEntityBasicEntityPersister.php line 1669
at DoctrineORMPersistersEntityBasicEntityPersister->getSelectConditionStatementColumnSQL('staff', null)
in C:wamp64wwwappvendordoctrineormlibDoctrineORMPersistersEntityBasicEntityPersister.php line 1582
at DoctrineORMPersistersEntityBasicEntityPersister->getSelectConditionStatementSQL('staff', object(Staff), null)
in C:wamp64wwwappvendordoctrineormlibDoctrineORMPersistersEntityBasicEntityPersister.php line 1724
at DoctrineORMPersistersEntityBasicEntityPersister->getSelectConditionSQL(array('id' => '4', 'staff' => object(Staff)), null)
in C:wamp64wwwappvendordoctrineormlibDoctrineORMPersistersEntityBasicEntityPersister.php line 1058
at DoctrineORMPersistersEntityBasicEntityPersister->getSelectSQL(array('id' => '4', 'staff' => object(Staff)), null, null, '1', null, null)
in C:wamp64wwwappvendordoctrineormlibDoctrineORMPersistersEntityBasicEntityPersister.php line 710
at DoctrineORMPersistersEntityBasicEntityPersister->load(array('id' => '4', 'staff' => object(Staff)), null, null, array(), null, '1', null)
in C:wamp64wwwappvendordoctrineormlibDoctrineORMEntityRepository.php line 196
at DoctrineORMEntityRepository->findOneBy(array('id' => '4', 'staff' => object(Staff)))
in C:wamp64wwwappsrcVSCrmBundleControllerManagerController.php line 21
at VSCrmBundleControllerManagerController->dashboardAction('4')
in line
at call_user_func_array(array(object(ManagerController), 'dashboardAction'), array('4'))
in C:wamp64wwwappvendorsymfonysymfonysrcSymfonyComponentHttpKernelHttpKernel.php line 153
at SymfonyComponentHttpKernelHttpKernel->handleRaw(object(Request), '1')
in C:wamp64wwwappvendorsymfonysymfonysrcSymfonyComponentHttpKernelHttpKernel.php line 68
at SymfonyComponentHttpKernelHttpKernel->handle(object(Request), '1', true)
in C:wamp64wwwappvendorsymfonysymfonysrcSymfonyComponentHttpKernelKernel.php line 169
at SymfonyComponentHttpKernelKernel->handle(object(Request))
in C:wamp64wwwappwebapp_dev.php line 28
那我做错了什么?也许做错了吗?
请添加joinColumns
和inverseJoinColumns
具有相对列名称。
Staff.php
/**
* @var ArrayCollection
*
* @ORMManyToMany(targetEntity="Nursery", inversedBy="staff", cascade={"persist"})
* @ORMJoinTable(name="nursery_staff",
* joinColumns={@ORMJoinColumn(name="nursery_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="staff_id", referencedColumnName="id")}
* )
*/
private $nurseries;
参考此处
通过更改控制器在:
中解决的问题 public function dashboardAction($nursery_id)
{
//$currentUser = $this->get('security.token_storage')->getToken()->getUser();
$nurseryRepo = $this->getDoctrine()->getRepository('VSCrmBundle:Nursery');
$nursery = $nurseryRepo->findOneBy(array('id' => $nursery_id));
if(!$nursery)
{
throw $this->createNotFoundException("The nursery has not been found or you are not allowed to access it.");
}
return $this->render("VSCrmBundle:Manager:dashboard.html.twig", array(
'nursery' => $nursery
));
}
我认为,如果您通过ManyToMany
关联中的属性进行搜索,则findOneBy
方法不起作用。