我有一个我想要排序的类别列表(更改顺序)。我在后端使用Sonata Admin。我试图用following tutorial提供此内容。
这是我的类别实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation\SortablePosition;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Category
*
* @ORM\Table(name="category", indexes={@ORM\Index(name="fk_category_category_idx", columns={"parent_category_id"})})
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
*/
class Category
{
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* @var boolean
*
* @ORM\Column(name="enabled", type="boolean", nullable=false)
*/
private $enabled = true;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \AppBundle\Entity\Category
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="parent_category_id", referencedColumnName="id")
* })
*/
private $parentCategory;
/**
* @Gedmo\SortablePosition
* @ORM\Column(name="position", type="integer")
*/
private $position;
public function __toString()
{
//return $this->getName();
if($this->getParentCategory()){
return '(' . $this->getParentCategory()->getName() . ')' . ' ' . $this->getName();
}
else{
return $this->getName();
}
}
/**
* Set name
*
* @param string $name
*
* @return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set enabled
*
* @param boolean $enabled
*
* @return Category
*/
public function setEnabled($enabled)
{
$this->enabled = $enabled;
return $this;
}
/**
* Get enabled
*
* @return boolean
*/
public function getEnabled()
{
return $this->enabled;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set parentCategory
*
* @param \AppBundle\Entity\Category $parentCategory
*
* @return Category
*/
public function setParentCategory(\AppBundle\Entity\Category $parentCategory = null)
{
$this->parentCategory = $parentCategory;
return $this;
}
/**
* Get parentCategory
*
* @return \AppBundle\Entity\Category
*/
public function getParentCategory()
{
return $this->parentCategory;
}
/**
* Set position
*
* @param integer $position
*
* @return Category
*/
public function setPosition($position)
{
$this->position = $position;
return $this;
}
/**
* Get position
*
* @return integer
*/
public function getPosition()
{
return $this->position;
}
}
正如您所看到的,我有一个属性$position
以及 getter 和 setter 。
在 services.yml 我有:
services:
gedmo.listener.sortable:
class: Gedmo\Sortable\SortableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ "@annotation_reader" ] ]
和
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, 'PixSortableBehaviorBundle:SortableAdmin']
tags:
- { name: sonata.admin, manager_type: orm, label: Categorieën }
calls:
- [ setPositionService, ["@pix_sortable_behavior.position"]]
在 config.yml 中,我有:
stof_doctrine_extensions:
orm:
default:
sortable: true
在 CategoryAdmin 中,我有:
<?php
namespace AppBundle\Admin;
use AppBundle\Entity\Category;
use AppBundle\Repository\CategoryRepository;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
class CategoryAdmin extends AbstractAdmin
{
public $last_position = 0;
private $positionService;
protected $datagridValues = array(
'_page' => 1,
'_sort_order' => 'ASC',
'_sort_by' => 'position',
);
public function setPositionService(\Pix\SortableBehaviorBundle\Services\PositionHandler $positionHandler)
{
$this->positionService = $positionHandler;
}
protected function configureFormFields(FormMapper $formMapper)
{
$entity = new Category();
$query = $this->modelManager->getEntityManager($entity)->createQuery('SELECT c FROM AppBundle\Entity\Category c WHERE c.parentCategory IS NULL');
$formMapper
->add('name', 'text', array('label' => 'Naam'))
->add('parentCategory', 'sonata_type_model', array('label' => 'Hoofdcategorie', 'required' => false, 'query' => $query, 'multiple' => false, 'btn_add' => false))
->add('enabled', 'checkbox', array('label' => 'Actief', 'required' => false));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('name');
}
protected function configureRoutes(RouteCollection $collection)
{
$collection->add('move', $this->getRouterIdParameter().'/move/{position}');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name', null, array('label' => 'Naam'))
->add('enabled', null, array('label' => 'Actief'))
->add('parentCategory', null, array('label' => 'Hoofdcategorie'))
->add('_action', null, array(
'actions' => array(
'move' => array(
'template' => 'AppBundle:Sonata:_sort.html.twig'
),
)
)
);
//;
}
}
在视图 _sort.html.twig 中,我有:
{% if admin.isGranted('EDIT', object) and admin.hasRoute('edit') %}
{% set pix_position = sortableObjectPosition(object) %}
{% if pix_position < admin.last_position %}
<a class="btn btn-sm btn-default" href="{{ admin.generateObjectUrl('move', object, {'position': 'bottom'}) }}" title="{{ 'move_to_bottom'|trans }}">
{{- 'icon_move_to_bottom'|trans -}}
</a>
{% endif %}
{% if pix_position < admin.last_position %}
<a class="btn btn-sm btn-default" href="{{ admin.generateObjectUrl('move', object, {'position': 'down'}) }}" title="{{ 'move_down'|trans }}">
{{- 'icon_move_down'|trans -}}
</a>
{% endif %}
{% if pix_position > 0 %}
<a class="btn btn-sm btn-default" href="{{ admin.generateObjectUrl('move', object, {'position': 'up'}) }}" title="{{ 'move_up'|trans }}">
{{- 'icon_move_up'|trans -}}
</a>
{% endif %}
{% if pix_position > 0 %}
<a class="btn btn-sm btn-default" href="{{ admin.generateObjectUrl('move', object, {'position': 'top'}) }}" title="{{ 'move_to_top'|trans }}">
{{- 'icon_move_to_top'|trans -}}
</a>
{% endif %}
{% endif %}
我没有收到任何错误。但是我的“操作”列中没有显示任何按钮。 HTML是这样的:
<td class="sonata-ba-list-field sonata-ba-list-field-text" objectid="28">
<div class="btn-group">
</div>
</td>
当我从我的twig文件中转储变量 pix_position 时,我得到 0 。当我转储变量 admin.last_position 时,我也得到 0 。在我的数据库中,所有位置字段都是0.但是我如何在后端订购它们呢?
更新2:
现在显示按钮。
但是当我点击左边的那个时,他把它放在上面,而不是一个上面。对于右边的按钮也是如此....当我点击左键时,他应该只去一个地方。
答案 0 :(得分:1)
{% if admin.isGranted('EDIT', object) and admin.hasRoute('edit') %}
{% set pix_position = sortableObjectPosition(object) %}
{% if pix_position < admin.last_position %} // Won't pass
<a class="btn btn-sm btn-default" href="{{ admin.generateObjectUrl('move', object, {'position': 'bottom'}) }}" title="{{ 'move_to_bottom'|trans }}">
{{- 'icon_move_to_bottom'|trans -}}
</a>
{% endif %}
{% if pix_position < admin.last_position %} // Won't pass
<a class="btn btn-sm btn-default" href="{{ admin.generateObjectUrl('move', object, {'position': 'down'}) }}" title="{{ 'move_down'|trans }}">
{{- 'icon_move_down'|trans -}}
</a>
{% endif %}
{% if pix_position > 0 %} // Won't pass, but 1 yes
<a class="btn btn-sm btn-default" href="{{ admin.generateObjectUrl('move', object, {'position': 'up'}) }}" title="{{ 'move_up'|trans }}">
{{- 'icon_move_up'|trans -}}
</a>
{% endif %}
{% if pix_position > 0 %} // Won't pass, but 1 yes
<a class="btn btn-sm btn-default" href="{{ admin.generateObjectUrl('move', object, {'position': 'top'}) }}" title="{{ 'move_to_top'|trans }}">
{{- 'icon_move_to_top'|trans -}}
</a>
{% endif %}
{% endif %}
因此,不是将默认值初始化为0,而是将其初始化为1
<强>更新强>
实际上,如果您移动一个对象,它将会位于顶部,因为它的值为2而其他所有值都为1.
一种可能性是,默认情况下设置一个增量值,而不是将每个事物设置为1。
摘自answer:
mysql_query("
UPDATE member_profile
SET points = points + 1
WHERE user_id = '".$userid."'
");