本文整理汇总了PHP中Sonata\AdminBundle\Admin\AdminInterface::getModelManager方法的典型用法代码示例。如果您正苦于以下问题:PHP AdminInterface::getModelManager方法的具体用法?PHP AdminInterface::getModelManager怎么用?PHP AdminInterface::getModelManager使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Sonata\AdminBundle\Admin\AdminInterface
的用法示例。
在下文中一共展示了AdminInterface::getModelManager方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fixFieldDescription
/**
* The method defines the correct default settings for the provided FieldDescription.
*
* {@inheritdoc}
*
* @throws \RuntimeException if the $fieldDescription does not specify a type.
*/
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
{
$metadata = null;
if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
/** @var \Doctrine\ODM\PHPCR\Mapping\ClassMetadata $metadata */
$metadata = $admin->getModelManager()->getMetadata($admin->getClass());
// set the default field mapping
if (isset($metadata->mappings[$fieldDescription->getName()])) {
$fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]);
}
// set the default association mapping
if ($metadata->hasAssociation($fieldDescription->getName())) {
$fieldDescription->setAssociationMapping($metadata->getAssociation($fieldDescription->getName()));
}
}
if (!$fieldDescription->getType()) {
throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
}
$fieldDescription->setAdmin($admin);
$fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
$mappingTypes = array(ClassMetadata::MANY_TO_ONE, ClassMetadata::MANY_TO_MANY, 'children', 'child', 'parent', 'referrers');
if ($metadata && $metadata->hasAssociation($fieldDescription->getName()) && in_array($fieldDescription->getMappingType(), $mappingTypes)) {
$admin->attachAdminClass($fieldDescription);
}
}
示例2: fixFieldDescription
/**
* The method defines the correct default settings for the provided FieldDescription
*
* @param \Sonata\AdminBundle\Admin\AdminInterface $admin
* @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
* @return void
*/
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
{
if ($admin->getModelManager()->hasMetadata($admin->getClass())) {
$metadata = $admin->getModelManager()->getMetadata($admin->getClass());
// set the default field mapping
if (isset($metadata->fieldMappings[$fieldDescription->getName()])) {
$fieldDescription->setFieldMapping($metadata->fieldMappings[$fieldDescription->getName()]);
}
// set the default association mapping
if (isset($metadata->associationMappings[$fieldDescription->getName()])) {
$fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]);
}
}
if (!$fieldDescription->getType()) {
throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin)));
}
$fieldDescription->setAdmin($admin);
$fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'standard'));
if (in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_MANY, ClassMetadataInfo::MANY_TO_MANY, ClassMetadataInfo::MANY_TO_ONE, ClassMetadataInfo::ONE_TO_ONE ))) {
$admin->attachAdminClass($fieldDescription);
}
}
示例3: batchActionDelete
/**
* execute a batch delete
*
* @param array $idx
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function batchActionDelete($idx)
{
$modelManager = $this->admin->getModelManager();
$modelManager->batchDelete($this->admin->getClass(), $idx);
// todo : add confirmation flash var
return new RedirectResponse($this->admin->generateUrl('list'));
}
示例4: addFilter
/**
* {@inheritdoc}
*/
public function addFilter(DatagridInterface $datagrid, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
{
// Try to wrap all types to search types
if ($type == null) {
$guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
$type = $guessType->getType();
$fieldDescription->setType($type);
$options = $guessType->getOptions();
foreach ($options as $name => $value) {
if (is_array($value)) {
$fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, array())));
} else {
$fieldDescription->setOption($name, $fieldDescription->getOption($name, $value));
}
}
} else {
$fieldDescription->setType($type);
}
$this->fixFieldDescription($admin, $fieldDescription);
$admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
$fieldDescription->mergeOption('field_options', array('required' => false));
$filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
if (false !== $filter->getLabel() && !$filter->getLabel()) {
$filter->setLabel($admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'filter', 'label'));
}
$datagrid->addFilter($filter);
}
示例5: getBaseDatagrid
/**
* @param \Sonata\AdminBundle\Admin\AdminInterface $admin
* @param array $values
* @return \Sonata\AdminBundle\Datagrid\DatagridInterface
*/
public function getBaseDatagrid(AdminInterface $admin, array $values = array())
{
$pager = new Pager();
$pager->setCountColumn($admin->getModelManager()->getIdentifierFieldNames($admin->getClass()));
$formBuilder = $this->formFactory->createNamedBuilder('form', 'filter', array(), array('csrf_protection' => false));
return new Datagrid($admin->createQuery(), $admin->getList(), $pager, $formBuilder, $values);
}
示例6: getFormAdminType
/**
* @param FormMapper $formMapper
* @param AdminInterface $admin
* @param string $formField
* @param string $field
* @param array $fieldOptions
* @param array $adminOptions
*
* @return FormBuilder
*/
protected final function getFormAdminType(FormMapper $formMapper, AdminInterface $admin, $formField, $field, $fieldOptions = array(), $adminOptions = array())
{
$adminOptions = array_merge(array('edit' => 'list', 'translation_domain' => 'SonataClassificationBundle'), $adminOptions);
$fieldDescription = $admin->getModelManager()->getNewFieldDescriptionInstance($admin->getClass(), $field, $adminOptions);
$fieldDescription->setAssociationAdmin($admin);
$fieldDescription->setAdmin($formMapper->getAdmin());
$fieldDescription->setAssociationMapping(array('fieldName' => $field, 'type' => ClassMetadataInfo::MANY_TO_ONE));
$fieldOptions = array_merge(array('sonata_field_description' => $fieldDescription, 'class' => $admin->getClass(), 'model_manager' => $admin->getModelManager(), 'required' => false), $fieldOptions);
return $formMapper->create($formField, 'sonata_type_model_list', $fieldOptions);
}
示例7: fixFieldDescription
/**
* {@inheritdoc}
*/
public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription)
{
$fieldDescription->setTemplate('YnloAdminBundle::CRUD/list_enum.html.twig');
/** @var ModelManager $modelManager */
$modelManager = $admin->getModelManager();
if (null === $fieldDescription->getOption('enum_type') && $modelManager->hasMetadata($admin->getClass())) {
$mapping = $modelManager->getMetadata($admin->getClass())->getFieldMapping($fieldDescription->getName());
$fieldDescription->setOption('enum_type', $mapping['type']);
}
}
示例8: buildField
/**
* {@inheritdoc}
*/
public function buildField($type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
{
if ($type == null) {
$guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
$fieldDescription->setType($guessType->getType());
} else {
$fieldDescription->setType($type);
}
$this->fixFieldDescription($admin, $fieldDescription);
}
示例9: addField
/**
* @param \Sonata\AdminBundle\Admin\FieldDescriptionCollection $list
* @param string|null $type
* @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
* @param \Sonata\AdminBundle\Admin\AdminInterface $admin
*
* @return mixed
*/
public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
{
if ($type == null) {
$guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
$fieldDescription->setType($guessType->getType());
} else {
$fieldDescription->setType($type);
}
$this->fixFieldDescription($admin, $fieldDescription);
$admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
$list->add($fieldDescription);
}
示例10: preUpdate
/**
* {@inheritdoc}
*/
public function preUpdate(AdminInterface $admin, $object)
{
if (!$admin->hasRequest() || !($data = $admin->getRequest()->get($admin->getUniqid()))) {
return;
}
if (!isset($data[$this->fieldName])) {
return;
}
$modelManager = $admin->getModelManager();
if (!$modelManager instanceof LockInterface) {
return;
}
$modelManager->lock($object, $data[$this->fieldName]);
}
示例11: batchAction
/**
* return the Response object associated to the batch action
*
* @throws \RuntimeException
* @return \Symfony\Component\HttpFoundation\Response
*/
public function batchAction()
{
if ($this->get('request')->getMethod() != 'POST') {
throw new \RuntimeException('invalid request type, POST expected');
}
if ($data = json_decode($this->get('request')->get('data'), true)) {
$action = $data['action'];
$idx = $data['idx'];
$all_elements = $data['all_elements'];
} else {
$action = $this->get('request')->get('action');
$idx = $this->get('request')->get('idx');
$all_elements = $this->get('request')->get('all_elements', false);
}
$batchActions = $this->admin->getBatchActions();
if (!array_key_exists($action, $batchActions)) {
throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
}
if (count($idx) == 0 && !$all_elements) {
// no item selected
$this->get('session')->setFlash('sonata_flash_info', 'flash_batch_empty');
return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
}
$askConfirmation = isset($batchActions[$action]['ask_confirmation']) ? $batchActions[$action]['ask_confirmation'] : true;
if ($askConfirmation && $this->get('request')->get('confirmation') != 'ok') {
$data = json_encode(array('action' => $action, 'idx' => $idx, 'all_elements' => $all_elements));
$datagrid = $this->admin->getDatagrid();
$formView = $datagrid->getForm()->createView();
return $this->render('SonataAdminBundle:CRUD:batch_confirmation.html.twig', array('action' => 'list', 'datagrid' => $datagrid, 'form' => $formView, 'data' => $data));
}
// execute the action, batchActionXxxxx
$action = \Sonata\AdminBundle\Admin\BaseFieldDescription::camelize($action);
$final_action = sprintf('batchAction%s', ucfirst($action));
if (!method_exists($this, $final_action)) {
throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $final_action));
}
$datagrid = $this->admin->getDatagrid();
$datagrid->buildPager();
$query = $datagrid->getQuery();
$query->setFirstResult(null);
$query->setMaxResults(null);
if (count($idx) > 0) {
$this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
}
return call_user_func(array($this, $final_action), $query);
}
示例12: batchConfigureAcls
/**
* {@inheritdoc}
*/
public function batchConfigureAcls(OutputInterface $output, AdminInterface $admin, UserSecurityIdentity $securityIdentity = null)
{
$securityHandler = $admin->getSecurityHandler();
if (!$securityHandler instanceof AclSecurityHandlerInterface) {
$output->writeln('Admin class is not configured to use ACL : <info>ignoring</info>');
return;
}
$output->writeln(sprintf(' > generate ACLs for %s', $admin->getCode()));
$objectOwnersMsg = is_null($securityIdentity) ? '' : ' and set the object owner';
/** @var DocumentManager $om */
$om = $admin->getModelManager()->getDocumentManager();
$qb = $om->createQueryBuilder($admin->getClass());
$count = 0;
$countUpdated = 0;
$countAdded = 0;
try {
$batchSize = 20;
$batchSizeOutput = 200;
$objectIds = array();
foreach ($qb->getQuery()->iterate() as $row) {
$objectIds[] = ObjectIdentity::fromDomainObject($row);
$objectIdIterator = new \ArrayIterator($objectIds);
// detach from Doctrine, so that it can be Garbage-Collected immediately
$om->detach($row);
++$count;
if ($count % $batchSize == 0) {
list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity);
$countAdded += $batchAdded;
$countUpdated += $batchUpdated;
$objectIds = array();
}
if ($count % $batchSizeOutput == 0) {
$output->writeln(sprintf(' - generated class ACEs%s for %s objects (added %s, updated %s)', $objectOwnersMsg, $count, $countAdded, $countUpdated));
}
}
if (count($objectIds) > 0) {
list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity);
$countAdded += $batchAdded;
$countUpdated += $batchUpdated;
}
} catch (\BadMethodCallException $e) {
throw new ModelManagerException('', 0, $e);
}
$output->writeln(sprintf(' - [TOTAL] generated class ACEs%s for %s objects (added %s, updated %s)', $objectOwnersMsg, $count, $countAdded, $countUpdated));
}
示例13: addField
/**
* {@inheritDoc}
*/
public function addField(FieldDescriptionCollection $list, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
{
if ($type == null) {
$guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
$fieldDescription->setType($guessType->getType());
} else {
$fieldDescription->setType($type);
}
$this->fixFieldDescription($admin, $fieldDescription);
$admin->addShowFieldDescription($fieldDescription->getName(), $fieldDescription);
switch ($fieldDescription->getMappingType()) {
case ClassMetadata::MANY_TO_ONE:
case ClassMetadata::MANY_TO_MANY:
return;
default:
$list->add($fieldDescription);
}
}
示例14: alterNewInstance
/**
* Set a default parent if defined in the request
*
* {@inheritDoc}
*/
public function alterNewInstance(AdminInterface $admin, $object)
{
if (!$admin->hasRequest() || !($parentId = $admin->getRequest()->get('parent'))) {
return;
}
$parent = $admin->getModelManager()->find(null, $parentId);
if (!$parent) {
return;
}
switch ($object) {
case $object instanceof HierarchyInterface:
$object->setParentDocument($parent);
break;
case $object instanceof ChildInterface:
$object->setParentObject($parent);
break;
default:
throw new \InvalidArgumentException(sprintf('Class %s is not supported', get_class($object)));
}
}
示例15: batchAction
/**
* return the Response object associated to the batch action
*
* @throws \RuntimeException
* @return \Symfony\Component\HttpFoundation\Response
*/
public function batchAction()
{
if ($this->get('request')->getMethod() != 'POST') {
throw new \RuntimeException('invalid request type, POST expected');
}
$action = $this->get('request')->get('action');
$idx = $this->get('request')->get('idx');
$all_elements = $this->get('request')->get('all_elements', false);
if (count($idx) == 0 && !$all_elements) { // no item selected
$this->get('session')->setFlash('sonata_flash_notice', 'flash_batch_empty');
return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
}
if (!array_key_exists($action, $this->admin->getBatchActions())) {
throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
}
// execute the action, batchActionXxxxx
$action = \Sonata\AdminBundle\Admin\BaseFieldDescription::camelize($action);
$final_action = sprintf('batchAction%s', ucfirst($action));
if (!method_exists($this, $final_action)) {
throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $final_action));
}
$datagrid = $this->admin->getDatagrid();
$datagrid->buildPager();
$query = $datagrid->getQuery();
$query->setFirstResult(null);
$query->setMaxResults(null);
if (count($idx) > 0) {
$this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
}
return call_user_func(array($this, $final_action), $query);
}